-6

我是 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");
    }

}
4

2 回答 2

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实际上是数组的变量完全令人困惑......

于 2013-09-30T05:23:56.190 回答
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;
}

祝你好运!

于 2014-05-09T00:29:46.580 回答