day14: 文件读写操作

This commit is contained in:
flykhan 2023-07-21 00:58:46 +08:00
parent 5408f0b622
commit 048a4d83ac
27 changed files with 347 additions and 0 deletions

View File

@ -19,3 +19,5 @@
#### day12: 字符串处理函数const结构体
#### day13: 结构体,位域,共用体,枚举,链表操作
#### day14: 文件读写操作

Binary file not shown.

36
day14/d10.c Normal file
View File

@ -0,0 +1,36 @@
#include <stdio.h>
#include <stdlib.h>
// 实现读取 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;
}

6
day14/d10.txt Normal file
View File

@ -0,0 +1,6 @@
hello,world
张三
爱唱歌
lihai
hahaha
手机

22
day14/d11.c Normal file
View File

@ -0,0 +1,22 @@
#include <stdio.h>
// 文件复制
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;
}

6
day14/d11.txt Normal file
View File

@ -0,0 +1,6 @@
hello,world
张三
爱唱歌
lihai
hahaha
手机

6
day14/d11_cpy.txt Normal file
View File

@ -0,0 +1,6 @@
hello,world
张三
爱唱歌
lihai
hahaha
手机

22
day14/d12.c Normal file
View File

@ -0,0 +1,22 @@
#include <stdio.h>
// 二进制文件复制
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;
}

BIN
day14/d12.jpg Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 96 KiB

BIN
day14/d12_cpy.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 96 KiB

36
day14/d13.c Normal file
View File

@ -0,0 +1,36 @@
// 自定义二进制文件写入
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}

BIN
day14/d13.txt Normal file

Binary file not shown.

28
day14/d14.c Normal file
View File

@ -0,0 +1,28 @@
// 自定义二进制文件读取
#include <stdio.h>
#include <stdlib.h>
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;
}

42
day14/d15.c Normal file
View File

@ -0,0 +1,42 @@
// 自定义二进制文件读取
#include <stdio.h>
#include <stdlib.h>
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;
}

15
day14/d2.c Normal file
View File

@ -0,0 +1,15 @@
// 行缓冲区
// 写数据时遇到换行符,则刷新缓冲
// 如果行缓冲不刷新时,则不会输出结果。
// 因为输出的内容没有遇到换行符Linux `\n`, Window(`\r\n`)。
#include <stdio.h>
int main()
{
printf("Hello World"); // 缓冲区未写满,不会刷新缓冲区,不会输出
while (1)
;
return 0; // 缓冲未写满,不会刷新缓冲区,不会退出程序
}

15
day14/d3.c Normal file
View File

@ -0,0 +1,15 @@
#include <stdio.h>
int main()
{
int i = 0;
// 缓冲区一般为 4KB这里写入 32B不会刷新缓冲区
char m[33] = "disenjackydisenjackydisenjackyab";
printf("%s", m);
fflush(stdout); // 刷新缓冲区,目的是为了让缓冲区中的数据输出到屏幕上
while (1)
; // 用来观察程序退出时是否会刷新缓冲区,阻塞程序
return 0;
}

14
day14/d4.c Normal file
View File

@ -0,0 +1,14 @@
// 程序正常退出也会刷新缓冲区,显示输出
#include <stdio.h>
int main()
{
int i = 0;
// 缓冲区一般为 4KB这里写入 32B不会刷新缓冲区
char m[33] = "disenjackydisenjackydisenjackyab";
printf("%s", m);
;
return 0;
}

18
day14/d5.c Normal file
View File

@ -0,0 +1,18 @@
// 读写文本文件
#include <stdio.h>
#include <stdlib.h>
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;
}

1
day14/d5.txt Normal file
View File

@ -0,0 +1 @@
disen

13
day14/d6.c Normal file
View File

@ -0,0 +1,13 @@
#include <stdio.h>
#include <string.h>
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;
}

1
day14/d6.txt Normal file
View File

@ -0,0 +1 @@
hello disen

16
day14/d7.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
#include <string.h>
int main()
{
FILE *f = fopen("d7.txt", "r");
char line[100] = "";
while (fgets(line, 100, f) != NULL)
{
printf("%s", line);
}
fclose(f);
return 0;
}

3
day14/d7.txt Normal file
View File

@ -0,0 +1,3 @@
hello,world
张三
爱唱歌

18
day14/d8.c Normal file
View File

@ -0,0 +1,18 @@
#include <stdio.h>
#include <string.h>
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;
}

3
day14/d8.txt Normal file
View File

@ -0,0 +1,3 @@
hello,world
张三
爱唱歌

21
day14/d9.c Normal file
View File

@ -0,0 +1,21 @@
#include <stdio.h>
#include <string.h>
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;
}

3
day14/d9.txt Normal file
View File

@ -0,0 +1,3 @@
hello,world
张三
爱唱歌