1. Get the file size
1. Use the stat function to obtain the file size.
Let's take a look at the struct structure. The file size is defined as follows:
# Ifndef _ use_file_offset64
_ Off_t st_size;/* size of file, in bytes .*/
# Else
_ Off64_t st_size;/* size of file, in bytes .*/
# Endif
Generally, we use _ off_t, that is, long int (4 bytes,-2147438648 ~ + 2141438647), while _ off64_t is long int (8 bytes,-9223372036854775808 ~ + 9223372036854775807 ).
We try to use stat to read a 4.2g file and check the file size. An error occurred while running. "Permission denied". The permission is rejected.
It seems that direct reading is not acceptable. We added the-d_file_offset_bits = 64 option during compilation and tried again. The file size was obtained normally this time.
To sum up, if you want to obtain the size of a file larger than 2 GB, you need to add the compilation option during compilation.
2. Use the awk script to obtain
Char buff [16];
File * fp = popen ("ls-L/mnt/HGFS/share/worspace/cis7.5/src/test/big | awk '{print $5 }'", "R ");
Fgets (buff, sizeof (buff), FP );
This method requires support from awk scripts and is not recommended.
Ii. Read and Write files
Under normal circumstances, when the number of reading and writing times reaches 2 GB for files larger than 2 GB, the system automatically exits and reports"The file size exceeds the limit..
There are two ways to solve this problem.
1. Add compilation options
-D_file_offset_bits = 64
2. Add the o_largefile bit to the second parameter of open.
The above two methods have passed the test, and there is no problem in reading and writing files larger than 4G.
To operate large files, I personally think the most reliable thing is to add the compilation macro, which is reliable. Others are the same as before.