1 / 6

实验 2 编写自己的 cp 命令

实验 2 编写自己的 cp 命令. 计算机学院 潘薇 panwei117@qq.com. 实验内容. 编写一个 C 程序 mycp ,完成 cp 命令完成的功能,并在 Linux 下编译生成可执行文件,并进行测试。 要求: 使用库函数完成; 支持目标文件存在和不存在的情况; 支持源文件存在和不存在的情况; 如果操作失败返回失败原因; 对各种分支情况进行测试。. 问题 1 : cp 命令能做些什么?. cp 命令用于复制文件,典型的用法是 cp srcfile dstfile 如果 dstfile 不存在则创建;存在则覆盖;

kamran
Download Presentation

实验 2 编写自己的 cp 命令

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 实验2 编写自己的cp命令 计算机学院 潘薇 panwei117@qq.com

  2. 实验内容 • 编写一个C程序mycp,完成cp命令完成的功能,并在Linux下编译生成可执行文件,并进行测试。 • 要求: • 使用库函数完成; • 支持目标文件存在和不存在的情况; • 支持源文件存在和不存在的情况; • 如果操作失败返回失败原因; • 对各种分支情况进行测试。

  3. 问题1:cp命令能做些什么? • cp命令用于复制文件,典型的用法是 • cp srcfile dstfile • 如果dstfile不存在则创建;存在则覆盖; • 如果srcfile不存在则报错;

  4. 问题2:怎样实现文件读写? • 打开文件fopen • 读文件fread • 写文件fwrite • 关闭文件fclose • 文件是否读取到结束feof • 考虑文件大小的因素,读写可能不一定能一次完成,如何设计流程?

  5. 问题3:cp命令的流程 • 1打开源文件 • 2打开目标文件 • 3从源文件读取内容 • 4写入目标文件 • 5重复3-4,直至源文件结束 • 6关闭源文件 • 7关闭目标文件

  6. 问题4:哪些阶段可能出错? • 对所有可能出错的原因进行分析 • 对可能出错的地方进行处理,避免程序异常退出 • 编译通过后就各种情况进行测试 • mycp • mycp file1 • mycp file1 file2(file1不存在) • mycp file1 file2(file1存在file2不存在) • mycp file1 file2(file1存在file2存在)

More Related