By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

Random Named File Opener

P: 8
In my spare time I have decided I wanted to write a program that will open files no matter their names. This is the code I have written but the program doesn't seem to be able to find the file though it is in the same folder as the exe itself. Hopefully someone can find the flaw in it i didn't. I already asked two of my friends who know c++ and they don't have a clue.

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib> 
  2. #include <ctime> 
  3. #include <iostream>
  4. #include <sstream>
  5. #include <fstream>
  6. using namespace std;
  8. void main()
  9. {
  10.     srand((unsigned)time(0));
  12.     int random_integer;
  13.     string dir = "";
  14.     string txt = ".txt";
  15.     string file;     
  16.     string str = "";
  17.     char c;
  18.     ifstream myFile;
  20.     do {
  21.         string str = "";
  22.         for(int index=0; index<1; index++)
  23.         { 
  24.             random_integer = (rand()%25)+65;  
  25.             c = (char)random_integer;
  26.             str += c;
  27.         } 
  28.         file = str;
  29.         file.insert (1,txt);
  32.     if (!myFile) {          
  33.         cout << "Unable to open " << file << endl;
  34.     }
  35. }    while (!myFile);
  36.     cout << "File open";
  37. }
Mar 7 '07 #1
Share this Question
Share on Google+
7 Replies

P: 1,806
1) Your for loop executes exactly 1 time. (is this your plan)
2) ifstream (as far as I know) is an input stream. Since the file you are trying to open may not exist, there is little point creating an empty file for you to read.
Mar 7 '07 #2

P: 8
the loop that runs once is the loop that makes the random letter that will start the file name it is looking for.
Mar 7 '07 #3

P: 1,806
Right....but you do have 25 files existing in the directory your in right?
One named A.txt, B.txt......X.txt,Y.txt?
Because you can't open a file to read if it doesn't exist.....
Mar 7 '07 #4

P: 8
yes i have a file called P.txt and C.txt in the directory but it wont open them.
Mar 7 '07 #5

P: 8
any help that someone can give me would really help.
Mar 12 '07 #6

Expert Mod 2.5K+
P: 4,677
any help that someone can give me would really help.
Just out of curiousity, why not do a system("dir"); or system("ls"); for a list of the contents, put that into an array of strings, and try opening them? Randomly guessing the filename seems to be a bit more difficult than necessary...
Mar 12 '07 #7

P: 39
would it not be more efficient to traverse known directories or at least look for certain directory names and files to open rather than guessing file names and such. I would look at, this and that should of give you a good general idea if not solution to your problem. As for your current problem, my suggestion is to start small and work your way up. There have been numerous times where I would write code and not test it, thinking it is straightforward and it does not run they way it should.
Mar 12 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.