簡體   English   中英

使用 sscanf 和 fgets 讀取多行文本文件

[英]Reading a multiline text file using sscanf and fgets

我需要讀取一個文本文件,然后將結果打印出來。 我使用結構來保存變量,我不需要創建數組。 問題是代碼似乎沒有讀取第二行,或者我打印不正確,結果完全混亂。 這是文本文件:

UTSA01 8 0 150.00 80.00
Armadillos,utsa@xyz.com,(210)555-1111,Jean E Us 
COM001 7 1 150.00 75.00
Comm Eagles,maeking@xyz.com,(210)555-2222,Mae King
SOUTH1 5 3 120.00 75.00
Slam Dunk,slamdunk@gmail.com,(210)555-3333,Jerry Tall
ALHGHT 4 4 175.00 100.00
Cake Eaters,sliverspoon@xyz.com,(210)555-6666,E Z Street
UNKN01 1 7 150.00 50.00 
Org New Blk,bobwire@xyz.com,(210)555-1234,Bob Wire
NEWB01 0 8 120.00 75.00
River Rats,riverrat@xyz.com,(210)555-4444,Rock D Boat
UNKN02 3 5 150.00 75.00
Hackers,cyber@gmail.com,(210)555-5555,Tom E Gunn

我一直在一個 while 循環中使用 sscanf 和 fgets。 這是我的代碼:

char szInputBuffer[100];
/* Create a struct array that will hold the data that is being read in */
Team teams;

/* The following segment contains the header for the file */
printf("%-7s%-13sWins Loss Fee Amt  Paid Amt\n", "Id", "Team Name");
printf("                %-21s%-14sEmail\n", "Contact Name", "Phone");

/* The following structure will begin to read file data into the respectful 
   variables until the end-of-file marker has been reached.
   The data will be used later to print out. */

while(!feof(pFileTeam)) {
    fgets(szInputBuffer, 100, pFileTeam);
    sscanf(szInputBuffer, "%7s %d %d %lf %lf",
                           teams.szTeamId,
                           &teams.iWins,
                           &teams.iLosses,
                           &teams.dFeeAmount,
                           &teams.dPaidAmount);

    fgets(szInputBuffer, 256, pFileTeam);                      
    sscanf(szInputBuffer, "%[^,]%[^,]%[^,]%[^\n]",
           teams.szTeamName,
           teams.szEmailAddr,
           teams.szPhone,
           teams.szContactname);


    printf("%-7s%-13s%-5d%-5d%-9.2lf%-8.2lf\n",
        teams.szTeamId,
        teams.szTeamName,
        teams.iWins,
        teams.iLosses,
        teams.dFeeAmount,
        teams.dPaidAmount);
    printf("                %-21s%-14s%-5s\n",
        teams.szContactname,
        teams.szPhone,
        teams.szEmailAddr);
}//END while

我遇到的問題是我可以讀取和打印文件的第一行,第二行中的第一個字符串很長。 但是,一旦我點擊逗號,它似乎就會被絆倒。 我不確定是我在 sscanf 中使用的說明符還是正在打印的說明符。 我真的很困惑我應該做什么。

這是我編譯(gcc)並執行時的結果:

Id     Team Name    Wins Loss  Fee Amt   Paid Amt
                    Contact Name         Phone         Email
    UTSA01 Armadillos      8    0    150.00     80.00

    COM001 Comm Eagles     7    1    150.00     75.00

    SOUTH1 Slam Dunk       5    3    120.00     75.00

    ALHGHT Cake Eaters     4    4    175.00    100.00

    UNKN01 Org New Blk     1    7    150.00     50.00

    NEWB01 River Rats      0    8    120.00     75.00

    UNKN02 Hackers         3    5    150.00     75.00

它實際上在我終端的第二行顯示了一些奇怪的字符,但它們似乎沒有出現在這里。

編輯:我添加了逗號,但輸出仍然無效。 只有最后打印的行會顯示 szContactname 信息。 所有其他人都將該位置留空,但顯示了電話和電子郵件:

Id     Team Name    Wins Loss  Fee Amt   Paid Amt
                    Contact Name         Phone         Email
    UTSA01 Armadillos      8    0    150.00     80.00
                    (210)555-1111 utsa@xyz.com
    COM001 Comm Eagles     7    1    150.00     75.00
                    (210)555-2222 maeking@xyz.com
    SOUTH1 Slam Dunk       5    3    120.00     75.00
                    (210)555-3333 slamdunk@gmail.com
    ALHGHT Cake Eaters     4    4    175.00    100.00
                    (210)555-6666 sliverspoon@xyz.com
    UNKN01 Org New Blk     1    7    150.00     50.00
                    (210)555-1234 bobwire@xyz.com
    NEWB01 River Rats      0    8    120.00     75.00
                    (210)555-4444 riverrat@xyz.com
    UNKN02 Hackers         3    5    150.00     75.00
                    Tom E Gunn           (210)555-5555 cyber@gmail.com

一些問題

  1. 使用fgets()緩沖區而不檢查結果。 在嘗試使用szInputBuffer之前檢查fgets()的結果。 不需要進一步的feof()檢查。 @kaylum

     // while(!feof(pFileTeam)) { // fgets(szInputBuffer, 100, pFileTeam); // sscanf(szInputBuffer, .... while(fgets(szInputBuffer, sizeof szInputBuffer, pFileTeam) != NULL) { sscanf(szInputBuffer, ....
  2. 使用魔術數字。 想想szInputBuffer[]並沒有那么大。

     char szInputBuffer[100]; ... // fgets(szInputBuffer, 256, pFileTeam); if (fgets(szInputBuffer, sizeof szInputBuffer, pFileTeam) == NULL) break;
  3. 沒用的格式。 "%[^,]%[^,]..."嘗試在不讀取文件流的情況下讀取文本到','離開','的位置。 @用戶3121023 健壯的代碼限制字符串輸入並檢查sscanf()結果。

     // sscanf(szInputBuffer, "%[^,]%[^,]%[^,]%[^\\n]", teams.szTeamName, ... // Assuming szTeamName[10] if (4 != sscanf(szInputBuffer, "%9[^,],%9[^,],%9[^,],%9[^\\n]", teams.szTeamName, ...) { break; }
  4. 也許其他問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM