473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Accessing large >2GB file succeeds fails with open/read

(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");
}

---------------------------------------------------------------
Rolf Schroedter, German Aerospace Center

Remove .nospam to reply: mailto:Ro*************@dlr.de.nospam
Nov 14 '05 #1
6 8432

Rolf Schroedter wrote:
(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().


This is fine from the C point of view -- we do not know open() and
read()... If you want to restrict yourself to standard C library
calls, you will have to go with fopen() and fread().
For large files, ftell()/fseek() are obviously not the right choice;
use fgetpos()/fsetpos() instead.

[snip]
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #2
Rolf Schroedter wrote:
(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
None of those is a standard C library call.
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().
The standard functions work and the non-standard ones don't.
Not surprising..

For help with the non-standard functions, you should ask in
a system-specific group.
i = 0;
while( ! feof(f) ) {
result = fread(buf, 1, sizeof(buf), f);
printf("%d: read %d bytes\n", ++i, result);
}


Incorrect use of feof. (Read its man page, or the newsgroup FAQ).

Nov 14 '05 #3
Old Wolf wrote:
Rolf Schroedter wrote:

i = 0;
while( ! feof(f) ) {
result = fread(buf, 1, sizeof(buf), f);
printf("%d: read %d bytes\n", ++i, result);
}


Incorrect use of feof. (Read its man page, or the newsgroup FAQ).


Strictly speaking, the sample doesn't show incorrect use of
feof(), in the way of resultant undefined behaviour. The problem
here is that the last loop will show "##: read 0 bytes", and
there is an infinite loop if a read error occurs.

[I found it sad that M$'s sample of feof shows just such a loop.

http://msdn.microsoft.com/library/de.../erlrffeof.asp

Unfortunately, the construct is correct within the sample, but is
_highly_ misleading.]

In a less trivial loop the potential for more serious error is
greatly increased.

--
Peter

Nov 14 '05 #4
Peter Nilsson wrote:
Old Wolf wrote:
Rolf Schroedter wrote:

i = 0;
while( ! feof(f) ) {
result = fread(buf, 1, sizeof(buf), f);
printf("%d: read %d bytes\n", ++i, result);
}


Incorrect use of feof. (Read its man page, or the newsgroup FAQ).


Strictly speaking, the sample doesn't show incorrect use of
feof(), in the way of resultant undefined behaviour. The problem
here is that the last loop will show "##: read 0 bytes", and
there is an infinite loop if a read error occurs.


Right. There is another problem which I didn't notice at first:
fread() returns a size_t but the OP assigns it to an int. If
the size of the read was > 2^31 bytes , and int is 32 bit,
then we have implementation-defined behaviour (possibly generating
an implementation-defined signal, causing the program to abort).

Nov 14 '05 #5
Thanks for your remarks.
But please be aware that this is a quick-and-dirty example
kept small to illustrate the problem.

If there was only this sample I would concludethat
open/read doesn't work for MS-Windows.
But I have a larger program using FLTK, where
heavy low-level read/_lseeki64 work fine on files >2GB...

I see that probably c.l.c is not the right place to ask.
I would need something like comp.lang.c.cross-platform

---------------------------------------------------------------
Rolf Schroedter, German Aerospace Center

Remove .nospam to reply: mailto:Ro*************@dlr.de.nospam
Nov 14 '05 #6
[FUT: c.o.m-w.p.win32]

Rolf Schroedter wrote:
Thanks for your remarks.
But please be aware that this is a quick-and-dirty example
kept small to illustrate the problem.

If there was only this sample I would concludethat
open/read doesn't work for MS-Windows.
But I have a larger program using FLTK, where
heavy low-level read/_lseeki64 work fine on files >2GB...

I see that probably c.l.c is not the right place to ask.
I would need something like comp.lang.c.cross-platform


Eh, no. As others have pointed out, open/read/write etc are not part of
any C standard at all, they are the low-level I/O API in the Posix
standards. Win32 API are not Posix compatible and the CRT libraries in
Windows that run under Win32 only support a small sub-set of Posix API
that is emulated, open/read/write etc are translated to
CreateFile/ReadFile/WriteFile etc, they are not low-level APIs in the
Win32 subsystem.

So if you really want low-level I/O and not streaming I/O I would
suggest you to use the low-level API in each operating system, i.e.
open/read/write in Linux and other Posix systems, including Cygwin or
Interix under Windows and to use CreateFile/ReadFile/WriteFile under Win32.

--
Olof Lagerkvist sm6xmk
ICQ: 724451 @ssa.se
Web: http://here.is/olof

Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ohaya | last post by:
Hi, I'm a real newbie, but have been asked to try to fix a problem in one of our JSP pages that is suppose to read in a text file and display it. From my testing thus far, it appears this page...
1
by: DJTB | last post by:
zodb-dev@zope.org] Hi, I'm having problems storing large amounts of objects in a ZODB. After committing changes to the database, elements are not cleared from memory. Since the number of...
3
by: Charlie | last post by:
Dear all, I am currently writting a trace analyzer in C++. It always fails to open a very large input file (3.7Gb). I tried on a simple program, same thing happens:...
2
by: TOI DAY | last post by:
I have a large file, I want to be to open the first file in a stream, read a chunk (say 1024 bytes) and stick it in a byte array, open the second file in a different stream, read a chuck (the same...
5
by: Daniel Corbett | last post by:
I am trying to save a file dynamically created in a webpage. I get the following headers, but cannot figure out how to save the attachment. I am basically trying to replicate what internet...
3
by: Kenneth H. Brannigan | last post by:
Hello, I have a FileSystemWatcher looking at a particular directory. When I copy a large file (750 MB) to this directory I receive multiple Change events. I am fine with this but when I call...
10
by: DBC User | last post by:
Hi All, Quick question, in my program I need to download a large file (100MB) file. I would like to know how can I do this in segments so that if the download fails, I will able to download from...
2
by: Sean Davis | last post by:
This should be a relatively simple problem, but I haven't quite got the idea of how to go about it. I have a VERY large file that I would like to load a line at a time, do some manipulations on...
4
by: coldy | last post by:
Hi, I have a large txt file (1GB) which I need to break into smaller files based on the contents of a column in the file. The values in the column of intrest starts low, then increases, then...
2
by: robert | last post by:
Somebody who uses my app gets a error : os.stat('/path/filename') OSError: Value too large for defined data type: '/path/filename' on a big file >4GB ( Python 2.4.4 / Linux )
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.