473,382 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib> 
  2. #include <ctime> 
  3. #include <iostream>
  4. #include <sstream>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8. void main()
  9. {
  10.     srand((unsigned)time(0));
  11.  
  12.     int random_integer;
  13.     string dir = "";
  14.     string txt = ".txt";
  15.     string file;     
  16.     string str = "";
  17.     char c;
  18.     ifstream myFile;
  19.  
  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);
  30.         myFile.open(file.c_str());
  31.  
  32.     if (!myFile) {          
  33.         cout << "Unable to open " << file << endl;
  34.     }
  35. }    while (!myFile);
  36.     cout << "File open";
  37. }
Mar 7 '07 #1
7 2096
DeMan
1,806 1GB
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
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
DeMan
1,806 1GB
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
yes i have a file called P.txt and C.txt in the directory but it wont open them.
Mar 7 '07 #5
any help that someone can give me would really help.
Mar 12 '07 #6
sicarie
4,677 Expert Mod 4TB
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
lqdeffx
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

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

Similar topics

3
by: Rob B | last post by:
Hello, I am just starting to learn Python and was writing a simple script on my machine (Mac OS X 10.3.4), but I can't seem to import the random module: #!/usr/bin/env python import random
6
by: Diodak | last post by:
**************************************** How can get RANDOM record from XML file? **************************************** for e.g. in xml files i have aprox.200 records. how get random position...
3
by: Heath | last post by:
Using MSIE 5+ Heath writes: My problem deals with working with window objects between pages as follows: My Introduction page contains a link to my Action page. The onClick of that link...
5
by: Christopher Brandsdal | last post by:
Hi! I'm not sure, but this might be a question not right for this group. Problem: I have a sql statement that selects all rows with a sertain criteria, an display it on a page. What I need...
14
by: DataSmash | last post by:
Hi, When I import the random module at the python interpreter, it works fine: >>> import random >>> x = random.randint(1,55) >>> print x 14 >>> BUT, when I put the same code in a python...
4
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. ...
16
by: HMS Surprise | last post by:
I edited environment varialbes and have added C:\Python25\Lib to PYTHONPATH but get the no module message when the statement import urllib is executed. Even tried copying the urllib file to...
9
by: cdm2883 | last post by:
Ok im trying to make a lil random number game. I have most of the code and comments on how i want the game to work. but i can not get to it work. if you have any answers any help what so ever it woul...
2
newnewbie
by: newnewbie | last post by:
I am learning VBA and need a little help with a macro that I am writing. I have a worksheet with data that I split into multiple worksheets based on criteria in one column. E.g. Column A has unique...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.