ifstream and its open method

Started by
9 comments, last by Kylotan 16 years, 2 months ago
Hi, I've got a little problem with std::ifstream ... I have a piece of code like that :

std::ifstream file;
file.open("a_filename_i_m_sure_doesn_t_exist", std::ios::in | std::ios::binary);
if (file.is_open() == false)
    return(false);
I'd like this piece of code to return false, but unfortunately, it refuses to do so, although there is absolutely no file named "a_filename_i_m_sure_doesn_t_exist" on my harddrive. I've tried using file.fail() or file.eof() but none returned true ... How should I do to test if a file was opened or not ??? I thought specifying std::ios::in in the openmode should tell the class to NOT create the file if it doesn't exist, but it seems not :s Any help welcomed :) Thx
Advertisement
Try the 'std::ios::nocreate' flag.
ios::in is not needed for ifstreams
Quote:
file.open("a_filename_i_m_sure_doesn_t_exist", std::ios::in | std::ios::binary);
if (file.is_open() == false)
return(false);

How should I do to test if a file was opened or not ???


The ifstream method is_open() returns a boolean on whether the file is open. Since the value itself is a boolean, you do not need to compare it with false, i.e.
std::ifstream file;file.open("a_filename_i_m_sure_doesn_t_exist");if(!file.is_open()){...} 

- If the file is not open, is_open() returns false, but you want to enter the if statement when the file does not open, so you add the ! (not) operator; !false = true, so you enter the statement.

Now, if you just want your program to exit if the file was not opened, you enter in some code within this if statement to throws an exception and quits the program, or just throws an exception (error message), or does whatever you want it to do.

Or, if this code is within a method of type boolean, say fileOpenedCorrectly(), then within your if statement (that you only enter if the file was not opened) you insert your return false; statement. If the file was loaded correctly, you wouldn't enter the if statement and you would continue on to a return true; statement (since the file opened correctly).

Hope this answers your question!
The std::ios::nocreate is not defined kylotan ... that would effectively help me :)

Shakedown, thx for your little tutorial, but I think you misunderstood my problem : my problem is the ifstream opening a file which DOESN'T EXISTS, without any way to catch that.

How am I going to tell if the file was simply empty or if it doesn't exists, if the ifstream behaves exactly the same way in both cases ??


Edit : it was in fact "std::ios::_Nocreate" in my implementation (VS2008 Express) And it now works :) Thx a lot Kylotan !!
Note that the underscore and the capital letter mean it's non-portable/non-standard - if you want portable operation, try using both the in and out flags at the same time and see if that gives the results you want.
I don't need my app to be portable, but if I use "std::ios::in | std::ios::out" as the openmode, the "out" flag will tell the stream to create the file if it doesn't already exists, no ? So that won't help ?
Anyway, I'll keep in mind that _Nocreate is specific to VS2008, and hope that my problem was only due to their implementation of the STL :)
I cannot get my MSVC2008 std::ifstream to act the same as yours, with any combination of std::ios::in and std::ios::binary. It simply doesn't create the file. Can you post a complete example?
#include <iostream>#include <fstream>int main(){	std::ifstream in("in.file",std::ios::in | std::ios::binary);		if(in.is_open())	{		std::cout << "in is open\n";	}	else	{		std::cout << "in isn't open\n";	}}
Almost the same as yours ... but I re-created a simple project, and now it works ... makes me wonder if there wasn't something more complicated going on ...
I can't really post anything else, but I think I will have to check my class to see if there is not a stack overflow or something like that which might corrupt my ifstream -_-
I like stupid bugs like that, you always learn a lot :)
Quote:Original post by Kylotan
Note that the underscore and the capital letter mean it's non-portable/non-standard
Underscore or not, I don't think(?) that std::ios::nocreate is standard defined anyway.

std::ifstream isn't meant to create a file if it doesn't exist, it shouldn't need any flags for this. If you were to use the old .h style headers that might cause non-standard behaviour, but then they probably wouldn't be in the std:: namespace, hmmm.

This topic is closed to new replies.

Advertisement