🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How do you use fstream's file.open?

Started by
2 comments, last by VECTOR 24 years ago
fstream file; file.open ".\\data\\tstfile.txt",ios::in/ios::nocreate ); if ( !file ) { MessageBox( NULL, "No File", "INFO", NULL ); return 0; } I want to open file ''data/tstfile.txt'' and it says ''NO FILE''.
The object of war is not to die for your country, but to make the other bastard die for his . . -General MacArthur
Advertisement
Hello,

fstream file;
file.open("data/tstfile.txxt",ios::nocreate);
if(!file.is_open())
{
//insert relevant Error message
return 0;
}

As far as I know ios::in is the default mode argument. And ios::nocreate should work even though I can`t find anything on it right now.


ios::in is only the default if you use ifstream and not fstream.
The default for fstream, as stated in the stl documentation for VC6 is
ios::in // ios:: out  
.

If you are opening a file for reading, you do not need to specify nocreate. I may be wrong here, but as long as it's input, it will fail if the file does not exist and not try to open one, so ios::nocreate seems redundant.

ifstream inStream;inStream.open("file.txt");if ( !inStream ){    cerr << "Unable to open file." << endl;}   

The above should work.

From the code that you submitted, I would assume that you only want to open a file for reading. If so, use ifstream instead of fstream. If you need to read/write then use fstream.

There is nothing wrong with using fstream though for either situation.

If my pipe doesn't work, could someone tell me how to display a single pipe. I tried using a double pipe to see what would happen.

Thanks


Edited by - AlekM on June 22, 2000 2:33:50 PM
file.open( ".\\data\\tstfile.txt",ios::in/ios::nocreate );

if the / is a binary-or, your file may be placed incorrect.

when "compiling" is has to be in the work folder of your project. If you run the the exe manually it has to to in the debug/release dir.

eg in msvc++.

\\project\\workspace.dsp
\\project\\data\\tstfile.txt

if you run manually:
\\project\\debug\\my.exe
\\project\\debug\\data\\tstfile.txt

if you have done this, and it still dosn''t work. Try taking properties on the file, and see if its not named tstfile.txt.txt
. notepad likes to f*** it up.

add a /ios::binary to read binary files. (/ is also a binary-or)
Ries

This topic is closed to new replies.

Advertisement