Reputation: 836
Is there a way to find out if a file write occurred in c. I'm supposed to return a 0 if I wrote to the file and -1 if nothing was written.
Upvotes: 0
Views: 51
Reputation: 882068
If you're using fwrite()
, for example, the return code is the number of items written.
Similarly, fprintf()
returns the number of characters written.
So you can examine the return code to figure out if anything was written, something like:
if (fprintf (fh, "val=%d\n", value) == 0)
return -1;
return 0;
or:
if (fwrite (buffer, sizeof(something), 7, fh) == 0)
return -1;
return 0;
Keep in mind you're asking for a nothing/something indication and the something means partial or complete success. If you want to know how much of what you requested was written, hat's a tiny bit more complex.
Upvotes: 1