【C语言详解】——文件操作(建议收藏)_c语言 写文件原理-CSDN博客
一、文件的读取
# define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<errno.h>
#include<string.h>int main()
{FILE * pf=fopen("C:\\Users\\zhw\\Desktop\\test.txt", "r");if (pf == NULL) //如果打开文件失败{printf("%s\n", strerror(errno)); //打印错误日志return 1;}//读文件int ch = 0;while ((ch = fgetc(pf)) != EOF){printf("%c ", ch);}//关闭文件fclose(pf);pf = NULL;return 0;
}
搞懂C语言中的EOF是什么_eof ==在c语言中表示什么-CSDN博客
二 文件的写入
# define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<errno.h>
#include<string.h>int main()
{FILE * pf=fopen("C:\\Users\\zhw\\Desktop\\test.txt", "w");if (pf == NULL) //如果打开文件失败{printf("%s\n", strerror(errno)); //打印错误日志return 1;}//写文件char i = 0;for (i = 'a'; i <= 'z'; i++){fputc(i, pf);}//关闭文件fclose(pf);pf = NULL;return 0;
}
三 读取一行数据
# define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<errno.h>
#include<string.h>int main()
{FILE * pf=fopen("C:\\Users\\zhw\\Desktop\\test.txt", "r");if (pf == NULL) //如果打开文件失败{printf("%s\n", strerror(errno)); //打印错误日志return 1;}//写一行数据/*fputs("hellowold\n", pf);fputs("hellowold\n", pf);*///读一行数据char arr[20];fgets(arr, 20, pf);printf("%s\n", arr);//关闭文件fclose(pf);pf = NULL;return 0;
}
四 fprintf()函数的用法
# define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<errno.h>
#include<string.h>struct S
{char arr[10];int age;float score;};int main()
{struct S s = { "zhangsan",25,50.5f };FILE* pf = fopen("C:\\Users\\zhw\\Desktop\\test.txt", "w");if (pf == NULL) //如果打开文件失败{perror("fopen"); //打印错误日志return 1;}fprintf(pf, " %s %d %f", s.arr, s.age, s.score);//关闭文件fclose(pf);pf = NULL;return 0;
}
# define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<errno.h>
#include<string.h>struct S
{char arr[10];int age;float score;};int main()
{struct S s = { "zhangsan",25,50.5f };FILE* pf = fopen("C:\\Users\\zhw\\Desktop\\test.txt", "r");if (pf == NULL) //如果打开文件失败{perror("fopen"); //打印错误日志return 1;}//fprintf(pf, " %s %d %f", s.arr, s.age, s.score);//读取文件fscanf(pf, "%s %d %f", s.arr, &(s.age), &(s.score));//printf("%s %d %f\n", s.arr, s.age, s.score);fprintf(stdout,"%s %d %f\n", s.arr, s.age, s.score);//关闭文件fclose(pf);pf = NULL;return 0;
}
任何 一个C语言程序,只要运行起来就会默认打开3个流:
FILE * stdin -标准输入流(键盘)
FILE * stdout -标准输出流(屏幕)
FILE * stderr -标准错误流(屏幕)
五 fwrite 和fread
# define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<errno.h>
#include<string.h>struct S
{char arr[10];int age;float score;};int main()
{struct S s = { "zhangsan",25,50.5f };FILE* pf = fopen("C:\\Users\\zhw\\Desktop\\test.txt", "wb");if (pf == NULL) //如果打开文件失败{perror("fopen"); //打印错误日志return 1;}//以二进制的形式写到文件中fwrite(&s, sizeof(struct S), 1, pf);//关闭文件fclose(pf);pf = NULL;return 0;
}
# define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<errno.h>
#include<string.h>struct S
{char arr[10];int age;float score;};int main()
{struct S s = { 0 };FILE* pf = fopen("C:\\Users\\zhw\\Desktop\\test.txt", "rb");if (pf == NULL) //如果打开文件失败{perror("fopen"); //打印错误日志return 1;}//以二进制的形式写到文件中//fwrite(&s, sizeof(struct S), 1, pf);//以二进制的方式读fread(&s, sizeof(struct S), 1, pf);printf("%s %d %f\n", s.arr, s.age, s.score);//关闭文件fclose(pf);pf = NULL;return 0;
}