命令 tail 用来打印文件中最后 n 行

命令 tail 用来打印文件中最后 n 行。

题目

命令 tail 用来打印文件中最后 n 行。
命令格式为:tail [−n] filename
其中:−n: 要打印的行数,当省略此参数时,n 的缺省值为 10。
filename: 文件名
例如,命令 tail -20 example.txt 表示打印文件 example.txt 的最后 20 行。
请用 C 语言实现该程序,该程序应具有一定的错误处理能力,例如能处理非法命令参数和非法文件名。

  • 提示一:使用命令行参数;
  • 提示二:可以使用下面的

C 库函数:·int atoi(char *s) 将数字串转换为相应整数;·fopen, fclose, printf, fprintf, exit; ·fgets(char *s, int n, FILE *fp) 从文件中读入一行;
·void *malloc(unsigned size), free 申请和释放内存;
·strlen 计算字符串长度;
·strcpy 将一个字符串拷贝到另一个字符串中。
除此之外,不允许使用其它库函数。

代码(源码点击这里)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/honeylife/article/details/101278908