Access large files in C

I need to access a file larger than 2 GB using C. During one run of the program, a variable number of bytes and the location of the next saved position will be read from the file. The next time the program starts, the file position is read and several bytes are read, starting from this point.

The complication is that sometimes a file can be “compressed” by copying it to a new file, with the exception of all bytes already read (I think that copying is the only way to do this). The number of deleted bytes in this way will also be saved.

I need to know the current position of the file from the initial launch in order to synchronize with another file. This should be easy, because it is simple (current_offset + deleted_bytes).

The reason is that fseek only uses long int indexes that limit the 2gb file, and fsetpos uses the fpos_t structure to index the position, which is not a number and cannot be converted back and forth to one. I don't know how to use a long int index to position files, which would be an ideal solution.

What should I do?

+5
source share
2 answers

In windows you can use _lseeki64()to execute 64-bit queries.

For compatibility with linux, you can also add -D_FILE_OFFSET_BITS=64at compile time and then do it in one of your headers:

#ifdef __MINGW32__ // or whatever you use to find out you're compiling on windows
#define lseek _lseeki64
#endif

then use lseek()everywhere as usual. This works because windows ignore the flag _FILE_OFFSET_BITSand linux will not see the override lseek.

_fseeki64(), FILE*, 64 tell() ftell() (_telli64() _ftelli64()).

+6

-D_FILE_OFFSET_BITS=64, fopen, fseek, off_t .. 64- 2 . . Linux.

+1

All Articles