user6722
user6722

Reputation: 21

Difference between write and fwrite not the same results

If I have piece of code that writes out text like this: write(1, buf, bytes); why doesn't the same works for writing to a file fwrite(1, buf, bytes,f1);?

Where f1 is declared as FILE *f1; and f1=fopen("Test.txt", "wb");. The tutorials I'm looking at indicate that it should work. I'm C# coder and not a C coder and would like some help with this.

Upvotes: 0

Views: 2385

Answers (1)

Jack
Jack

Reputation: 133557

The signature of fwrite is:

fwrite(const void * ptr, size_t size, size_t count, FILE * stream );

While the signature of write is:

write(int fd, const void *buf, size_t count);

If you match your examples you'll see that parameter don't match.

count of write should be size*count of fwrite (which let you specify the size of every element you are writing). In addition write has a file descriptor hardcoded as 1 and which you replace with a FILE* obtained from fopen, how do you know that 1 is referring to that file?

Upvotes: 2

Related Questions