How do you check if a file appended successfully or has written to a file successfully. I thought it would be something like this:
1 2 3 4 5 6 7 8 9
//appending to file
ofstream file;
file.open("Text.txt", ios::out | ios::app)
file << "string";
//checkin to see if it appended successfully
if(file.good())
returntrue;
elsereturnfalse;
ofstream file;
errno_t ferror;
ferror= file.open("Text.txt", ios::out | ios::app)
if(ferror !=0 )
{
cout<<"append operation failed";
exit(0);
}
file << "string";
//checkin to see if it appended successfully
if(file.good())
returntrue;
elsereturnfalse;
the file operations return different values to indicate different results, usually 0 is returned on success ,read more on individual operations to see what and all values they return
You orig fragment can be rewritten like this (I like complete functions...)
1 2 3 4 5 6 7 8 9
void test_append_to_file() {
ofstream file("Text.txt", ios::out | ios::app);
if ( !file.is_open() )
returnfalse; // exiting the app due to a file open failure is too draconian
if (file << "string")
returntrue;
returnfalse;
}