From 1455f339be381cf4ff80ba693487ef527282e571 Mon Sep 17 00:00:00 2001 From: smallkun Date: Mon, 24 Mar 2025 19:46:42 +0800 Subject: [PATCH] Auto commit --- 2207/C语言同步练习源码/8-文件-1.c | 18 ++++++++++++++ 2207/C语言同步练习源码/8-文件-2.c | 18 ++++++++++++++ 2207/C语言同步练习源码/file1.c | 9 +++++++ 2207/C语言同步练习源码/file2.c | 9 +++++++ 2207/C语言同步练习源码/letter.txt | 1 + 2207/C语言同步练习答案.md | 41 +++++++++++++++++++++++++++++++ 6 files changed, 96 insertions(+) create mode 100644 2207/C语言同步练习源码/8-文件-1.c create mode 100644 2207/C语言同步练习源码/8-文件-2.c create mode 100644 2207/C语言同步练习源码/file1.c create mode 100644 2207/C语言同步练习源码/file2.c create mode 100644 2207/C语言同步练习源码/letter.txt diff --git a/2207/C语言同步练习源码/8-文件-1.c b/2207/C语言同步练习源码/8-文件-1.c new file mode 100644 index 0000000..87b3eb7 --- /dev/null +++ b/2207/C语言同步练习源码/8-文件-1.c @@ -0,0 +1,18 @@ +#include + +int main(){ + FILE *in, *out; + char str[100]; + in = fopen("file1.c", "r");//r:ֻ + out = fopen("file2.c", "w");//w: + + while(fgets(str, 100, in)){//fgetsÿδļȡһв洢str + puts(str);//ʹÿ̨ + fputs(str, out);//file2.c + } + fclose(in);//رļָ + fclose(out); + + return 0; +} + diff --git a/2207/C语言同步练习源码/8-文件-2.c b/2207/C语言同步练习源码/8-文件-2.c new file mode 100644 index 0000000..a221c8d --- /dev/null +++ b/2207/C语言同步练习源码/8-文件-2.c @@ -0,0 +1,18 @@ +#include + +int main(){ + + FILE *fp; + char ch; + int count=0; + fp = fopen("letter.txt", "r"); + while((ch=fgetc(fp)) != EOF){ + if(ch == 'c'){ + count++; + } + } + printf("cΪ:%d\n", count); + + return 0; +} + diff --git a/2207/C语言同步练习源码/file1.c b/2207/C语言同步练习源码/file1.c new file mode 100644 index 0000000..5485f69 --- /dev/null +++ b/2207/C语言同步练习源码/file1.c @@ -0,0 +1,9 @@ +#include + +int main(){ + + + + + return 0; +} \ No newline at end of file diff --git a/2207/C语言同步练习源码/file2.c b/2207/C语言同步练习源码/file2.c new file mode 100644 index 0000000..5485f69 --- /dev/null +++ b/2207/C语言同步练习源码/file2.c @@ -0,0 +1,9 @@ +#include + +int main(){ + + + + + return 0; +} \ No newline at end of file diff --git a/2207/C语言同步练习源码/letter.txt b/2207/C语言同步练习源码/letter.txt new file mode 100644 index 0000000..03f1447 --- /dev/null +++ b/2207/C语言同步练习源码/letter.txt @@ -0,0 +1 @@ +ccadfadsfasfadsfafa \ No newline at end of file diff --git a/2207/C语言同步练习答案.md b/2207/C语言同步练习答案.md index e423aae..a768209 100644 --- a/2207/C语言同步练习答案.md +++ b/2207/C语言同步练习答案.md @@ -1035,12 +1035,53 @@ int main(){ +```c +#include + +int main(){ + FILE *in, *out; + char str[100]; + in = fopen("file1.c", "r");//r:只读 + out = fopen("file2.c", "w");//w:覆盖 + + while(fgets(str, 100, in)){//fgets每次从文件获取一行并存储到str中 + puts(str);//使用控制台输出 + fputs(str, out);//向file2.c输出 + } + fclose(in);//关闭文件指针 + fclose(out); + + return 0; +} +``` + ### 文件-2 ![image-20250227231509693](https://yp.smallkun.cn/markdown/image-20250227231509693.png!compress) +```c +#include + +int main(){ + + FILE *fp; + char ch; + int count=0; + fp = fopen("letter.txt", "r"); + while((ch=fgetc(fp)) != EOF){ + if(ch == 'c'){ + count++; + } + } + printf("c个数为:%d\n", count); + + return 0; +} + +``` + ### 文件-3