Reputation: 1031
I am trying to update some code and change a fopen() file operation with std:ofstream.During an fopen file operation fputs() is used to write contents into a fail.Checking the return of fputs ::(fputs() return a nonnegative number on success, or EOF on error
). I need to check the copy of contents into the file.
This example is taken from : cplusplus.com
// ofstream::open / ofstream::close
#include <fstream> // std::ofstream
int main () {
std::ofstream ofs;
ofs.open ("test.txt", std::ofstream::out | std::ofstream::app);
ofs << " more lorem ipsum";
ofs.close();
return 0;
}
I am trying to find a way to check ofs << " more lorem ipsum";
operation.Is there any kind of return of this operation?Does this throws something?
Warmest Regards.
Upvotes: 3
Views: 215
Reputation: 409186
Many operations on a stream, including the <<
operators (in your case the non-member operator<<
function), returns the stream itself. And the stream objects have a boolean conversion operator that you can use in a condition to check if the stream is in a good state or not.
For example like
if (ofs << " more lorem ipsum")
{
// Output operation okay
}
This is most commonly used for input.
There's also the possibility to set an exception mask which will cause the streams to throw exceptions on specific states.
Exceptions are disabled by default.
Upvotes: 3