zoukankan      html  css  js  c++  java
  • 问题:Linux ftruncate64() 修改文件大小,执行无错误,但只能指定文件到1GB大小。

    测试代码:

     1 #define _LARGEFILE_SOURCE
     2 #define _LARGEFILE64_SOURCE
     3 #define _FILE_OFFSET_BITS 64
     4 
     5 #include <sys/types.h>
     6 #include <sys/stst.h>
     7 #include <fcntl.h>
     8 #include <unistd.h>
     9 
    10 int main(){
    11    int fd;
    12    __off64_t offset;
    13    fd = open("test", O_RDWR | O_CREAT | O_LARDEFILE,0666);
    14    offset = 25*65536*65536; //100GB
    15    int err = ftruncate64(fd,offset);
    16    if(err < 0){
    17      printf("ftruncate err
    ");
    18 
    19   }
    20   
    21   struct stat64 filestat;
    22   fstat64(fd, &filestat);
    23   printf("file size: %ld
    ",filestat.st_size);
    24   //打印__off64_t 大小:
    printf("__off64_t is %d bytes ",sizeof(__off64_t)); 25 26 return 0; 27 28 }

    使用gcc - D_FILE_OFFSET_BITS=64 -D_LARGE_FILE  test.c; 编译警告:iteger overflow  "offset = 25*65536*65536;"。运行sudo ./a.out, ftruncate64()返回值为0, __0ff64_t 大小为8字节,但是test大小只有1GB.

    有没有大牛知道原因?

    PS: 在终端下,执行truncate -s 100GB test  或者 代码中执行 system(" truncate -s 100GB test ")后,都能将test大小设置为100GB.

  • 相关阅读:
    Shell与if相关参数
    Linux盘符漂移问题
    shell脚本,每5个字符之间插入"|",行末不插入“|”
    paste:linux合并两个文件中的列(左右合并)
    关于bc 的scale .
    RxJS与观察者模式
    什么是虚拟DOM
    JS设计模式
    JS自定义事件
    原生js实现拖拽功能
  • 原文地址:https://www.cnblogs.com/coding-for-a-better-world/p/13577312.html
Copyright © 2011-2022 走看看