C
cylin
Dear all,
I write a simple program to test file open,read,close.
It worked fine in windows and unix, even redhat 8.0. And the compilers are
g++,kylix,vc++6,bcb++6.
The problem is when using fread(or C++ istream::read() ) to read larger
than about 20,000 file position.
Then it'll cost moemry unlimitedly.
Does anyone know why? Thanks your help.
Here is my C code. Almost the same as C++ istream::read().
----------------------------------------------------------------------------
-------------------------------------------------
#include <cstdio>
#include <iostream>
using namespace std;
#define BYTES_PER_LOAD 4096
int main()
{
FILE *l_pFile=fopen("/mnt/windows/gds/demo.gds","rb");
if (l_pFile==NULL)
{
cout << "File open failure." << endl;
return 0;
}
unsigned char l_ucharBuf[BYTES_PER_LOAD];
fread(l_ucharBuf,sizeof(unsigned char),BYTES_PER_LOAD,l_pFile);
if (!feof(l_pFile)) { // file size > BYTES_PER_LOAD= 4096 Bytes
do {
fread(l_ucharBuf,sizeof(unsigned char),BYTES_PER_LOAD,l_pFile);
cout << "Current file position : " << ftell(l_pFile) << endl;
}while (!feof(l_pFile));
}
else {
/*... */
}
fclose(l_pFile);
return 0;
}
I write a simple program to test file open,read,close.
It worked fine in windows and unix, even redhat 8.0. And the compilers are
g++,kylix,vc++6,bcb++6.
The problem is when using fread(or C++ istream::read() ) to read larger
than about 20,000 file position.
Then it'll cost moemry unlimitedly.
Does anyone know why? Thanks your help.
Here is my C code. Almost the same as C++ istream::read().
----------------------------------------------------------------------------
-------------------------------------------------
#include <cstdio>
#include <iostream>
using namespace std;
#define BYTES_PER_LOAD 4096
int main()
{
FILE *l_pFile=fopen("/mnt/windows/gds/demo.gds","rb");
if (l_pFile==NULL)
{
cout << "File open failure." << endl;
return 0;
}
unsigned char l_ucharBuf[BYTES_PER_LOAD];
fread(l_ucharBuf,sizeof(unsigned char),BYTES_PER_LOAD,l_pFile);
if (!feof(l_pFile)) { // file size > BYTES_PER_LOAD= 4096 Bytes
do {
fread(l_ucharBuf,sizeof(unsigned char),BYTES_PER_LOAD,l_pFile);
cout << "Current file position : " << ftell(l_pFile) << endl;
}while (!feof(l_pFile));
}
else {
/*... */
}
fclose(l_pFile);
return 0;
}