I want to generate file through ofstream
, here are the codes.
void testOfstream(string filename){
ofstream ofs(filename, ofstream::out | ofstream::trunc | ofstream::binary);
char body[] = { 'a', 'b', 'c' };
ofs.write(body, sizeof(body)/sizeof(char));
ofs.close();
}
filename
is C:\\MyProject\\CodeTest2010\\CodeTest2010\\test.txt
, the test.txt
file is created successfully.filename
is C:\\MyProject\\CodeTest2010\\CodeTest2010\\
, or C:\\MyProject\\CodeTest2010\\CodeTest2010
, nothing is changed in the folder CodeTest2010. I think it should throw exception to warning that the input file name is invalid.My question is why it keeps silence instead of throwing exception, when the input file is folder not a filename?
According to this, I do not find any information about the folder name is input into ofstream.
IOStreams by default do not throw exceptions. Instead they delegate error indication to a bitmask type representing certain stream errors. The inability to open a file is classified as a recoverable error and therefore it sets std::ios_base::failtbit
in its stream state. You can enable exceptions by specifying this bit in the parameters of the exceptions()
method:
ofs.exceptions(std::ios_base::failbit);