feof() - end of file is reached almost in the start using fread()
<p>I have a function in C which reads files and writes them over to socket, 
I am using <code>fread()</code> function to read file and then write whatever number of bytes are read to the socket, but seems like the end of file is reached a lot earlier in my case, I could not detect the cause yet.
The file is roughly 7MB but <code>feof()</code> is seen after reading almost 5K bytes..</p>

<p>the code snippet looks like this</p>

<pre><code>    long total = 0;
    const long BUF_LENGTH = 1024  ;
    char buffer[BUF_LENGTH];
    FILE* f = fopen(filename.data(), "r");

    if(f == NULL)
    {
       return -1;           
    }

    while(true)
    {
        size_t read = fread(buffer, sizeof(char),  BUF_LENGTH, f);

        if(read &gt; 0)
        {
            client.sendData(buffer, read);
            total += read;
        }
        //if the read number of bytes are less than our buffer , it must be an error or eof ?
        if(read &lt; BUF_LENGTH)
        {
            if(ferror(f))
            {
                //throw an exception..
            } 
            else if (feof(f))
            {
                //this code is reached after 5757 bytes only ..
                break;
            }
        }
    }   
</code></pre>

<p>The <code>feof()</code> block in the above code hits long before file is fully sent, I do not know why, I must be doing something wrong here,  I can not use <code>fgetc()</code> because of performance, </p>

<p>Thanks for your help..</p>
