p4-10.c

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

C
30
字号
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>#include <sys/stat.h>#include "err_exit.h"int main(int argc, char*argv[]){      int length;    int fd;    /* 检查参数的正确性 */    if (argc != 3){       printf("usage: a.out <filename> <integer unmber>\n");       exit(1);    }    /* 读取参数并打开文件 */    length = atoi(argv[2]);    if ((fd = open(argv[1],O_RDWR)) < 0)       err_exit("open() call failed");    /* 按第二参数指定字节截断文件 */    printf ("truncate %s to %d characaters\n", argv[1], length);    if ( ftruncate(fd, length) < 0 )        err_exit("truncate() call failed");    printf ("truncate() call successful\n");    /* 在截断后的文件尾部写入尾部标识 */    lseek(fd,(long)0, SEEK_END);    write(fd, "#here is tail.",14);    close(fd);}

⌨️ 快捷键说明

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