【C语言IO】标准IO相关函数与文件IO相关函数总结

一、标准IO

  • fopen

    • 功能

      	打开一个文件
      
    • 头文件

      	#include <stdio.h>
      
    • 函数原型

      	FILE *fopen(const char *pathname, const char *mode);
      
    • 参数

      	const char *pathname  指定要打开的文件的路径以及文件名。
      
      	const char *mode  模式权限,以什么方式打开文件。
          
          mode类型:
             r      作用:以仅读的方式打开文件,若文件不存在不创建,光标在文件头
          		  返回值:不存在返回:NULL,根据错误设置errno
          				 存在返回: FILE*
      
             r+     作用:以读和写的方式打开文件,若文件不存在不创建,光标在文件头
          		  返回值:不存在返回:NULL,根据错误设置errno
          				 存在返回: FILE*
      
             w      作用:以写方式打开文件,若文件不存在将自动创建,光标在文件头
          		  返回值:创建失败返回:NULL,根据错误设置errno
          				 创建成功返回:FILE*
      
             w+     作用:以读写方式打开文件,若文件不存在将自动创建,光标在文件头
          		  返回值:创建失败返回:NULL,根据错误设置errno
          				 创建成功返回:FILE*
      
             a      作用:以仅写的方式(追加)打开文件,若文件不存在将自动创建,光标在文件尾
          		  返回值:创建失败返回:NULL,根据错误设置errno
          				 创建成功返回:FILE*
      
             a+     作用:以仅读写的方式(追加)打开文件,若文件不存在将自动创建,光标在文件尾
          		  返回值:创建失败返回:NULL,根据错误设置errno
          				 创建成功返回:FILE*
          
      
    • 实例

          FILE *p = fopen("./fopen.txt","a");
              if (p==NULL)
              {
                  perror("open file erro\n");
              }
          printf("open succful\n");
      
  • fclose

    • 功能

       关闭文件
      
    • 头文件

      	#include <stdio.h>
      
    • 函数原型

      	int fclose(FILE *stream);
      
    • 参数

      	FILE *stream //流指针
      
    • 返回值

      	成功: 返回0;
      	失败: 返回EOF,更新errno;     # define EOF (-1)
      
    • 实例

          if (fclose(p) ==EOF)
              {
                   perror("close file erro\n");
              }
              printf("close succful\n");
      

  • perror

    • 功能

      	根据errno打印对应的错误信息
      
    • 头文件

      	#include <stdio.h>
      
    • 函数原型

      	void perror(const char *s);
      
    • 参数

          const char *s  //参数通常用于传递一个描述性的错误信息字符串,以便在打印错误消息时能够显示出问题所在。
      
    • 实例

      	perror("close file erro\n");
      
  • fprintf

    • 功能

      	将数据格式化形式打印到指定的文件中 (file printf)//写入
      
    • 头文件

      	#include <stdio.h>
      
    • 函数原型

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

      	FILE *stream:表示要写入数据的文件流指针。
      
      	const char *format:是一个格式字符串,用于指定要写入的数据的格式。它包含格式转换说明符,例如 %d、%f 等
      
    • 返回值

          成功,返回成功被打印的字符个数;
          失败,返回负数。没有更新errno,所以无法用perror打印
      
    • 实例

          //创建流指针,并打开文件
          FILE *p = fopen("./fopen.txt","a");
          if (p==NULL)
          {
            perror("open file erro\n");
          }
          printf("open succful\n");
          
          //向文件中写入内容
          fprintf(p,"8-711\n");
      
  • fscanf

    • 功能

      	函数用于从指定文件流中按照指定格式读取数据,并将其存储到指定的变量中。
      
    • 头文件

      	从指定文件中格式化读取数据 //读取
      
    • 函数原型

      	int fscanf(FILE *stream, const char *format, ...);
      
    • 参数

      	FILE *stream:表示要读取数据的文件流指针。
      
      	const char *format:是一个格式字符串,用于指定如何解析文件中的数据。它包含格式转换说明符,例如 %d、%f 等
      
    • 返回值

          成功,         返回成功读取到的数据个数; 注意不是字符数;  >0;
          读取到文件结尾: 返回EOF;不更新errno, errno==0
          函数运行失败:   返回EOF,更新errno;
      
    • 实例

      //创建流指针并打开文件
          FILE *file =fopen ("./file.txt","r");
      
          if (file == NULL)
          {
              perror("fopen");
              return -1;
          }
           printf("fopen successful\n");
      
      
          char buf;
      
         //读取
         while (fscanf(file,"%c",&buf)!=EOF)
      {
          printf("%c",buf);
      }
      
         //关闭
         if (fclose(file)==EOF)
         {
              perror("fclose");
         }
         printf("fclose successful\n");
      
         
         
      
          return 0;
      

实例一:用fprintf和fsacnf完成cp功能

#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *source_file, *destination_file;
    char source_filename[100], destination_filename[100];
    char ch;

    // 获取源文件名和目标文件名
    printf("Enter source file name: ");
    scanf("%s", source_filename);
    printf("Enter destination file name: ");
    scanf("%s", destination_filename);

    // 打开源文件
    source_file = fopen(source_filename, "r");
    if (source_file == NULL) {
        printf("Error opening source file.\n");
        exit(1);
    }

    // 打开目标文件
    destination_file = fopen(destination_filename, "w");
    if (destination_file == NULL) {
        printf("Error creating destination file.\n");
        exit(1);
    }

    // 从源文件读取内容,并写入到目标文件中
    while ((ch = fgetc(source_file)) != EOF) {
        fprintf(destination_file, "%c", ch);
    }

    // 关闭文件
    fclose(source_file);
    fclose(destination_file);

    printf("File copied successfully.\n");

    return 0;
}

  • fputc

    • 功能

      	函数的功能是将字符 c 写入到由 stream 指向的文件中。它会将 c 转换为 unsigned char 类型,并将其写入到文件中
      
    • 头文件

      	#include <stdio.h>
      
    • 函数原型

      	int fputc(int c, FILE *stream); 
      
    • 参数

          int c:表示要写入的字符,以整数形式传递。通常是一个字符的 ASCII 值。
          FILE *stream:表示要写入数据的文件流指针。
      
    • 返回值

      	fputc(int c, FILE *stream) 返回写入的字符 c 的 ASCII 值,如果出错则返回 EOF-1)。
      
    • 实例

      character = fputc('A', file);
      
          if (character == EOF) {
              printf("写入文件时出错。\n");
              return 1;
          }
      
  • fgetc

    • 功能

      	从指定的文件流中读取一个字符,并将该字符作为无符号字符值转换为 unsigned char 类型返回
      
    • t头文件

      	#include <stdio.h>
      
    • 函数原型

      	int fgetc(FILE *stream);
      
    • 参数

          参数:stream:指向 FILE 对象的指针,表示要读取字符的文件流。
      
    • 返回值

          如果读取成功,则返回读取的字符作为 unsigned char 类型的值,
          否则返回 EOF(文件结束符或出错)
      
    • 实例

      FILE *file1 = fopen("example.txt", "r"); // 假设存在名为 "example.txt" 的文件
          if (file1 != NULL) {
              int ch;
              while ((ch = fgetc(file1)) != EOF) {
                  putchar(ch); // 输出读取到的字符
              }
              fclose(file1);
          }
      

实例二:用fputc和fgetc完成cp功能

#include <stdio.h>

int main() {
    FILE *source_file, *target_file;
    char ch;

    // 打开源文件
    source_file = fopen("source.txt", "r");
    if (source_file == NULL) {
        perror("Error opening source file");
        return 1;
    }

    // 打开目标文件
    target_file = fopen("target.txt", "w");
    if (target_file == NULL) {
        perror("Error opening target file");
        fclose(source_file);
        return 1;
    }

    // 从源文件读取字符,并写入目标文件
    while ((ch = fgetc(source_file)) != EOF) {
        fputc(ch, target_file);
    }

    // 关闭文件
    fclose(source_file);
    fclose(target_file);

    printf("File copied successfully.\n");
    return 0;
}

  • fputs

    • 功能

      	用于将字符串写入到指定的文件流中
      
    • 头文件

      	#include <stdio.h>
      
    • 函数原型

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

      	const char *s:要写入的字符串,必须是以 null 结尾的字符数组(C 字符串)。
          
      	FILE *stream:表示要写入数据的文件流指针。
      
    • 返回值

          如果成功写入字符串,则返回非负值。
          如果出现错误,则返回 EOF(通常为 -1)。
      
    • 实例

      FILE *file2 = fopen("example.txt", "a"); // 打开文件以追加模式
          if (file2 != NULL) {
              const char *str = "Hello, World!"; // 要写入的字符串
              if (fputs(str, file2) != EOF) {
                  printf("String written successfully.\n");
              } else {
                  printf("Error writing string.\n");
              }
              fclose(file2);
          } else {
              printf("Failed to open file.");
          }
      
  • fgets

    • 功能

      	函数用于从指定的文件流中读取一行字符串。
      
    • 头文件

      	#include <stdio.h>
      
    • 函数原型

      	char *fgets(char *s, int size, FILE *stream);
      
    • 参数

      	s:指向字符数组的指针,用于存储读取到的字符串。
      	size:要读取的最大字符数,包括 null 终止符。
      	stream:指向 FILE 对象的指针,表示要读取字符串的文件流。
      
    • 返回值

          如果成功读取字符串,则返回指向 str 的指针。
          如果到达文件结束或发生错误,则返回 NULL
    • 实例

      FILE *file2 = fopen("example.txt", "r"); // 假设存在名为 "example.txt" 的文件
          
      if (file2 != NULL) {
      	char buffer[100]; // 假设行的最大长度不超过 100 个字符
      	while (fgets(buffer, sizeof(buffer), file2) != NULL) {
      	printf("%s", buffer); // 输出读取到的字符串
         }
         fclose(file2);
      

实例三:用fputs和fgets完成cp功能

#include <stdio.h>

#define MAX_LINE_LENGTH 1024

int main() {
    FILE *source_file, *target_file;
    char buffer[MAX_LINE_LENGTH];

    // 打开源文件
    source_file = fopen("source.txt", "r");
    if (source_file == NULL) {
        perror("Error opening source file");
        return 1;
    }

    // 打开目标文件
    target_file = fopen("target.txt", "w");
    if (target_file == NULL) {
        perror("Error opening target file");
        fclose(source_file);
        return 1;
    }

    // 逐行从源文件读取内容,并写入目标文件
    while (fgets(buffer, MAX_LINE_LENGTH, source_file) != NULL) {
        fputs(buffer, target_file);
    }

    // 关闭文件
    fclose(source_file);
    fclose(target_file);

    printf("File copied successfully.\n");
    return 0;
}

  • fwrite

    • 功能

      	函数用于将数据块写入到指定的文件流中。
      
    • 头文件

      	#include <stdio.h>
      
    • 函数原型

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

      	ptr:指向要写入的数据的指针。
      	size:每个数据项的大小(以字节为单位)。
      	nmemb:要写入的数据项数量。
      	stream:指向 FILE 对象的指针,它标识了要写入的文件。
      
    • 返回值

          返回成功写入的数据项数量,
          如果出现错误,则返回一个小于 nmemb 的值。
      
    • 实例

      char data[] = "Hello, world!";
          size_t data_size = sizeof(data[0]);
          size_t data_length = sizeof(data) / sizeof(data[0]);
          size_t written_items = fwrite(data, data_size, data_length, fp);
          if (written_items != data_length) {
              perror("Error writing to file");
              return -1;
          }
      
  • fread

    • 功能

      	函数用于从指定的文件流中读取数据块。
      
    • 头文件

      	#include <stdio.h>
      
    • 函数原型

      	size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
      
    • 参数

          ptr:指向存储读取数据的内存块的指针。
          size:每个数据项的大小(以字节为单位)。
          nmemb:要读取的数据项数量。
          stream:指向 FILE 对象的指针,它标识了要读取的文件。
      
    • 返回值

          返回成功读取的数据项数量,
          如果出现错误或到达文件末尾,则返回一个小于 nmemb 的值。
      
    • 实例

      char data[] = "Hello, world!";
          size_t data_size = sizeof(data[0]);
          size_t data_length = sizeof(data) / sizeof(data[0]);
          size_t written_items = fwrite(data, data_size, data_length, fp);
          if (written_items != data_length) {
              perror("Error writing to file");
              return -1;
          }
      

实例四:用fread和fwrite完成cp功能

#include <stdio.h>

#define BUFFER_SIZE 4096

int main() {
    FILE *source_file, *target_file;
    char buffer[BUFFER_SIZE];
    size_t bytes_read;

    // 打开源文件
    source_file = fopen("source.txt", "rb");
    if (source_file == NULL) {
        perror("Error opening source file");
        return 1;
    }

    // 打开目标文件
    target_file = fopen("target.txt", "wb");
    if (target_file == NULL) {
        perror("Error opening target file");
        fclose(source_file);
        return 1;
    }

    // 从源文件读取内容,并写入目标文件
    while ((bytes_read = fread(buffer, 1, BUFFER_SIZE, source_file)) > 0) {
        fwrite(buffer, 1, bytes_read, target_file);
    }

    // 关闭文件
    fclose(source_file);
    fclose(target_file);

    printf("File copied successfully.\n");
    return 0;
}

  • fseek

    • 功能

      	函数用于设置文件位置指针,以便在文件中进行定位。
      
    • 头文件

      	#include <stdio.h>
      
    • 函数原型

      	int fseek(FILE *stream, long int offset, int whence);
      
    • 参数

          stream:指向 FILE 对象的指针,它标识了要定位的文件。
          offset:偏移量,用于指定位置相对于 whence 参数的偏移量。偏移量的单位是根据 whence 参数的不同而不同。
          whence:用于确定 offset 解释方式的值。可以是以下值之一:
          	SEEK_SET:从文件开头开始计算偏移量。
          	SEEK_CUR:从当前文件位置开始计算偏移量。
          	SEEK_END:从文件末尾开始计算偏移量。
      
    • 返回值

      	若成功,返回值为 0;若出错,返回值为 -1,且 errno 被设置为相应的错误代码。
      
    • 实例

      if (fseek(fp, 0, SEEK_END) != 0) {
              perror("Error seeking to end of file");
              fclose(fp);
              return -1;
          }
      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值