-
Lesson 1 - 最简单的C程序
-
Lesson 2 - 打印输出
-
Lesson 3 - 循环打印
-
Lesson 4 - 判断奇偶
-
Lesson 5 - 从1加到100求和
-
Lesson 6 - 乘法表
-
Lesson 7 - 求100以内的最大素数
-
Lesson 8 - 1到100有多少个9
-
Lesson 9 - 整型转字符串
-
Lesson 10 - 约瑟夫环
-
Lesson 11 - 求两个坐标点之间的距离
-
Lesson 12 - 判断机器存储是否小尾端
-
Lesson 13 - 对不起,你的车今天限行
-
Lesson 14 - 判断地图上某点是否有出路
-
Lesson 15 - 统计一个数二进制表示中1的个数
-
Lesson 16 - 字符串拷贝
-
Lesson 17 - 统计单词个数
-
Lesson 18 - 实现 printf
-
Lesson 19 - 命令解释器
-
Lesson 20 - 预处理器实现
-
Lesson 21 - 词法分析器实现
-
Lesson 22 - 猜数游戏
-
Lesson 23 - 五子棋
-
Lesson 24 - 超链接分析器
-
Lesson 25 - cp命令实现
-
Lesson 26 - ELF文件头分析器实现
-
Lesson 27 - 简单流处理器实现和正则表达式
-
Lesson 28 - 数学计算器实现
-
Lesson 29 - 数学计算器实现more命令实现
-
Lesson 30 - sort命令实现
-
Lesson 31 - ls -l命令实现
-
Lesson 32 - Bash项目
-
Lesson 33 - 动态数组实现
-
Lesson 34 - 约瑟夫环问题
-
Lesson 35 - 表达式求值问题
-
Lesson 36 - 广度优先解决迷宫问题
-
Lesson 37 - 词频统计器
-
Lesson 38 - 堆排序问题
-
Lesson 39 - 构造符号表
-
Lesson 40 - MyDictionary项目
-
Lesson 41 - BSearch 实现
-
Lesson 42 - QSort 实现
-
Lesson 43 - 深度优先解决迷宫问题
-
Lesson 44 - KMP 算法实现
-
Lesson 45 - 最长公共子序列(LCS)问题
-
Lesson 46 - Dijkstra 算法
-
Lesson 47 - Huffman Coding 算法
-
Lesson 48 - 地图导航项目
Lesson 25 - mycp (cp命令实现)
课程任务
- 完成 Linux 下的 cp 命令,实现文件的复制功能。
- 实现 cat 命令,查看复制后的文件内容。
cp 命令格式
NAME
cp -- copy files
SYNOPSIS
cp source_file target_file
copy
cat 命令格式
NAME
cat -- concatenate and print files
SYNOPSIS
cat filename
copy
预备知识
重要知识点
FILE struct
typedef struct __sFILE { unsigned char *_p; /* current position in (some) buffer */ int _r; /* read space left for getc() */ int _w; /* write space left for putc() */ short _flags; /* flags, below; this FILE is free if 0 */ short _file; /* fileno, if Unix descriptor, else -1 */ struct __sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */ int _lbfsize; /* 0 or -_bf._size, for inline putc */ /* operations */ void *_cookie; /* cookie passed to io functions */ int (*_close)(void *); int (*_read) (void *, char *, int); fpos_t (*_seek) (void *, fpos_t, int); int (*_write)(void *, const char *, int); /* separate buffer for long sequences of ungetc() */ struct __sbuf _ub; /* ungetc buffer */ struct __sFILEX *_extra; /* additions to FILE to not break ABI */ int _ur; /* saved _r when _r is counting ungetc data */ /* tricks to meet minimum requirements even when malloc() fails */ unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */ unsigned char _nbuf[1]; /* guarantee a getc() buffer */ /* separate buffer for fgetln() when line crosses buffer boundary */ struct __sbuf _lb; /* buffer for fgetln() */ /* Unix stdio files get aligned to block boundaries on fseek() */ int _blksize; /* stat.st_blksize (may be != _bf._size) */ fpos_t _offset; /* current lseek offset (see WARNING) */ } FILE;
copy
stdio/stdout/stderr
#define stdin __stdinp #define stdout __stdoutp #define stderr __stderrp extern FILE *__stdinp; extern FILE *__stdoutp; extern FILE *__stderrp;
copy
两个常用的宏定义 (stdio.h stddef.h)
#define EOF (-1) #define NULL ((void *)0)
copy
几个常见的 typedef
- size_t
- fpos_t
- va_list
常用 API
fopen(); 打开文件
fclose(); 关闭一个流。
feof(); 检测文件结束符
fread(); 从文件流读取数据
fwrite(); 将数据写至文件流
fprintf(); 格式化输出数据至文件
fscanf(); 格式化字符串输入
fflush(); 更新缓冲区
fgetc(); 由文件中读取一个字符
fgets(); 文件中读取一字符串
fputc(); 将一指定字符写入文件流中
fputs(); 将一指定的字符串写入文件内
fseek(); 移动文件流的读写位置
fsetpos(); 定位流上的文件指针
fgetpos(); 移动文件流的读写位置
ftell(); 取得文件流的读取位置
rewind(); 重设读取目录的位置为开头位置
fileno(); 获取文件描述符
ferror(); 检查流是否有错误
freopen(); 打开文件
remove(); 删除文件
rename(); 更改文件名称或位置
tmpfile(); 以wb+形式创建一个临时二进制文件
tmpnam(); 产生一个唯一的文件名
copy