C语言学习(38)

 1 //fwrite函数的作用是将指定长的数据写入文件中
 2 //fwrite(fp,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","wb"))==NULL){//以二进制形式打开,如果直接打开文件可能会乱码
14         printf("打开文件失败\n");
15         exit(0);
16     }
17     printf("输入书的信息:\n");
18     for(int i=0;i<2;i++){
19         scanf("%s%d%s",book[i].name,&book[i].price,book[i].author);
20         fwrite(&book[i],sizeof(struct Book_Type),1,fp);
21     }
22     fclose(fp);
23     return 0;
24 }

猜你喜欢

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