IO输入输出

来源:互联网 发布:妙笔书生精英数据论坛 编辑:程序博客网 时间:2024/05/07 06:14
#include<stdio.h>#include<stdlib.h>#define SIZE 2struct student{    int no;    char name[10];}stud[SIZE];void save(){       FILE *fp;    int i;    if((fp=fopen("C:\\Users\\Carl_Hugo\\Desktop\\a.txt","wb"))==NULL){        printf("can not open\n");        return;    }    for(i=0;i<SIZE;i++)        if(fwrite(&stud[i],sizeof(struct student),1,fp)!=1)            printf("ERROR\n");    fclose(fp);}void read(){    FILE *fp;    int i;    if((fp=fopen("C:\\Users\\Carl_Hugo\\Desktop\\a.txt","rb"))==NULL){        printf("can not open\n");        return;    }    for(i=0;i<SIZE;i++){        fread(&stud[i],sizeof(struct student),1,fp);        printf("no=%d,name=%s\n",stud[i].no,stud[i].name);    }    fclose(fp);}int main(){    int i;    for(i=0;i<SIZE;i++){        scanf("%d%s",&stud[i].no,stud[i].name);        save();    }    read();    return 1;}
0 0
原创粉丝点击