Linux C / C ++ to read and write large files more than 2G Notes

background

Big file in the project incremental write, encountered a problem:

fopen : Value too large for defined data type.

According to this prompt access to relevant information is displayed:

  • Toolchain too old: Hass tool chain I currently can not find a replacement method
  • Inde file system is 64-bit: View the cat /proc/fs/{文件系统类型}/{设备名}/optionsfound everything normal

Changed the thinking and look directly according to the needs: Linux C read and write large files.

For information learned

Linux environment by default, read and write more than 2G file returns an error.

Defined as macros can break this limit, valid for both read / write and fread / fwrite.

Note that it must be defined before all the header files.

#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64

fseek issue of access to the file size larger than 4G
reproduced feng plum Last post on 2015-06-23 23:30:50 reading number 3154 collection
launched

Recently encountered problems in obtaining the file size, because it everywhere to find the problem, and finally I realized that there is a problem at the time to obtain the file size. Closer to home:

Previously acquired file size is the time to do so:

fseek(fp,0,SEEK_END);

_length=ftell(fp);

fseek( fp, 0, SEEK_SET);

Under normal circumstances like this is no problem, but it appears when the reading is greater than 3,4G misread the situation, either -1, either _length maximum data types, in short, is wrong.

The solution is:

fpos_t pos;
fseek(fp,0,SEEK_END);
fgetpos(fp,&pos);
fseek( fp, 0, SEEK_SET);

pos is the size of the file, and specially checked fpos_t, this data type is defined in stdio.h file inside

        typedef __int64 fpos_t;

It is such a meaning, like this would not add any additional library, or some say in what api or windows can be solved!

Mmap-based reading and writing large files

Guess you like

Origin www.cnblogs.com/schips/p/12456653.html