Random Named File Opener

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Firepony
    New Member
    • Mar 2007
    • 8

    Random Named File Opener

    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.

    Code:
    #include <cstdlib> 
    #include <ctime> 
    #include <iostream>
    #include <sstream>
    #include <fstream>
    using namespace std;
    
    void main()
    {
    	srand((unsigned)time(0));
    
    	int random_integer;
    	string dir = "";
    	string txt = ".txt";
    	string file; 	
    	string str = "";
    	char c;
    	ifstream myFile;
    	
    	do {
    		string str = "";
    		for(int index=0; index<1; index++)
    		{ 
    			random_integer = (rand()%25)+65;  
    			c = (char)random_integer;
    			str += c;
    		} 
    		file = str;
    		file.insert (1,txt);
    		myFile.open(file.c_str());
    		
        if (!myFile) {          
            cout << "Unable to open " << file << endl;
        }
    }	while (!myFile);
    	cout << "File open";
    }
    Last edited by Ganon11; Mar 7 '07, 12:19 PM. Reason: code tags added
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    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.

    Comment

    • Firepony
      New Member
      • Mar 2007
      • 8

      #3
      the loop that runs once is the loop that makes the random letter that will start the file name it is looking for.

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

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

        Comment

        • Firepony
          New Member
          • Mar 2007
          • 8

          #5
          yes i have a file called P.txt and C.txt in the directory but it wont open them.

          Comment

          • Firepony
            New Member
            • Mar 2007
            • 8

            #6
            any help that someone can give me would really help.

            Comment

            • sicarie
              Recognized Expert Specialist
              • Nov 2006
              • 4677

              #7
              Originally posted by Firepony
              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...

              Comment

              • lqdeffx
                New Member
                • Mar 2007
                • 39

                #8
                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.

                Comment

                Working...