现在的位置: 首页 > 综合 > 正文

使用文件(FILE)输入输出

2018年09月13日 ⁄ 综合 ⁄ 共 433字 ⁄ 字号 评论关闭
#include<stdio.h>
#include<stdlib.h>
 struct Stu
{
 char name[10];
 unsigned no,age; 
};
int main()
{
 struct Stu s;
 FILE *fp;
 fscanf(stdin,"%s%d%d",s.name,&s.no,&s.age);
 if((fp=fopen("stu.txt","w"))==NULL)
 {
  puts("error\n");
  exit(1);
 }
 fprintf(fp,"%s\t%d\t%d\n",s.name,s.no,s.age);
 fclose(fp);
 if((fp=fopen("stu.txt","r"))==NULL)
 {
  puts("error\n");
  exit(1);
 }
 fscanf(fp,"%s%d%d",s.name,s.no,s.age);
 fprintf(stdout,"%s\t%d\t%d\n",s.name,s.no,s.age);
 fclose(fp);
 return 0;
}

 

抱歉!评论已关闭.