为什么结构体数组写入文件,是写入的二进制文件?我没有用wb啊
代码:int main(int argc, const char * argv[]) {
struct student std[3] = {{"张三丰",28,60},{"胡斐",25,70.5},{"诸葛亮",45,100}};
FILE *fp = fopen("a.txt", "w+");
if (fp!=NULL) {
//fseek(fp, sizeof(struct student),SEEK_SET);
fwrite(std, sizeof(struct student), 3, fp);
printf("写入成功!\n");
struct student std1[3];
fseek(fp, -sizeof(struct student), SEEK_END);
fread(std1, sizeof(struct student), 1, fp);
for (int i=0; i<1; i++) {
printf("%s,%d,%.2f\n",std1[i].name,std[i].age,std1[i].score);
}
}
fclose(fp);
return 0;
}
|
|