diff --git a/README.md b/README.md index cf8c40a..636b999 100644 --- a/README.md +++ b/README.md @@ -19,3 +19,5 @@ #### day12: 字符串处理函数,const,结构体 #### day13: 结构体,位域,共用体,枚举,链表操作 + +#### day14: 文件读写操作 diff --git a/day14/a.out b/day14/a.out deleted file mode 100755 index a245c7d..0000000 Binary files a/day14/a.out and /dev/null differ diff --git a/day14/d10.c b/day14/d10.c new file mode 100644 index 0000000..1938d25 --- /dev/null +++ b/day14/d10.c @@ -0,0 +1,36 @@ +#include +#include + +// 实现读取 filepath 文件的前 top_n 行数据 +void *read_top_line(const char *filepath, int top_n) +{ + FILE *f = fopen(filepath, "r"); + if (NULL == f) + { + perror("fopen"); + return NULL; + } + + char(*lines)[100] = calloc(top_n, 100); + for (int i = 0; i < top_n; i++) + { + char *flag = fgets(lines[i], 100, f); + if (flag == NULL) + break; + } + fclose(f); + return lines; +} + +int main() +{ + int n = 5; + char(*p)[100] = read_top_line("d10.txt", n); + for (int i = 0; i < n; i++) + { + printf("第 %d 行: %s", i + 1, *(p + i)); + } + free(p); + + return 0; +} \ No newline at end of file diff --git a/day14/d10.txt b/day14/d10.txt new file mode 100644 index 0000000..425b436 --- /dev/null +++ b/day14/d10.txt @@ -0,0 +1,6 @@ +hello,world +张三 +爱唱歌 +lihai +hahaha +手机 \ No newline at end of file diff --git a/day14/d11.c b/day14/d11.c new file mode 100644 index 0000000..2d970e1 --- /dev/null +++ b/day14/d11.c @@ -0,0 +1,22 @@ +#include + +// 文件复制 +int main() +{ + FILE *aF = fopen("d11.txt", "r"); + if (NULL == aF) + { + perror("fopen"); + return 1; + } + + FILE *bF = fopen("d11_cpy.txt", "w"); + int c; + while ((c = fgetc(aF)) != EOF) + { + fputc(c, bF); + } + fclose(aF); + fclose(bF); + return 0; +} \ No newline at end of file diff --git a/day14/d11.txt b/day14/d11.txt new file mode 100644 index 0000000..425b436 --- /dev/null +++ b/day14/d11.txt @@ -0,0 +1,6 @@ +hello,world +张三 +爱唱歌 +lihai +hahaha +手机 \ No newline at end of file diff --git a/day14/d11_cpy.txt b/day14/d11_cpy.txt new file mode 100644 index 0000000..425b436 --- /dev/null +++ b/day14/d11_cpy.txt @@ -0,0 +1,6 @@ +hello,world +张三 +爱唱歌 +lihai +hahaha +手机 \ No newline at end of file diff --git a/day14/d12.c b/day14/d12.c new file mode 100644 index 0000000..a13d110 --- /dev/null +++ b/day14/d12.c @@ -0,0 +1,22 @@ +#include + +// 二进制文件复制 +int main() +{ + FILE *aF = fopen("d12.jpg", "rb"); + if (NULL == aF) + { + perror("fopen"); + return 1; + } + + FILE *bF = fopen("d12_cpy.jpg", "wb"); + int c; + while ((c = fgetc(aF)) != EOF) + { + fputc(c, bF); + } + fclose(aF); + fclose(bF); + return 0; +} \ No newline at end of file diff --git a/day14/d12.jpg b/day14/d12.jpg new file mode 100755 index 0000000..cfe825e Binary files /dev/null and b/day14/d12.jpg differ diff --git a/day14/d12_cpy.jpg b/day14/d12_cpy.jpg new file mode 100644 index 0000000..cfe825e Binary files /dev/null and b/day14/d12_cpy.jpg differ diff --git a/day14/d13.c b/day14/d13.c new file mode 100644 index 0000000..69b7ba5 --- /dev/null +++ b/day14/d13.c @@ -0,0 +1,36 @@ +// 自定义二进制文件写入 +#include +#include +#include + +typedef struct +{ + int sid; + char name[32]; + int age; + char sex[4]; +} STU; + +int main() +{ + STU s1 = {1, "disen", 20, "男"}; + STU s2 = {2, "jack", 18, "女"}; + STU *s3 = malloc(sizeof(STU)); + s3->sid = 3; + strcpy(s3->name, "lily"); + s3->age = 19; + strcpy(s3->sex, "女"); + + FILE *f = fopen("d13.txt", "wb"); + if (NULL == f) + { + return 1; + } + + fwrite(&s1, sizeof(STU), 1, f); + fwrite(&s2, sizeof(STU), 1, f); + fwrite(&s3, sizeof(STU), 1, f); + fclose(f); + + return 0; +} \ No newline at end of file diff --git a/day14/d13.txt b/day14/d13.txt new file mode 100644 index 0000000..852165b Binary files /dev/null and b/day14/d13.txt differ diff --git a/day14/d14.c b/day14/d14.c new file mode 100644 index 0000000..7e705f3 --- /dev/null +++ b/day14/d14.c @@ -0,0 +1,28 @@ +// 自定义二进制文件读取 +#include +#include + +typedef struct +{ + int sid; + char name[32]; + int age; + char sex[4]; +} STU; + +int main() +{ + FILE *f = fopen("d13.txt", "rb"); + if (NULL == f) + { + return 1; + } + STU *s = malloc(sizeof(STU)); + fread(s, sizeof(STU), 1, f); + printf("%d %s %d %s\n", s->sid, s->name, s->age, s->sex); + free(s); + + fclose(f); + + return 0; +} \ No newline at end of file diff --git a/day14/d15.c b/day14/d15.c new file mode 100644 index 0000000..2135984 --- /dev/null +++ b/day14/d15.c @@ -0,0 +1,42 @@ +// 自定义二进制文件读取 +#include +#include + +typedef struct +{ + int sid; + char name[32]; + int age; + char sex[4]; +} STU; + +int main() +{ + FILE *f = fopen("d13.txt", "rb"); + if (NULL == f) + { + return 1; + } + + // 让光标移动到文件尾部 + fseek(f, 0, SEEK_END); + long file_size = ftell(f); // 获取文件大小 + printf("文件大小: %ld B\n", file_size); + int stu_num = file_size / sizeof(STU); + printf("学生数目: %d 位\n", stu_num); + + fseek(f, 0, SEEK_SET); // 重置光标位置到头 + for (int i = 0; i < stu_num; i++) + { + STU *s = calloc(1, sizeof(STU)); + + int fx = fread(s, sizeof(STU), 1, f); + if (fx != 0) + printf("%d %s %d %s\n", s->sid, s->name, s->age, s->sex); + free(s); + } + + fclose(f); + + return 0; +} \ No newline at end of file diff --git a/day14/d2.c b/day14/d2.c new file mode 100644 index 0000000..1b6f29a --- /dev/null +++ b/day14/d2.c @@ -0,0 +1,15 @@ +// 行缓冲区 +// 写数据时遇到换行符,则刷新缓冲 +// 如果行缓冲不刷新时,则不会输出结果。 +// 因为输出的内容没有遇到换行符(Linux `\n`), Window(`\r\n`)。 + +#include + +int main() +{ + printf("Hello World"); // 缓冲区未写满,不会刷新缓冲区,不会输出 + while (1) + ; + + return 0; // 缓冲未写满,不会刷新缓冲区,不会退出程序 +} \ No newline at end of file diff --git a/day14/d3.c b/day14/d3.c new file mode 100644 index 0000000..20e8dc4 --- /dev/null +++ b/day14/d3.c @@ -0,0 +1,15 @@ +#include + +int main() +{ + int i = 0; + // 缓冲区一般为 4KB,这里写入 32B,不会刷新缓冲区 + char m[33] = "disenjackydisenjackydisenjackyab"; + + printf("%s", m); + fflush(stdout); // 刷新缓冲区,目的是为了让缓冲区中的数据输出到屏幕上 + while (1) + ; // 用来观察程序退出时是否会刷新缓冲区,阻塞程序 + + return 0; +} \ No newline at end of file diff --git a/day14/d4.c b/day14/d4.c new file mode 100644 index 0000000..671f7ca --- /dev/null +++ b/day14/d4.c @@ -0,0 +1,14 @@ +// 程序正常退出也会刷新缓冲区,显示输出 +#include + +int main() +{ + int i = 0; + // 缓冲区一般为 4KB,这里写入 32B,不会刷新缓冲区 + char m[33] = "disenjackydisenjackydisenjackyab"; + + printf("%s", m); + ; + + return 0; +} \ No newline at end of file diff --git a/day14/d5.c b/day14/d5.c new file mode 100644 index 0000000..4b2a850 --- /dev/null +++ b/day14/d5.c @@ -0,0 +1,18 @@ +// 读写文本文件 +#include +#include + +int main() +{ + char *f = (char *)malloc(100); + FILE *fp = fopen("d5.txt", "w"); + if (fp == NULL) + { + perror("fopen"); + exit(1); + } + fputs("disen", fp); + fclose(fp); + + return 0; +} \ No newline at end of file diff --git a/day14/d5.txt b/day14/d5.txt new file mode 100644 index 0000000..3da68f7 --- /dev/null +++ b/day14/d5.txt @@ -0,0 +1 @@ +disen \ No newline at end of file diff --git a/day14/d6.c b/day14/d6.c new file mode 100644 index 0000000..3b4c6b5 --- /dev/null +++ b/day14/d6.c @@ -0,0 +1,13 @@ +#include +#include + +int main() +{ + FILE *f = fopen("d6.txt", "a"); + // char content[20] = "hello disen"; + char *m = "小李"; + int n = fputs(m, f); // 单字符写入 + printf("已追加内容的结构 n = %d\n", n); + fclose(f); + return 0; +} \ No newline at end of file diff --git a/day14/d6.txt b/day14/d6.txt new file mode 100644 index 0000000..63dc57e --- /dev/null +++ b/day14/d6.txt @@ -0,0 +1 @@ +hello disen \ No newline at end of file diff --git a/day14/d7.c b/day14/d7.c new file mode 100644 index 0000000..b75f67f --- /dev/null +++ b/day14/d7.c @@ -0,0 +1,16 @@ +#include +#include + +int main() +{ + FILE *f = fopen("d7.txt", "r"); + char line[100] = ""; + + while (fgets(line, 100, f) != NULL) + { + printf("%s", line); + } + + fclose(f); + return 0; +} \ No newline at end of file diff --git a/day14/d7.txt b/day14/d7.txt new file mode 100644 index 0000000..98a0f9f --- /dev/null +++ b/day14/d7.txt @@ -0,0 +1,3 @@ +hello,world +张三 +爱唱歌 \ No newline at end of file diff --git a/day14/d8.c b/day14/d8.c new file mode 100644 index 0000000..47ecd77 --- /dev/null +++ b/day14/d8.c @@ -0,0 +1,18 @@ +#include +#include + +int main() +{ + FILE *f = fopen("d8.txt", "r"); + fseek(f, 3, SEEK_SET); // SEEK_SET 0 + char m[7]; + for (int i = 0; i < 6; i++) + { + m[i] = (char)fgetc(f); + } + m[6] = '\0'; + printf("%s\n", m); + + fclose(f); + return 0; +} \ No newline at end of file diff --git a/day14/d8.txt b/day14/d8.txt new file mode 100644 index 0000000..98a0f9f --- /dev/null +++ b/day14/d8.txt @@ -0,0 +1,3 @@ +hello,world +张三 +爱唱歌 \ No newline at end of file diff --git a/day14/d9.c b/day14/d9.c new file mode 100644 index 0000000..423560d --- /dev/null +++ b/day14/d9.c @@ -0,0 +1,21 @@ +#include +#include + +int main() +{ + FILE *f = fopen("d9.txt", "r"); + + int line1_n = 0; + while (fgetc(f) != '\n') + line1_n++; + + fseek(f, line1_n + 1, SEEK_SET); + + char line2[100]; + fgets(line2, 100, f); + line2[strlen(line2) - 1] = '\0'; // 去掉换行符 + printf("line2: %s", line2); + + fclose(f); + return 0; +} \ No newline at end of file diff --git a/day14/d9.txt b/day14/d9.txt new file mode 100644 index 0000000..98a0f9f --- /dev/null +++ b/day14/d9.txt @@ -0,0 +1,3 @@ +hello,world +张三 +爱唱歌 \ No newline at end of file