R
Rolf Schroedter
(Sorry for cross-posting).
I need to access large files > 2GByte (Linux, WinXP/NTFS)
using the standard C-library calls.
Till today I thought I know how to do it, namely for
Win32:
Use open(), read(), _itelli64(), _lseeki64() with type __int64
Linux/Cygwin:
#define _FILE_OFFSET_BITS 64
Use open(), read(), lseek() with type off_t
My environment is
- WindowsXP, SP2 + latest updates
- VC 6.0 + SP6
- NTFS
Now the following little program using sequential read's
fails for me when using low-level File I/O functions
open(), read()
but succeeds with stream I/O
fopen(), fread().
What drives me crazy is that for a large project - a GUI using the fltk
library - I'm doing heavy open(),read() and _lseeki64() skipping through
the same large video files without any problem.
Any ideas are highly appreciated.
Here is the small sample program. Note that the seek() is only
to accelerate the test, it can be removed without changing the result.
Use it with any >2GB file:
/**********************************************************************
* open/read fails, fopen/fread succeeds for files >2GB
***********************************************************************/
//#define _INTEGRAL_MAX_BITS 64
#include <io.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
/**********************************************************************
* Main
***********************************************************************/
int main(int argc, char **argv)
{
int i, result;
char buf[1000000];
/*
* Parse command line parameters
*/
if( (argc < 2) || (argc % 2 != 0) ) {
printf("call: %s filename\n", argv[0]);
exit(1);
}
#if 0
{
int fh;
fh = open(argv[1], O_RDONLY | O_BINARY);
if( fh < 0 ) {
printf("Unable to open FITS '%s'\n", argv[1]);
perror("");
exit(1);
}
lseek(fh, 2000*1000000, SEEK_SET);
i = 0;
while( ! eof(fh) ) {
result = read(fh, buf, sizeof(buf));
printf("%d: read %d bytes\n", ++i, result);
}
}
#else
{
FILE *f;
f = fopen(argv[1], "rb");
if( f == NULL ) {
printf("Unable to open FITS '%s'\n", argv[1]);
perror("");
exit(1);
}
fseek(f, 2000*1000000, SEEK_SET);
i = 0;
while( ! feof(f) ) {
result = fread(buf, 1, sizeof(buf), f);
printf("%d: read %d bytes\n", ++i, result);
}
}
#endif
printf("<eof>\n");
}
I need to access large files > 2GByte (Linux, WinXP/NTFS)
using the standard C-library calls.
Till today I thought I know how to do it, namely for
Win32:
Use open(), read(), _itelli64(), _lseeki64() with type __int64
Linux/Cygwin:
#define _FILE_OFFSET_BITS 64
Use open(), read(), lseek() with type off_t
My environment is
- WindowsXP, SP2 + latest updates
- VC 6.0 + SP6
- NTFS
Now the following little program using sequential read's
fails for me when using low-level File I/O functions
open(), read()
but succeeds with stream I/O
fopen(), fread().
What drives me crazy is that for a large project - a GUI using the fltk
library - I'm doing heavy open(),read() and _lseeki64() skipping through
the same large video files without any problem.
Any ideas are highly appreciated.
Here is the small sample program. Note that the seek() is only
to accelerate the test, it can be removed without changing the result.
Use it with any >2GB file:
/**********************************************************************
* open/read fails, fopen/fread succeeds for files >2GB
***********************************************************************/
//#define _INTEGRAL_MAX_BITS 64
#include <io.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
/**********************************************************************
* Main
***********************************************************************/
int main(int argc, char **argv)
{
int i, result;
char buf[1000000];
/*
* Parse command line parameters
*/
if( (argc < 2) || (argc % 2 != 0) ) {
printf("call: %s filename\n", argv[0]);
exit(1);
}
#if 0
{
int fh;
fh = open(argv[1], O_RDONLY | O_BINARY);
if( fh < 0 ) {
printf("Unable to open FITS '%s'\n", argv[1]);
perror("");
exit(1);
}
lseek(fh, 2000*1000000, SEEK_SET);
i = 0;
while( ! eof(fh) ) {
result = read(fh, buf, sizeof(buf));
printf("%d: read %d bytes\n", ++i, result);
}
}
#else
{
FILE *f;
f = fopen(argv[1], "rb");
if( f == NULL ) {
printf("Unable to open FITS '%s'\n", argv[1]);
perror("");
exit(1);
}
fseek(f, 2000*1000000, SEEK_SET);
i = 0;
while( ! feof(f) ) {
result = fread(buf, 1, sizeof(buf), f);
printf("%d: read %d bytes\n", ++i, result);
}
}
#endif
printf("<eof>\n");
}