IO输入输出

#include<stdio.h>
#include<stdlib.h>
#define SIZE 2

struct 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;
}



你可能感兴趣的:(C语言)