how do i get my program to check if the file is actually there or not
i.e
fileIn("text.txt")
but text.txt doesnt exist
thanks for any help
how do i get my program to check if the file is actually there or not
i.e
fileIn("text.txt")
but text.txt doesnt exist
thanks for any help
Hooked On Phonics Didn't Work For Me!
Or, for C++ stream objects...
Code:ifstream infile ("/home/user/file.txt", ios::in); if (!infile) { cerr << "File does not exist!\n"; exit (1); } //Do stuff
now it tells me that every file that i enter doesnt exist
Hooked On Phonics Didn't Work For Me!
First, are you using vVv's file pointer method or the stream menthod I described?
Second, if possible, post the control statement that you use to determine if a file exists.
i tried using vvv's but i couldnt get it to work, now im using yours but every file in try it tells me that it doesnt exist
Hooked On Phonics Didn't Work For Me!
Erm, you problem lies in your use of globals.
globals in this context are bad.
Your problem is probably that the global fileIn (exists at all) is an int, not an ifstream.
If you really must use globals (which you really mustn't) You need to make the global fileIn an ifstream object and use fileIn.open("filename", ios::in) to open it.
But you really ought to get those globals squared away. They cause nothing but trouble in that context, as you problem here so aptly illustrates.
Last edited by Imperito; 11-03-2002 at 12:47 PM.
i dont understand
Hooked On Phonics Didn't Work For Me!
The way you declare your variables globally (outside of any function) is what is causing this problem.
The quick fix would be to chance the global declaration of "int fileIn" to "ifstream fileIn". But this isn't a good solution.
Not by a long shot.
Your use of global variable is only going to make more problems as the program gets bigger, you really need to switch over to parameter passing and return values for your functions.
the program is most likely not going to get any bigger so how would i do what you mentioned here.The quick fix would be to chance the global declaration of "int fileIn" to "ifstream fileIn". But this isn't a good solution.
Hooked On Phonics Didn't Work For Me!
Just that.
At the top of the file, in your global variable declarations (Which regardless of program size you really ought not to use, its a bad habit to get into and it keeps you from developing proper parameter passing) at the very bottom you have "int fileIn" change that to "ifstream fileIn"
Then later on, when you have ifstream fileIn("whatever", ios::in), change it to fileIn.open("whatever", ios::in);
But I'll say it again, and once more for clarity, don't use global variables like that, its bad.
i just cant figure it out. i changed the stuff you said to change to fix it but i just cant get it right for some reason.
the problem seems to be the changing the int filein to the ifstream filein.
Hooked On Phonics Didn't Work For Me!
ok, ive been researching on this subject all day and ive finally narrowed it down to 2 errors which are..
when i first used ifstream fileIn(/*file here*/)
i used fileIn >> array[i] during a loop to open the contents into an array.
now that i switched to using the method vVv described above what would i use to put the contents of the file into the array?
Hooked On Phonics Didn't Work For Me!