一旦打开了流,则可以对流进行读写:每次一个字符,每次一行,二进制。
#include <stdio.h> int getc(FILE* fp); int fgetc(FILE* fp); int getchar(void);如果成功返回读取到的字符,出错或者到达文件结尾则返回EOF(一般为-1)。
#include <stdio.h> int ferror(FILE* fp); int feof(FILE* fp);
#include <stdio.h> void clearerr(FILE* fp);
#include <stdio.h> int main(void){ int c; while((c = fgetc(stdin)) != EOF){ fputc(c,stdout); } if(ferror(stdin) > 0){ printf("stdin error.\n"); }else if(feof(stdin) > 0){ printf("stdin eof.\n"); } clearerr(stdin); if(ferror(stdin) > 0){ printf("stdin error after clear.\n"); }else if(feof(stdin) > 0){ printf("stdin eof after clear.\n"); } return 0; }运行结果:
#include <stdio.h> int ungetc(int c, FILE* fp);
#include <stdio.h> int main(void){ int c; if((c = fgetc(stdin)) != EOF){ fputc(c,stdout); } ungetc(c,stdin); ungetc(c+1,stdin); while((c = fgetc(stdin)) != EOF){ fputc(c,stdout); } printf("\n"); return 0; }运行结果:
#include <stdio.h> int putc(int c, FILE* fp); int fputc(int c, FILE* fp); int putchar(int c);与输入函数一样,putc为宏,fputc为函数,putchar等价于fputc(stdout)。
#include <stdio.h> char* fgets(char* restrict buf, int n, FILE* restrict fp); char* gets(char * buf);
#include <stdio.h> int fputs(const char* restrict str, FILE* restrict fp); int puts(const char* str);如果成功则返回非负值,出错则返回EOF。
#include <stdio.h> int main(void){ int result = 0; char buf[256]; FILE* fp; if((fp = fopen("a.txt", "r+")) == NULL){ result = -1; perror("fopen"); goto FINALLY; } while(fgets(buf,256,fp) != NULL){ fputs(buf,stdout); } FINALLY: if(fp != NULL){ fclose(fp); } return result; }运行结果:
fputs(buf,stdout);
puts(buf);则结果为:
#include <stdio.h> size_t fread(void* restrict ptr, size_t size, size_t nobj, FILE* restrict fp); size_t fwrite(const void* restrict ptr, size_t size, size_t nobj, FILE* restrict fp);函数返回读或者写的对象数,其中size为每个元素的长度,nobj为欲写入元素的数量。
#include <stdio.h> int main(void){ int result = 0; int data[10] = {0,1,2,3,4,5,6,7,8,9}; int data2[10] = {0}; FILE* fp; if((fp = fopen("bfile","wb+")) == NULL){ result = -1; perror("fopen"); goto FINALLY; } if(fwrite(&data[1], sizeof(int), 3, fp) < 0){ result = -1; perror("fwrite"); goto FINALLY; } fclose(fp); if((fp = fopen("bfile","rb+")) == NULL){ result = -1; perror("fopen"); goto FINALLY; } if(fread(data2, sizeof(int), 3, fp) < 0){ result = -1; perror("fread"); goto FINALLY; } int i; for(i=0;i<10;i++){ printf("i=%d:%d\n",i,data2[i]); } FINALLY: if(fp != NULL){ fclose(fp); } return result; }运行结果:
#include <stdio.h> typedef struct{ int a; char b; }myst; int main(void){ int result = 0; FILE* fp; myst st1,st2; st1.a = 1; st1.b = 'a'; if((fp = fopen("bfile","wb+")) == NULL){ result = -1; perror("fopen"); goto FINALLY; } if(fwrite(&st1, sizeof(myst), 1, fp) < 0){ result = -1; perror("fwrite"); goto FINALLY; } fclose(fp); if((fp = fopen("bfile","rb+")) == NULL){ result = -1; perror("fopen"); goto FINALLY; } if(fread(&st2, sizeof(myst), 1, fp) < 0){ result = -1; perror("fread"); goto FINALLY; } printf("st2.a=%d\n",st2.a); printf("st2.b=%c\n",st2.b); FINALLY: if(fp != NULL){ fclose(fp); } return result; }运行结果: