#include<stdio.h>intmain(){
FILE *fd;
fd=fopen("w.c","w+");if(fd==NULL){printf("fopen is error\n");return-1;}pritnf("fopen is success\n");fputc('a',fd);fclose(fd);return0;}
如果证明是否是行缓存直接在后面加 while(1);就行了
#include<stdio.h>intmain(){
FILE *fd;
fd=fopen("w.c","w+");if(fd==NULL){printf("fopen is error\n");return-1;}pritnf("fopen is success\n");fputc('a',fd);while(1);flcose(fd);return0;}
#include<stdio.h>intmain(){
FILE *fd;
fd=fopen("w.c","w+");if(fd==NULL){printf("fopen is error\n");return-1;}pritnf("fopen is success\n");fputc('a',fd);fputc('\n',fd);while(1);flcose(fd);return0;}
文件里 还是没有内容 所以 fputc 不是行缓存 但有缓存。
说明 fputc 不是行缓存函数
fgetc 函数
用 fgetc 把fputc 内容(文件内容 )到输出终端
#include<stdio.h>intmain(){
FILE *fd;int str;
fd=fopen("w.c","w+");if(fd==NULL){printf("fopen is error\n");return-1;}printf("fopen is success\n");fputc('a',fd);
str=fgetc(fd);printf("str is %c\n",str);flcose(fd);return0;}
为什么乱码呢
需要光标移动 因为fputc 到了最后 但是fgetc 却不知道。
fseek函数
这么配置就行了
SEEK_SET :从0开始
offest : 0单位
stream: 文件流
#include<stdio.h>intmain(){
FILE *fd;int str;
fd=fopen("w.c","w+");if(fd==NULL){printf("fopen is error\n");return-1;}printf("fopen is success\n");fputc('a',fd);fseek(fd,0,SEEK_SET);
str=fgetc(fd);printf("str is %c\n",str);flcose(fd);return0;}
#include<stdio.h>intmain(){
FILE *fd;int str;
fd=fopen("w.c","w+");if(fd==NULL){printf("fopen is error\n");return-1;}printf("fopen is success\n");fputc('a',fd);rewind(fd);
str=fgetc(fd);printf("str is %c\n",str);flcose(fd);return0;}