fread() after fwrite() gets 0 bytes
<p>I am set a big buffer for the <code>FILE*</code> object(size of <code>8 * 1024</code>) and I'm making a write of 4 bytes to it and then I read of the 4, but the read fail.
If I flush the <code>FILE*</code> the read succeeds.</p>

<pre><code>typedef struct _MyFile {                 /* file descriptor */
    unsigned char fileBuf[8*1024];
    FILE *file;
} MyFile;

int main() {
    MyFile myFile;
    int res;
    char bufWrite[4] = { 1, 2, 3, 4 };
    char bufRead[4];

    /* Open file for both reading and writing */
    myFile.file = fopen("myfile.txt", "w");

    /* Change the file internal buffer size */
    setvbuf(myFile.file, myFile.fileBuf, _IOFBF, sizeof(myFile.fileBuf));

    /* Write data to the file */
    res = (int)fwrite(buf, 1, 4, myFile.file);
    printf("write: res = %d\n", res);

    /* Seek to the beginning of the file */
    fseek(fp, SEEK_SET, 0);

    /* Read and display data */
    res = (int)fread(buf, 1, 4, myFile.file);
    printf("read: res = %d\n", res);
    fclose(myFile.file);

    return 0;        
}
</code></pre>

<p>The output is:</p>

<pre><code>write: res = 4
read: res = 0
</code></pre>

<p>If I write more than 8K or use <code>write()</code> instead of <code>fwrite()</code> the <code>fread()</code> works well.</p>

<p><strong>The thing is I can't use <code>fflush()</code>!!!</strong></p>

<p>Is there any way to fix it?</p>