c语言的文件读写简单使用
文件读写在日常编程中经常使用,但今天看c语言有关于文件的操作几乎忘完了,所以记录一下
1.下面是对一个字符进行读操作,
#include <stdio.h> #include <stdlib.h> FILE* fp=NULL; int ch=0; if((fp=fopen("filename","mode"))==NULL) { exit(-1); } while ((ch=fgetc(fp))!=EOF) //读操作 { printf("%c\n", ch); } fclose(fp);
2.下面的对一个字符进行写操作,
#include <stdio.h> #include <stdlib.h> FILE* fp=NULL; int ch=0; scanf("%d",&ch); if((fp=fopen("filename","mode"))==NULL) { exit(-1); } fputc(ch,fp); //写操作 fclose(fp);
上面两个都是对一个字符操作的,其实字符串也就是改一下函数,读将fgetc()改为fgets(),写将fputc()改为fputs(),如果是二进制则用fread()和fwrite()函数就可以了,
好了,我们下回见,peace

浙公网安备 33010602011771号