按照字符进行读写

  • 写文件 fgetc
  • 读文件 fputc
  • 文件结尾 EOF END

代码示例:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//1.字符的读写回顾
void test01()
{
   
        //写文件
        FILE*f_write = fopen("./test.txt","w");
        if (f_write == NULL)
        {
   
               return;
        }
        char buf[] = "hello C++!";
        for (int i = 0; i < strlen(buf); i++)
        {
   
               fputc(buf[i],f_write);
        }
        fclose(f_write);
        //读文件
        FILE*f_read = fopen("./test.txt", "r");
        if (f_read == NULL)
        {
   
               return;
        }
        char ch;
        while ( (ch = fgetc(f_read))   != EOF) //EOF END OF FILE
        {
   
               printf("%c",ch);
        }
        fclose(f_read);
}
int main()
{
   
        test01();
        return EXIT_SUCCESS;
}