代码之家  ›  专栏  ›  技术社区  ›  Thomas Winsnes

如何将文件读入结构数组?

c
  •  0
  • Thomas Winsnes  · 技术社区  · 14 年前

    我现在正在做一项任务,这让我耽搁了好几个小时。有人能帮我指出为什么这对我不起作用吗?

    
    struct book
    {
      char title[25];
      char author[50];
      char subject[20];
      int callNumber;
      char publisher[250];
      char publishDate[11];
      char location[20];
      char status[11];
      char type[12];
      int circulationPeriod;
      int costOfBook;
    }; 
    
    void PrintBookList(struct book **bookList)
    {
      int i;
      for(i = 0; i < sizeof(bookList); i++)
      {
        struct book newBook = *bookList[i];
        printf("%s;%s;%s;%d;%s;%s;%s;%s;%s;%d;%d\n",newBook.title, newBook.author, newBook.subject, 
            newBook.callNumber,newBook.publisher, newBook.publishDate, newBook.location, newBook.status, 
            newBook.type,newBook.circulationPeriod, newBook.costOfBook);
    
      }
    }
    
    void GetBookList(struct book** bookList)
    {
      FILE* file = fopen("book.txt", "r");
      struct book newBook[1024];
      int i = 0;
    
      while(fscanf(file, "%s;%s;%s;%d;%s;%s;%s;%s;%s;%d;%d",
            &newBook[i].title, &newBook[i].author, &newBook[i].subject, 
            &newBook[i].callNumber,&newBook[i].publisher, &newBook[i].publishDate, 
            &newBook[i].location, &newBook[i].status,
            &newBook[i].type,&newBook[i].circulationPeriod, &newBook[i].costOfBook) != EOF)
      {
        bookList[i] = &newBook[i];
        i++;
      }
    
      /*while(fscanf(file, "%s;%s;%s;%d;%s;%s;%s;%s;%s;%d;%d",
        &bookList[i].title, &bookList[i].author, &bookList[i].subject,
        &bookList[i].callNumber, &bookList[i].publisher, &bookList[i].publishDate,
        &bookList[i].location, &bookList[i].status, &bookList[i].type,
        &bookList[i].circulationPeriod, &bookList[i].costOfBook) != EOF)
      {
        i++;
      }*/
    
      PrintBookList(bookList);
    
      fclose(file);
    }
    
    int main()
    {
      struct book *bookList[1024];
      GetBookList(bookList);
    }
    

    编译时没有错误或警告

    它应该打印文件的内容,就像在文件中一样。 这样地:

    OperatingSystems Internals and Design principles;William.S;IT;741012759;Upper Saddle River;2009;QA7676063;Available;circulation;3;11200
    Communication skills handbook;Summers.J;Accounting;771239216;Milton;2010;BF637C451;Available;circulation;3;7900
    Business marketing management:B2B;Hutt.D;Management;741912319;Mason;2010;HF5415131;Available;circulation;3;1053
    Patient education rehabilitation;Dreeben.O;Education;745121511;Sudbury;2010;CF5671A98;Available;reference;0;6895  
    Tomorrow's technology and you;Beekman.G;Science;764102174;Upper Saddle River;2009;QA76B41;Out;reserved;1;7825  
    Property & security: selected essay;Cathy.S;Law;750131231;Rozelle;2010;D4A3C56;Available;reference;0;20075  
    Introducing communication theory;Richard.W;IT;714789013;McGraw-Hill;2010;Q360W47;Available;circulation;3;12150  
    Maths for computing and information technology;Giannasi.F;Mathematics;729890537;Longman;Scientific;1995;QA769M35G;Available;reference;0;13500  
    Labor economics;George.J;Economics;715784761;McGraw-Hill;2010;HD4901B67;Available;circulation;3;7585  
    Human physiology:from cells to systems;Sherwood.L;Physiology;707558936;Cengage Learning;2010;QP345S32;Out;circulation;3;11135  
    bobs;thomas;IT;701000000;UC;1006;QA7548;Available;Circulation;7;5050
    

    但当我运行它时,它会输出:

    OperatingSystems;;;0;;;;;;0;0  
    Internals;;;0;;;;;;0;0  
    and;;;0;;;;;;0;0  
    Design;;;0;;;;;;0;0  
    principles;William.S;IT;741012759;Upper;41012759;Upper;;0;;;;;;0;0  
    Saddle;;;0;;;;;;0;0  
    River;2009;QA7676063;Available;circulation;3;11200;lable;circulation;3;11200;;0;;;;;;0;0  
    Communication;;;0;;;;;;0;0  
    

    提前谢谢,你是救命恩人

    3 回复  |  直到 14 年前
        1
  •  3
  •   Paul R    14 年前

    我认为你的问题是你的字段包含空格。 fscanf 将停止扫描字符串( %s )当它看到一个空白字符时。你需要改变你的 %s 允许包含空格的格式说明符。您可以只排除分隔符,例如。 %[^;] 或者指定要包含的字符,例如。 %[ a-zA-Z0-9-] (我想我可能会选择第一个)。

    % man fscanf

        2
  •  3
  •   Mitch Wheat    14 年前

    不是原因,而是你的 PrintBookList 方法,你有

    for(i = 0; i < sizeof(bookList); i++)
    

    但是不能用这种方法得到结构数组的大小(它返回4,指针的大小)。

    标准做法是通过以下尺寸:

    void PrintBookList(struct book **bookList, int numBooks) 
    
        3
  •  0
  •   dplante Tschallacka    12 年前

    不要忘记限制读取字符串的大小(不要忘记它们以“\0”符号结尾)(scanf不知道字段有多长)。

    扫描(“%24[^;],……