Q:

two consecutive fwrites on same file

i have read all sources and I tried to understand why this code is giving such output, but i couldn't understand. Please if someone could give me specific answers....
#include<stdio.h>

int main()
{
    FILE *fp1;
    FILE *fp2;

    fp1=fopen("abc","w");
    fp2=fopen("abc","w");

    fwrite("BASIC",1,5,fp1);
    fwrite("BBBBB CONCEPTS",1,14,fp2);

    return 0;
}

The output is BASIC CONCEPTS when i opened the file "abc". 
Why has second fwrite  not overwritten the contents of file "abc"?
the expected output should be BBBBB CONCEPTS

A:

The problem is, that you are using the buffered fwrite() instead of the unbuffered write(). The later tells the kernel: "write this stuff to the file now", the first tells the standard C library "write this stuff to the file whenever you see fit". Obviously, that standard C library implementation has flushed the stuff from fp2 first, then overwriting it with the stuff from fp1.
Of course, you can enforce the correct flushing sequence by calling fflush() yourself, and/or actually closing your files.

