p2-10.c

来自「UNIX程序设计教程」· C语言 代码 · 共 28 行

C
28
字号
#include <stdio.h>#include <stdlib.h>#include "err_exit.h"char buf[132];int get_line(char *,int size, FILE *);     /* 程序2-8 */int main(void){    FILE *fd;    /* 创建一个文件并写入二行数据 */    if ((fd = fopen("test_file","w+")) == NULL)        err_exit("fopen faild");    fprintf(fd,"this is first line.\n");    fprintf(fd,"this is second line.\n");    fclose(fd);    /* 再次打开该文件读写数据*/    if ((fd = fopen("test_file","r+")) == NULL)        err_exit("fopen faild");    /* 读一行数据 */    get_line(buf, sizeof(buf), fd);    /* 由读变为写 */    /* fflush(fd); */     fprintf(fd,"I hope this line become the new second line.\n");    /* 由写变为读 */    /* fflush(fd); */      get_line(buf, sizeof(buf), fd);    fclose(fd);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?