Archived

This topic is now archived and is closed to further replies.

How do you use fstream's file.open?

This topic is 6544 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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''.

Share on other sites
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 cant find anything on it right now.

Share on other sites
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

Share on other sites
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)

• 23
• 15
• 23
• 10
• 19