Subject: B18&17.1 problem reading large files (more than 28k!) with read() or fread()
From: [email-masked] (Paul Toseland)

17 May 1997 10:09:38 -0700
Approved: [email-masked]
Distribution: cygnus
Message-ID: <[email-masked]>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Mailer: Mozilla 3.01 (Win95; I)
Original-To: [email-masked]
Original-Sender: [email-masked]

I stat a file (a Net$cape FAT.DB), using its DOS filename, and it tells
me it is 692224 bytes long.  I open it, fstat it, with the same result. 
I read from it, it reads 28720 bytes and gives end of file.  The same
result happens whether I use low-level I/O or streams, or whether I use
the DOS filename or the UNIX filename.  It didn't work with b17.1
either.

Code:

#include "stdio.h"
#include "_String.h"
#include "sys\stat.h"
#include "fcntl.h"
#include "unistd.h"

void main(int argc, char **argv)
{
	if(argc < 2)
	{
		printf("Invocation: import netscape_dir\nE.G. import
c:\\netscape\\cache\n");
		return 1;	// error!
	};
	String s1(argv[1]);
	s1 = argv[1];	// netscape cache dir
	s1 += "\\FAT.DB";
	cout << s1 << "\n";
	struct stat st;
	if(stat(s1, &st))
	{
		perror(NULL);
		return 2;
	};
	printf("stat file size: %i\n", st.st_size);
	char* pChar = new char[st.st_size];
	cout << "Successfully allocated " << st.st_size << " bytes for
file.\n";
	int handle = open(s1, O_RDONLY);
	cout << "Handle: " << handle << ".\n";
	//FILE* pFile = fopen(s1, "r");
	// Get file size from handle
	fstat(handle, &st);
	printf("fstat file size: %i\n", st.st_size);
	int iRead;
	int iTotal = 0;
	char* pThisChar = pChar;
	while(iRead = read(handle, pThisChar, st.st_size))
	{
		pThisChar+=iRead;
		iTotal += iRead;
		cout << iRead << " bytes read for a total of " << iTotal << ".\n";
	};
	cout << iRead << ".\n";
	close(handle);
	cout << "File closed.\n";
/*	while(iRead = fread(pChar, 1, st.st_size, pFile))
	{
		cout << "Read " << iRead << ".\n";
	};
	cout << "EOF: " << feof(pFile) << ".\n";
	cout << "Read " << iRead << ".\n";
	cout << "EOF: " << feof(pFile) << ".\n";
	fclose(pFile);*/
};

Results:

H:\Matthew\CPP\CACHIMPT>a \c\netscape\cache
\c\netscape\cache\FAT.DB
stat file size: 692224
Successfully allocated 692224 bytes for file.
Handle: 3.
fstat file size: 692224
28720 bytes read for a total of 28720.
0.
File closed.

I have tried this for other FAT.DB's also: it returns different numbers
with the same pattern - file size several hundred k, read less than 32k,
do EOF.

-
For help on using this list (especially unsubscribing), send a message to
"[email-masked]" with one line of text: "help".