📄 p4-10.c
字号:
#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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -