Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 27 Aug 2002 03:19:36 -0500 (CDT)
From:      ERIK G HAMILTON <ehamilt@siue.edu>
To:        freebsd-questions@FreeBSD.org, freebsd-bugs@FreeBSD.org
Subject:   fread returns eof too soon on a binary file
Message-ID:  <Pine.SO4.4.05.10208270302270.4016-100000@cougar.isg.siue.edu>

next in thread | raw e-mail | index | archive | help
First. I'm running FreeBSD 4.6-stable (before 4.6.2 was released) I am
using the b option in fopen to declare a binary file for portability. Even
though the man page says that it is ignored.  I've tested it on the same
file multiple times and occasionally there is no problem, but most of the 
time fread will return less than it's suppose to and checks to feof(file)
prove to be true.  Even when the file is obviously not complete.  Oddly
enough it tends to happen around the same place so I assume it's running
into a character it thinks is an eof.  I've done some searching on the net
and I can't find anything useful about an error like this, or a
workaround. I've ran the code on a redhat machine with the same file and
it hasn't once encountered the problem.

Can anyone help me out with this?  Is there some massive hole in my code
that I do not see? Something I need to #define?



BLOCKSIZE = 1024*4
size = size of the file
offset = offset on where to begin on that file
ctr = how much of the file has been sent


file = fopen(path, "rb");
if (file != NULL)
{
    if (fseek(file, (long)offset, 0) == 0)
    {
        ctr = offset;
        while(ctr < size)
        {
            if (size - ctr > BLOCKSIZE)
            {
                len = BLOCKSIZE;
            }
            else
            {
                len = size - ctr;
            }
            if (len)
            {
                ret = fread(buff, 1, len, file);
                if (ferror(file))
                {
                    break;
                }
                if (ret > 0)
                {
                    pfd.fd = sock;
                    pfd.events = POLLOUT;

                    if (poll(&pfd, 1, 30000) > 0)
                    {
                        status = send(sock, buff, len, 0);
                        if (status <= 0)
                        {
                            break;
                        }
                        ctr += status;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }

    }
    fclose(file);
}


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.SO4.4.05.10208270302270.4016-100000>