文件指针
struct _iobuf {
char *_ptr;
int _cnt;
char *_base;
int _flag;
int _file;
int _charbuf;
int _bufsiz;
char *_tmpfname;
}
typedef struct _iobuf FILE;
写文件
int main()
{ FILE *fp;
char ch1='a' ,ch2; int num1=50, num2 ;
char string1[20]="hellp", string2[20] ;
float score1 = 85.5,I score2;
if((fp = fopen("C: /Users/lzx/Desktop/file. txt", "w+"))==NULL) { printf("fail to fopen\n");
return - 1 ;
}
fprintf(fp,"%c %d %s %f\n", ch1,numl,string1, score1);
rewind(fp); //使用fscanf获取文件内容
fscanf(fp,"%c %d %s %f\n" , &ch2 , &num2 , &string2 ,&score2) ;
printf("%c %d %s %f\n", ch2 ,num2, string2, score2) ;
fclose(fp) ; return 0; }
格式化读写
int main() {
FILE *fp;
char ch1='a' ,ch2;
int num1=50, num2 ;
char string1[20]="hellp", string2[20] ;
float score1 = 85.5,I score2;
if((fp = fopen("C: /Users/lzx/Desktop/file. txt", "w+"))==NULL)
{
printf("fail to fopen\n"); return - 1 ;
}
fprintf(fp,"%c %d %s %f\n", ch1,numl,string1, score1);
rewind(fp);
fscanf(fp,"%c %d %s %f\n" , &ch2 , &num2 , &string2 ,&score2) ;
printf("%c %d %s %f\n", ch2 ,num2, string2, score2) ;
fclose(fp) ;
return 0;
}