我正在尝试将结构数组写入文件,然后将该文件读入一个空结构数组并打印出该数组每个struct元素内的变量。
我的代码在下面,目前我只有它才能尝试打印第一个元素,但屏幕上什么都没有打印出来。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct{
char name[1024];
int price;
} game;
void main(){
FILE *filename;
filename = fopen("file.txt","w");
game library[3]; //creation of our game library
//initializing the array to be written, 3 structs inside this array should be written to the file in sizeof(game).
strcpy(library[0].name,"Minecraft");
strcpy(library[1].name,"Opium");
strcpy(library[2].name,"Devil");
library[0].price = 10;
library[1].price = 20;
library[2].price = 30;
fwrite(library,sizeof(game),3,filename);
fseek(filename,0,SEEK_SET);
game second[3];
printf("test");
fread(second,sizeof(game),3,filename);
printf("element 0 is %s $%d",second[0].name,second[0].price);
fclose(filename);
}