C语言学习(39)

 1 //fread函数的作用是从指定文件中读入指定长度的数据块
 2 //fread(buffer,size,count,文件指针)
 3 #include<stdio.h>
 4 #include<stdlib.h>
 5 int main(){
 6     struct Book_Type{
 7         char name[10];
 8         int price;
 9         char author[10];
10     };
11     FILE *fp;
12     struct Book_Type book[2];
13     if((fp=fopen("file_data.txt","r"))==NULL){
14         printf("打开文件失败\n");
15         exit(0);
16     }
17     printf("书籍的信息如下:\n");
18     for(int i=0;i<2;i++){
19         fread(&book[i],sizeof(struct Book_Type),1,fp);
20     }
21     for(int j=0;j<2;j++){
22         printf("name=%s,price=%d,author=%s\n",book[j].name,book[j].price,book[j].author);
23     }
24     fclose(fp);
25     return 0;
26 }

猜你喜欢

转载自www.cnblogs.com/Tobi/p/9239912.html