p2-7.c
来自「UNIX程序设计教程」· C语言 代码 · 共 48 行
C
48 行
#include <stdio.h>#include <stdlib.h>char buf[132];int main(int argc, char *argv[]){ FILE *fd; fpos_t pos; if (argc !=2){ fprintf(stderr,"Usage: %s mode\n",argv[0]); exit(EXIT_FAILURE); } /* 打开一个文件 */ if(argv[1][0]!='a'){ /* 打开该文件写数据*/ if ((fd = fopen("test_file","w+")) == NULL){ perror("fopen faild"); exit(EXIT_FAILURE); } } else{ /* 打开该文件添加数据*/ if ((fd = fopen("test_file","a+")) == NULL){ perror("fopen faild"); exit(EXIT_FAILURE); } } /* 写入二行数据 */ fputs("0123456789",fd); fputs("ABCDEFGHIJ",fd); /* 查看当前文件尾位置 */ fseek(fd,0,SEEK_END); fgetpos(fd,&pos); printf("current file end position is %ld\n",pos); /* 定位至文件尾之后30字节 */ fseek(fd,30,SEEK_END); /* 查看当前文件尾位置 */ fgetpos(fd,&pos); printf("Now we call fseek(fd,30,SEEK_END)\n"); printf("current file position is %ld\n",pos); /* 写入数据 */ fputs("abcdefg",fd); printf("Now we write %c%s%c\n",'\"',"abcdefg",'\"'); /* 查看当前文件尾位置 */ fgetpos(fd,&pos); printf("current position of file end is %ld\n",pos); fclose(fd);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?