最新消息:

C 取得大于2G文件的大小

c admin 3059浏览 0评论

以下是源码(不解释):

#define __USE_LARGEFILE64
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int get_file_size( const char *filename )
{
  struct stat64 buf;

  if( stat64( filename, &buf ) < 0 ) {
    return 0;
  }
  return buf.st_size;
}

int main( int argc, char *argv[] )
{
  if( argc < 2 ) {
    printf( "Usage: ./filesize filenamen" );
    exit( 1 );
  }
  printf( "Filesize is %d bytesn", get_file_size( argv[1] ) );
  return 0;
}

gcc filesize.3.c -o filesize -std=c99 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -g

转载请注明:爱开源 » C 取得大于2G文件的大小

您必须 登录 才能发表评论!