Operating Files larger than 2 GB in Linux

Source: Internet
Author: User

The key point is to add the-d_file_offset_bits = 64 Macro during compilation,ProgramIn, replace fseek and ftell with fseeko and ftello respectively.

-----------------------------------------------------

Man fseeko

Description

TheFseeko ()AndFtello ()Functions are identicalFseek ()AndFtell ()(SeeFseek(3), respectively, doesn't thatOffsetArgumentFseeko ()And the return valueFtello ()Is of TypeOff_tInsteadLong.

On ubuntures bothOff_tAndLongAre 32-bit types, but compilation

# DEFINE _ file_offset_bits 64

Will turnOff_tInto a 64-bit type.

-----------------------------------------------------

Compilation Method:

#! /Bin/sh

Arm-mv5sft-linux-gnueabi-gcc-d_file_offset_bits = 64 main. C-o Test

Gcc-d_file_offset_bits = 64 main. C-o x86test

Source program:

# Include <stdio. h>

# Include <sys/STAT. h>

# Include <sys/types. h>

# Include <unistd. h>

# Include <assert. h>

# Define FILENAME "10g. IMG"

# Define readbufsize 100

Int main ()

{

Printf ("sizeof (size_t) % d, sizeof (off_t) % d \ n", sizeof (size_t), sizeof (off_t ));

Struct stat Buf;

If (STAT (filename, & BUF )! = 0)

{

Perror ("stat :");

Return-1;
}

Printf ("% LLD \ n", Buf. st_size );

File * stream = fopen (filename, "RW ");

If (! Stream)

{

Perror ("fopen :");

Return-1;
}

Char readbuf [readbufsize];

{

Size_t ret = fread (readbuf, readbufsize, 1, stream );

Printf ("fread: % u \ n", RET );

Assert (ret = 1 );

}

Printf ("% LLD \ n", ftello (Stream ));

If (fseeko (stream,-100, seek_end )! = 0)

{

Perror ("fseeko :");

Return-1;
}

{

Size_t ret = fread (readbuf, readbufsize, 1, stream );

Printf ("fread: % u \ n", RET );

Assert (ret = 1 );

}

Printf ("% LLD \ n", ftello (Stream ));

Return 0;

}

 

Running result:

/Mnt/diska/partition1 #./test

Sizeof (size_t) 4, sizeof (off_t) 8

10485760000

Fread: 1

100

Fread: 1

10485760000

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.