kevorski
kevorski

Reputation: 836

Determine if file write out is true c

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

Answers (1)

paxdiablo
paxdiablo

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

Related Questions