Friday, 15 April 2011

c++ - Should ofstream throw exception when folder name is input? -



c++ - Should ofstream throw exception when folder name is input? -

i want generate file through ofstream, here 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(); } if parameter filename c:\\myproject\\codetest2010\\codetest2010\\test.txt, test.txt file created successfully. if paramter filename c:\\myproject\\codetest2010\\codetest2010\\, or c:\\myproject\\codetest2010\\codetest2010, nil changed in folder codetest2010. think should throw exception warning input file name invalid.

my question why keeps quiet instead of throwing exception, when input file folder not filename?

according this, not find info folder name input ofstream.

iostreams default not throw exceptions. instead delegate error indication bitmask type representing stream errors. inability open file classified recoverable error , hence sets std::ios_base::failtbit in stream state. can enable exceptions specifying bit in parameters of exceptions() method:

ofs.exceptions(std::ios_base::failbit);

c++ ofstream

No comments:

Post a Comment