views:

435

answers:

3

My previous Question is about raw data reading and writing, but a new problem arised, it seems there is no ending....

The question is: the parameters of the functions like lseek() or fseek() are all 4 bytes. If i want to move a span over 4G, that is imposible. I know in Win32, there is a function SetPointer(...,Hign, Low,....), this pointers can generate 64 byte pointers, which is what i want.

But if i want to create an app in Linux or Unix (create a file or directly write the raw drive sectors), How can I move to a pointer over 4G?

Thanx, Waiting for your replies...

+5  A: 

a 4 byte unsigned integer can represent a value up to 4294967295, which means if you want to move more than 4G, you need to use lseek64(). In addition, you can use fgetpos() and fsetpos() to change the position in the file.

Matt
How can i use the function `lseek64()` in linux and windows,in linux whether i need to include some libs when i am compiling it
Macroideal
lseek64() is a POSIX defined function, so it's only available on systems that have a POSIX API. It's available in glibc (since 2.1), however, for glibc you need to include the define #define _LARGEFILE64_SOURCE in order for it to be available.
Matt
just workout the differences by setting some macro and `#if` in your souce code and making a configuration header.
RageZ
+6  A: 

The offset parameter of lseek is of type off_t. In 32-bit compilation environments, this type defaults to a 32-bit signed integer - however, if you compile with this macro defined before all system includes:

#define _FILE_OFFSET_BITS 64

...then off_t will be a 64-bit signed type.

For fseek, the fseeko function is identical except that it uses the off_t type for the offset, which allows the above solution to work with it too.

caf
where is the original definition of _FILE_OFFSET_BITS in windows
Macroideal
@Macroideal: You, yourself, should #define _FILE_OFFSET_BITS, in your .c file, before any #includes.
Thomas Padron-McCarthy
Well, @caf gives the EXACT answer. To achieve it, you could add -D_LARGEFILE_SOURCE, -D_LARGEFILE64_SOURCE or -D_FILE_OFFSET_BITS=64 to CFLAGS/XXFLAGS of any Makefile or compiler parameters by default.
EffoStaff Effo
+1  A: 

On Windows, use _lseeki64(), on Linux, lseek64().

I recommend to use lseek64() on both systems by doing something like this:

#ifdef _WIN32
#include <io.h>
#define lseek64 _lseeki64
#else
#include <unistd.h>
#endif

That's all you need.

Matt Joiner