我是C的新手。我正在尝试创建一个C程序作为练习的简单票务系统。
我想将结构写入二进制文件然后读取它。但它没有在二进制文件中写入任何内容。
我得到的只是
文件已成功关闭。
文件已成功关闭。
二进制文件(ticket.bin)仍为空。
如果有人可以输入一个例子来帮助我理解如何将结构写入二进制文件并阅读它。
define STATIONNUM 10//Maximun number of station.
define rate1 160
define rate2 190
define rate3 230
struct Ticket{
int code;//code of the list
char station[20];//destination name.
int price;//transportation fee.
};
int main(){
FILE *fp;
int c;//for open close judgement return value.
int i;//use at for loop.
struct Ticket list[STATIONNUM]={
{1, "NewYork", rate1},
{2, "London", rate1},
{3, "Paris", rate1},
{4, "Tokyo", rate1},
{5, "HongKong ", rate2},
{6, "Sydney", rate2},
{7, "Milan", rate2},
{8, "Berlin", rate2},
{9, "Vancouver", rate3},
{10, "Afghanistan", rate3},
};
//open a binary file to write.
fp = fopen("ticket.bin", "wb");
if(! fp){
printf("open file fail");
}
//write data into binary file.
if (fwrite(list, sizeof(list[0]), STATIONNUM, fp) != STATIONNUM);
//close it.
c = fclose(fp);
//judge if it's closed.
if(c == -1){
printf("File close failed.\n");
}else if(c == 0){
printf("File successfully closed.\n");
}
//open binary file to read.
fp = fopen("ticket.bin", "rb");
if(! fp){
printf("open file fail");
}
fread(list, sizeof(struct Ticket), STATIONNUM, fp);
//close it.
c = fclose(fp);
//judge if it's closed.
if(c == -1){
printf("File close failed.\n");
}else if(c == 0){
printf("File successfully closed.\n");
}
}
答案 0 :(得分:1)
你的行
if (fwrite(list, sizeof(list[0]), STATIONNUM, fp) != STATIONNUM);
至少应该
if (fwrite(list, sizeof(struct Ticket),
STATIONNUM, fp) != STATIONNUM)
{ perror("fwrite"); exit(EXIT_FAILURE); };
您可以考虑在此之后执行if (fflush(fp)) perror("fflush");
。
你明显忘记了
if fread(list, sizeof(struct Ticket),
STATIONNUM, fp) != STATIONNUM)
{ perror("fread"); exit(EXIT_FAILURE); };
成功fp = fopen("ticket.bin", "rb");
行后。
您正好在测试每个图书馆电话,例如fopen
& fread
但如果失败,您应该显示perror
错误原因(或使用strerror(errno)
)。
您的代码
fprintf(fp, "%d\t%s\t%d\n",
list[i].code, list[i].station, list[i].price);
毫无意义。 (您应该测试fprintf
的结果是3,否则perror
)。您正在打开二进制读取的fp
句柄中打印文本!
也许你只想在这里printf
。
list
一个实际上是数组的变量是完全令人困惑的......
答案 1 :(得分:0)
我想问题就在这里:
(1)
...
//open a binary file to write.
fp = fopen("ticket.bin", "wb");
|
v
///...fopen("ticket.bin", "w" ); ///
...
(2)
...
//write data into binary file.
if (fwrite(list, sizeof(list[0]), STATIONNUM, fp) != STATIONNUM);
|
v
///...fwrite(&list,... ///
...
按照此示例操作,尝试逐步更改代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STATIONNUM 10 //Maximun number of station.
#define rate1 160
#define rate2 190
#define rate3 230
struct Ticket
{
int code;//code of the list
char station[20];//destination name.
int price;//transportation fee.
};
int main()
{ FILE *fp;
int i; //use at for loop.
struct Ticket list[STATIONNUM] =
{
{ 1, "NewYork ", rate1 },
{ 2, "London ", rate1 },
{ 3, "Paris ", rate1 },
{ 4, "Tokyo ", rate1 },
{ 5, "HongKong ", rate2 },
{ 6, "Sydney ", rate2 },
{ 7, "Milan ", rate2 },
{ 8, "Berlin ", rate2 },
{ 9, "Vancouver ", rate3 },
{ 10, "Afghanistan ", rate3 },
};
// open a binary file to write; write it; and close ///
if (!(fp = fopen("ticket.bin", "w"))) { printf("open file fail"); }
if (fwrite(&list, sizeof(list[0]), STATIONNUM, fp) != STATIONNUM);
if ((fclose(fp))==-1) { printf("File close failed.\n"); }
// 1-st
// open binary file to read; read; and close it ///
if (!(fp = fopen("ticket.bin", "r"))) { printf("open file fail"); }
fread(&list, sizeof(struct Ticket), STATIONNUM, fp);
if ((fclose(fp))==-1) { printf("File close failed.\n"); }
// Display initial information
for (i=0;i<STATIONNUM;i++)
{ printf("%3d %s %d \n", list[i].code, list[i].station, list[i].price); }
getchar();
// Change some thing :) ///
list[0].code = 20; strcpy(list[0].station, "MOSKOW "); list[0].price = 200;
list[1].code = 30; strcpy(list[1].station, "MINSK "); list[1].price = 250;
list[8].code = 60; strcpy(list[8].station, "ALMATY "); list[8].price = 330;
list[9].code = 90; strcpy(list[9].station, "VLADIVOSTOK "); list[9].price = 530;
// open a binary file to write; write it; close the file ///
if (!(fp = fopen("ticket.bin", "w"))) { printf("open file fail"); }
if (fwrite(&list, sizeof(list[0]), STATIONNUM, fp) != STATIONNUM);
if ((fclose(fp))==-1) { printf("File close failed.\n"); }
// 2-nd
// open binary file to read; read; and close it ///
if (!(fp = fopen("ticket.bin", "r"))) { printf("open file fail"); }
fread(&list, sizeof(struct Ticket), STATIONNUM, fp);
if ((fclose(fp))==-1) { printf("File close failed.\n"); }
// Display new information again
for (i=0;i<STATIONNUM;i++)
{ printf("%3d %s %d \n", list[i].code, list[i].station, list[i].price); }
getchar();
return 0;
}
祝你好运!