I use this when include fstream.hWhat should I write when using fstream? (with namespace)Code:ifstream obj(path,ios::nocreate)
I use this when include fstream.hWhat should I write when using fstream? (with namespace)Code:ifstream obj(path,ios::nocreate)
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
In the case of ifstream, it is not necessary. If the file does not exist, the ifstream won't open it and won't create it. Just remove the flag and check obj.is_open after the constructor to verify that the file was opened or not.
So, what if we want to force it to create a file?
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
I don't see why you'd want to force it to create a file if you are using an ifstream (which is for input files). If you are using an ofstream (for output files) or a plain fstream, then the file will be created automatically.
If you are using an ofstream and you don't want to create the file (which is what nocreate was likely used for), then you could just use an ifstream to open the file for reading. If that succeeds, then close it and open the file for writing with your ofstream. If the ifstream open fails, then the file doesn't exist.