32-bit Linux System Operation Method larger than 2 GB File

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.