From 7cf9e55fd987bc681d5881a53b5b1d808673d9b8 Mon Sep 17 00:00:00 2001 From: jaywcjlove Date: Thu, 13 Oct 2022 08:32:15 +0000 Subject: [PATCH] doc: update `docs/c.md`. 877a763274e8ace1d5f157768cce916d5de30021 --- docs/c.html | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/docs/c.html b/docs/c.html index 6562140c..8d03217e 100644 --- a/docs/c.html +++ b/docs/c.html @@ -1259,8 +1259,8 @@ 13, 'B' }; // 修改值 -s1.myNum = 30; -s1.myLetter = 'C'; +s1.myNum = 30; +s1.myLetter = 'C'; // 打印值 printf("%d %c %s", s1.myNum, @@ -1384,10 +1384,12 @@
模式 Mode描述 Description
r读取模式打开一个文本文件,允许读取文件
w模式打开一个文本文件,允许写入文件
a追加模式打开一个文本文件
如果文件不存在,则会创建一个新文件
r+读写模式打开一个文本文件,允许读写文件
w+读写模式打开一个文本文件,允许读写文件
a+读写模式打开一个文本文件,允许读写文件
rb读取模式打开二进制文件
wb写入模式打开二进制文件
ab追加模式打开二进制文件
rb+读写模式打开二进制文件
wb+读写模式打开二进制文件
ab+读写模式打开二进制文件

打开文件:fopen()

#include<stdio.h>
+
 void main( ) {
   FILE *fp;
   char ch;
-  fp = fopen("file_handle.c", "r");
+  fp = fopen("file_handle.c", "r");
+
   while (1) {
     ch = fgetc(fp);
     if (ch == EOF)
@@ -1399,11 +1401,13 @@
 

对文件执行所有操作后,必须关闭 fclose() 该文件

写入文件:fprintf()

-
#include <stdio.h>  
+
#include <stdio.h>
+
 main() {
   FILE *fp;
   fp = fopen("file.txt", "w"); // 打开文件
-  fprintf(fp, "Hello file by fprintf...\n"); // 将数据写入文件
+  // 将数据写入文件
+  fprintf(fp, "fprintf 的 Hello 文件..\n");
   fclose(fp); // 关闭文件  
 }  
 
@@ -1411,9 +1415,9 @@
#include <stdio.h>  
 main(){
   FILE *fp;
-  char buff[255]; // 创建 char 数组来存储文件数据
+  char buff[255]; // 创建char数组存储文件数据
   fp = fopen("file.txt", "r");
-  while(fscanf(fp, "%s", buff)!=EOF) {
+  while(fscanf(fp, "%s", buff)!=EOF) {
     printf("%s ", buff);
   }
   fclose(fp);
@@ -1421,10 +1425,11 @@
 

写入文件:fputc()

#include <stdio.h>
+
 main(){
   FILE *fp;
   fp = fopen("file1.txt", "w"); // 打开文件
-  fputc('a',fp); // 将单个字符写入文件
+  fputc('a',fp); // 将单个字符写入文件
   fclose(fp);    // 关闭文件
 }
 
@@ -1436,7 +1441,7 @@ char c; clrscr(); fp=fopen("myfile.txt", "r"); - while((c=fgetc(fp))!=EOF){ + while((c=fgetc(fp))!=EOF){ printf("%c", c); } fclose(fp); @@ -1446,11 +1451,12 @@

写入文件:fputs()

#include<stdio.h>
 #include<conio.h>
+
 void main(){
   FILE *fp;
   clrscr();
   fp = fopen("myfile2.txt","w");
-  fputs("hello c programming",fp);
+  fputs("hello c programming",fp);
   fclose(fp);
   getch();
 }
@@ -1458,13 +1464,14 @@
 

读取文件:fgets()

#include<stdio.h>
 #include<conio.h>
+
 void main() {
   FILE *fp;
   char text[300];
   clrscr();
 
   fp=fopen("myfile2.txt", "r");
-  printf("%s", fgets(text, 200, fp));
+  printf("%s", fgets(text, 200, fp));
   fclose(fp);
   getch();
 }
@@ -1476,7 +1483,8 @@
   fp = fopen("myfile.txt","w+");
   fputs("This is Book", fp);
 
-  fseek(fp, 7, SEEK_SET);
+  // 将文件指针设置到给定位置
+  fseek(fp, 7, SEEK_SET);
   fputs("Kenny Wong", fp);
   fclose(fp);
 }
@@ -1493,7 +1501,7 @@
   while((c=fgetc(fp)) != EOF){
     printf("%c", c);
   }
-  rewind(fp); // 将文件指针移动到文件的开头
+  rewind(fp); // 将文件指针移动到文件的开头
   while((c=fgetc(fp)) != EOF){
     printf("%c", c);
   }
@@ -1506,6 +1514,7 @@
 

ftell()

#include <stdio.h>
 #include <conio.h>
+
 void main (){
    FILE *fp;
    int length;
@@ -1513,7 +1522,7 @@
    fp = fopen("file.txt", "r");
    fseek(fp, 0, SEEK_END);
 
-   length = ftell(fp);
+   length = ftell(fp); // 返回当前位置
 
    fclose(fp);
    printf("文件大小: %d bytes", length);