From owner-freebsd-questions Tue Aug 27 1:20: 5 2002 Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.FreeBSD.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 4A0F537B401 for ; Tue, 27 Aug 2002 01:19:53 -0700 (PDT) Received: from mail.isg.siue.edu (mail.isg.siue.edu [146.163.5.4]) by mx1.FreeBSD.org (Postfix) with ESMTP id CB0FB43E6E for ; Tue, 27 Aug 2002 01:19:51 -0700 (PDT) (envelope-from ehamilt@siue.edu) Received: from WEBSHIELD1.isg.siue.edu (webshield1.isg.siue.edu [146.163.5.149]) by mail.isg.siue.edu (8.9.1/8.9.1) with SMTP id DAA10886 for ; Tue, 27 Aug 2002 03:19:42 -0500 (CDT) Received: FROM mail.isg.siue.edu BY WEBSHIELD1.isg.siue.edu ; Tue Aug 27 03:19:41 2002 -0500 Received: from cougar.isg.siue.edu (cougar [146.163.5.29]) by mail.isg.siue.edu (8.9.1/8.9.1) with ESMTP id DAA10856; Tue, 27 Aug 2002 03:19:37 -0500 (CDT) Received: from localhost (ehamilt@localhost) by cougar.isg.siue.edu (8.9.1/8.9.1) with ESMTP id DAA04715; Tue, 27 Aug 2002 03:19:36 -0500 (CDT) Date: Tue, 27 Aug 2002 03:19:36 -0500 (CDT) From: ERIK G HAMILTON To: freebsd-questions@FreeBSD.org, freebsd-bugs@FreeBSD.org Subject: fread returns eof too soon on a binary file Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG 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-questions" in the body of the message