当前位置:   article > 正文

Linux应用编程之文件IO open 学习之O_TRUNC 标志_o_trnuc

o_trnuc

O_TRUNC含义:

        当文件存在时,将文件内容长度制定为0,与文件写入模式一起使用,可以达到覆盖写入的作用!

一下面这段文件复制的C代码为例:

  1. //copy.c 复制文件
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include<stdio.h>
  6. #include <unistd.h>
  7. int main(int argc,char** arg)
  8. {
  9. int file_old;
  10. int file_new;
  11. char rbuff[1024];
  12. int len;
  13. //argument num isn't enougth
  14. if(argc<3)
  15. {
  16. printf("usage: %s <old_filepath> <new_filepath>\n",arg[0]);
  17. return -1;
  18. }
  19. // step 1 open new file if successful will return a file ID("fd") else return -1
  20. file_old = open(arg[1],O_RDONLY);
  21. if(file_old<0)
  22. {
  23. printf("can't open file : %s\n",arg[1]);
  24. return -1;
  25. }
  26. //step 2 create or open new file
  27. //if want to write or read new file ,we need config its access mode
  28. file_new = open(arg[2],O_RDWR|O_CREAT|O_TRUNC,00664);//00 XXX与chmod xxx的权限类似
  29. if(file_new<0)
  30. {
  31. printf("can't create or open new file : %s\n",arg[2]);
  32. }
  33. //step 3 read old file and write to new file
  34. while((len=read(file_old,rbuff,1024))>0)//read old file
  35. {
  36. if(write(file_new,rbuff,len)!=len)
  37. {
  38. printf("can not write %s\n",arg[2]);
  39. } //write to new file
  40. }
  41. //step 4 close file ,use " open" must use "close"
  42. close(file_old);
  43. close(file_new);
  44. return 0;
  45. }

将代码放到Linux系统下的文件目录,在终端通过gcc编译指令生成可执行应用文件copy:

gcc -o copy copy.c

随后执行:

./copy copy.c copy2.c

此时,copy.c的内容将被复制到copy2.c当中,copy2.c的内容如下:

  1. //copy from copy.c
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include<stdio.h>
  6. #include <unistd.h>
  7. int main(int argc,char** arg)
  8. {
  9. int file_old;
  10. int file_new;
  11. char rbuff[1024];
  12. int len;
  13. //argument num isn't enougth
  14. if(argc<3)
  15. {
  16. printf("usage: %s <old_filepath> <new_filepath>\n",arg[0]);
  17. return -1;
  18. }
  19. // step 1 open new file if successful will return a file ID("fd") else return -1
  20. file_old = open(arg[1],O_RDONLY);
  21. if(file_old<0)
  22. {
  23. printf("can't open file : %s\n",arg[1]);
  24. return -1;
  25. }
  26. //step 2 create or open new file
  27. //if want to write or read new file ,we need config its access mode
  28. file_new = open(arg[2],O_RDWR|O_CREAT|O_TRUNC,00664);//00 XXX与chmod xxx的权限类似
  29. if(file_new<0)
  30. {
  31. printf("can't create or open new file : %s\n",arg[2]);
  32. }
  33. //step 3 read old file and write to new file
  34. while((len=read(file_old,rbuff,1024))>0)//read old file
  35. {
  36. if(write(file_new,rbuff,len)!=len)
  37. {
  38. printf("can not write %s\n",arg[2]);
  39. } //write to new file
  40. }
  41. //step 4 close file ,use " open" must use "close"
  42. close(file_old);
  43. close(file_new);
  44. return 0;
  45. }

假设我们此时有一个1.txt文件,其内容为:

HELLO WORLD,I AM 1.txt

再次执行:

./copy 1.txt copy2.c

copy2.c的内容将被覆盖为

Hello World,I am 1.txt

 若无添加O_TRUNC选项,则只会覆盖一部分文件内容,此时,copy2.c的内容如下:

 总结:

        O_TRUNC可以在打开文件的时候,将文件的内容长度设置为0,并于文件写入一同使用,达到覆盖写入的效果。

题外:若要实现追加写入,则使用选项O_APPEND

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/186465
推荐阅读
相关标签
  

闽ICP备14008679号