多种方法使用c语言读写文件

一:打开文件句柄

//参数1:文件路径

//参数2:文件打开模式

函数执行成功返回文件流指针,错误返回NULL。

FILE *fopen(const char *path, const char *mode);

 

模式               操作              区别                   文件要求

r                      读            从文件头开始        文件需存在

r+                  读写          从文件头开始        文件需存在

w                    写            从文件头开始        文件不存在则创建,存在则清空

w+                 读写         从文件头开始         文件不存在则创建,存在则清空

a                     写           从文件尾开始         文件不存在进行创建,存在则追加

a+                 读写         从文件头读取,从文件尾写入       文件不存在进行创建,存在则追加

 

代码示例:


   
   
   
   
  1. #include
  2. int main()
  3. {
  4. FILE *p = fopen( "/tmp/1.txt", "r");
  5. if(p == NULL)
  6. {
  7. printf( "open error!\n");
  8. return 0;
  9. }
  10. fclose(p);
  11. return 0;
  12. }

 

二:文件关闭

//参数1:文件流

int fclose(FILE *fp);

 

三:文件写入

1、字符写入:fputc();

//参数1:写入的字符

//参数2:文件流

//作用:将单个字符写入到文件中

//返回值:成功时,返回写入字符的ascii码值,错误返回EOF(-1)

int fputc(int c, FILE *stream);

 

示例:


   
   
   
   
  1. #include
  2. #include
  3. int main()
  4. {
  5. FILE *file = fopen( "/opt/admin/tmp/1.txt", "w");
  6. if(file == NULL)
  7. {
  8. printf( "open error!\n");
  9. return 0;
  10. }
  11. char name[] = "hello world!";
  12. int i;
  13. for(i = 0; i < strlen(name); i++)
  14. {
  15. fputc(name[i], file);
  16. }
  17. fputc( '\n', file);
  18. fclose(file);
  19. return 0;
  20. }

 

2、字符串写入:fputs();

//参数1:写入的字符串

//参数2:文件流

//作用:将字符串写入文件中

//返回值:返回一个非负值,如果发生错误则返回 EOF(-1)。

int fputs(const char *s, FILE *stream);

 

示例:


   
   
   
   
  1. #include
  2. #include
  3. int main()
  4. {
  5. FILE *file = fopen( "/opt/admin/tmp/1.txt", "w");
  6. if(file == NULL)
  7. {
  8. printf( "open error!\n");
  9. return 0;
  10. }
  11. char name[] = "hello world!\n";
  12. fputs(name, file);
  13. fclose(file);
  14. return 0;
  15. }

 

3、数据块写入:fwrite();

//参数1:要获取的数据的地址

//参数2:要写入内容的单字节数

//参数3:要写入size字节的数据项的个数

//参数4:目标文件指针

//返回值:返回实际写入的数据块的数目

//作用:向文件写入数据块,以二进制形式对文件进行操作,不局限于文本文件。

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);

 

示例:


   
   
   
   
  1. #include
  2. #include
  3. int main()
  4. {
  5. FILE *file = fopen( "/opt/admin/tmp/1.txt", "wb+");
  6. if(file == NULL)
  7. {
  8. printf( "open error!\n");
  9. return 0;
  10. }
  11. char name[] = "hello world!\n";
  12. fwrite(name, strlen(name), 1, file);
  13. fclose(file);
  14. return 0;
  15. }

 

4、格式化写入:fprintf();

//参数1:目标文件指针

//参数2:指定的格式控制字符串

//参数3:各种输出项,与格式控制字符串中的字段一起写到文件中

//返回值:执行成功返回实际写入文件的字符个数;执行失败,返回负数

//作用:用来将输出项按指定的格式写入到指定的文本文件中。

int fprintf(FILE *stream, const char *format, ...);

 

示例:


   
   
   
   
  1. #include
  2. #include
  3. int main()
  4. {
  5. FILE *file = fopen( "/opt/admin/tmp/1.txt", "wb+");
  6. if(file == NULL)
  7. {
  8. printf( "open error!\n");
  9. return 0;
  10. }
  11. char name[] = "hello world!\n";
  12. fprintf(file, "this is %s\n", name);
  13. fclose(file);
  14. return 0;
  15. }

四:文件读取

1、字符读取:fgetc()

//参数1:目标文件指针

//返回值:执行成功返回读取的字符,读取错误或者遇到结束标志EOF,返回EOF

//作用:从指定的文件中读取一个字符

int fgetc(FILE *stream);

 

示例:


   
   
   
   
  1. #include
  2. #include
  3. int main()
  4. {
  5. FILE *file = fopen( "./1.txt", "r");
  6. if(file == NULL)
  7. {
  8. printf( "open error!\n");
  9. return 0;
  10. }
  11. char c;
  12. while((c = fgetc(file)) != EOF)
  13. {
  14. printf( "%c", c);
  15. }
  16. fclose(file);
  17. return 0;
  18. }

2、字符串读取:fgets()

//参数1:存储读取的数据

//参数2:存储数据的大小

//参数3:要读取的文件流

//返回值:成功则返回读取的buf,失败则返回NULL,这是,buf中的数据不确定

//作用:读取指定场长度的字符串存到字符数组中。每次只读取一行,每次最多读bufsize-1个字符(第bufsize个字符赋'\0')

char *fgets(char *buf, int bufsize, FILE *stream);

 

示例:


   
   
   
   
  1. #include
  2. #include
  3. int main()
  4. {
  5. FILE *file = fopen( "./1.txt", "r");
  6. if(file == NULL)
  7. {
  8. printf( "open error!\n");
  9. return 0;
  10. }
  11. char buf[ 1024] = { 0};
  12. while(fgets(buf, 1024, file) != NULL)
  13. {
  14. printf( "%s", buf);
  15. }
  16. fclose(file);
  17. return 0;
  18. }

3、数据块读取:fread()

//参数1:存储读取的数据

//参数2:要读取的每个数据项的字节数

//参数3:要读取的数据项的个数

//参数4:读取的文件流

//返回值:返回真实读取的数据项count数,错误时返回0

//作用:一次读取文件中由若干个数据项组成的数据块,数据块的大小取决于数据项的大小和数据项的个数。

size_t fread ( void *buffer, size_t size, size_t count, FILE *stream) ;

 

示例:


   
   
   
   
  1. #include
  2. #include
  3. int main()
  4. {
  5. FILE *file = fopen( "./test", "r");
  6. if(file == NULL)
  7. {
  8. printf( "open error!\n");
  9. return 0;
  10. }
  11. char buf[ 102400] = { 0};
  12. fread(buf, 1024, 100, file);
  13. printf( "%s", buf);
  14. fclose(file);
  15. return 0;
  16. }

 

4、格式化读取:fscanf()

//参数1:读取的文件流

//参数2:读取的字符格式

//参数3:读取的各个输入项

//返回值:成功则返回读入的参数个数,失败返回EOF

//作用:根据数据格式format从文件流中读取数据,遇到空格或换行结束。

int fscanf(FILE*stream,const char*format,[argument...]);

示例:


   
   
   
   
  1. #include
  2. #include
  3. int main()
  4. {
  5. FILE *file = fopen( "./1.txt", "r");
  6. if(file == NULL)
  7. {
  8. printf( "open error!\n");
  9. return 0;
  10. }
  11. char buf[ 1024] = { 0};
  12. fscanf(file, "%s", buf);
  13. printf( "%s\n", buf);
  14. fclose(file);
  15. return 0;
  16. }

 

五:文件读写结合

1、fgetc()与fputc()结合使用

//以字符格式读取文件,再以字符格式写入文件,适用于文本文件

示例:


   
   
   
   
  1. #include
  2. #include
  3. int main()
  4. {
  5. FILE *file = fopen( "./1.txt", "r");
  6. if(file == NULL)
  7. {
  8. printf( "open error!\n");
  9. return 0;
  10. }
  11. FILE *fp = fopen( "./2.txt", "w");
  12. char c;
  13. while( (c = fgetc(file)) != EOF )
  14. {
  15. fputc(c, fp);
  16. }
  17. fclose(file);
  18. fclose(fp);
  19. return 0;
  20. }

 

2、fgets()与fputs()结合使用

//从文件中按行读取字符串,再以字符串写入文件,适用于文本文件,优点是按行读取很方便

示例:


   
   
   
   
  1. #include
  2. #include
  3. int main()
  4. {
  5. FILE *file = fopen( "./1.txt", "r");
  6. if(file == NULL)
  7. {
  8. printf( "open error!\n");
  9. return 0;
  10. }
  11. FILE *fp = fopen( "./2.txt", "w");
  12. char buf[ 1024] = { 0};
  13. while(fgets(buf, 1024, file) != NULL)
  14. {
  15. fputs(buf, fp);
  16. }
  17. fclose(file);
  18. fclose(fp);
  19. return 0;
  20. }

 

3、fread()与fwrite()结合使用

//以数据块格式读取,再以数据块格式写入到文件中,可以读取二进制文件,优点是读取二进制文件使用

示例:


   
   
   
   
  1. #include
  2. #include
  3. int main()
  4. {
  5. FILE *file = fopen( "./1.txt", "r");
  6. if(file == NULL)
  7. {
  8. printf( "open error!\n");
  9. return 0;
  10. }
  11. FILE *fp = fopen( "./2.txt", "w");
  12. char buf[ 1024] = { 0};
  13. fread(buf, 1024, 1, file);
  14. fwrite(buf, strlen(buf), 1, fp);
  15. fclose(file);
  16. fclose(fp);
  17. return 0;
  18. }

4、fprintf()与fscanf()结合使用

//以格式化的方式读取,遇到空格或换行就结束,再将读取的文件写入到文件中,优点是可以指定写入的文件格式

示例:


   
   
   
   
  1. #include
  2. #include
  3. int main()
  4. {
  5. FILE *file = fopen( "./1.txt", "r");
  6. if(file == NULL)
  7. {
  8. printf( "open error!\n");
  9. return 0;
  10. }
  11. FILE *fp = fopen( "./2.txt", "w");
  12. char buf[ 1024] = { 0};
  13. fscanf(file, "%s", buf);
  14. fprintf(fp, "aaa:%s", buf);
  15. fclose(file);
  16. fclose(fp);
  17. return 0;
  18. }

 

参考:

https://www.cnblogs.com/maluning/p/7955750.html

https://blog.csdn.net/songbook/article/details/79686322

 

 

你可能感兴趣的:(C语言)