2017-07-21 78 views
-2
#include <stdio.h> 
#define MAX_TITLE_SIZE 20 
#define MAX_BOOKS 10 

struct Book { 
    int _isbn; 
    float _price; 
    int _year; 
    char _title[MAX_TITLE_SIZE + 1]; 
    int _qty; 
}; 

void clear(void); 
int readRecord(FILE *fp, struct Book *b2read); 
void displayInventory(const char filename[]); 



int main(void) { 

    struct Book myBook; 
    char filename[21] = "144_w9_inventory.txt"; 

    displayInventory(filename); 

    return 0; 
} 

void clear(void) { 

    while (getchar() != '\n'); 

} 

int readRecord(FILE *fp, struct Book *b2read){ 

    //Define a variable int rv = 0 
    int rv = 0; 


    rv = fscanf(fp, "%d;%f;%d;%d;%20[^\n]", &(b2read->_isbn), &(b2read->_price), &(b2read->_year), &(b2read->_qty), b2read->_title); 
    //return rv; 

    return rv; 
} 

void displayInventory(const char filename[]) { 

    struct Book myBook; 

    FILE *fp = NULL; 
    int i; 

    fp = fopen(filename, "r"); //open the file for reading 

    if (fp != NULL) { 

     printf("\n\nInventory\n"); 
     printf("===================================================\n"); 
     printf("ISBN  Title    Year Price Quantity\n"); 
     printf("---------+-------------------+----+-------+--------\n"); 

     while(readRecord(fp, &myBook) == 5){ 
     printf("%-10.0d%-20s%-5d$%-8.2f%-8d\n", myBook._isbn, myBook._title, myBook._year, myBook._price, myBook._qty); 
     } 
     printf("===================================================\n"); 
     printf("\n"); 

     fclose(fp); 
    } 
    else { 
     printf("Failed to open file\n"); 
    } 
} 

什麼是文本文件,裏面是:如何從C文本文件中掃描值時清除輸入緩衝區?

234562;23.99;2010;3;Harry Potter 
567890;12.67;2015;4;The Hunger Games 
109821;53.20;2017;2;Stranger Things 

輸出:

Inventory 
=================================================== 
ISBN  Title    Year Price Quantity 
---------+-------------------+----+-------+-------- 
234562 Harry Potter 
     2010 $23.99 3  
567890 The Hunger Games 
    2015 $12.67 4  
109821 Stranger Things  2017 $53.20 2  
=================================================== 

當我輸出的程序,我能得到的所有值,但由於某種原因,當我打印這些值,整個字符串變爲一半,然後向下移動一行。

的repl.it在這裏,如果你想看看:

https://repl.it/JbRy/69

我如何得到輸出單行打印出來;而不是閱讀\ n「新行」,如果是這樣的話?

+1

這個代碼看起來很好,和我的系統上按預期工作。無法複製;你確定這與你在運行代碼的在線測試環境沒有關係嗎? –

+0

你是對的@DavidBowling,它在Visual Studio上工作,但它在repl.it環境中編譯的方式不同。我使用repl.it來測試我的輸出,而不是每次我想測試某些東西時都必須創建一個VS的新實例。 – mincedMinx

回答

-1

'哈利波特'和'陌生的東西'之間的區別是'陌生的東西'在文本文件中沒有換行符。 '哈利波特'和'飢餓遊戲'似乎還剩'\ r'。

試試這個。

rv = fscanf(fp, "%d;%f;%d;%d;%20[^\r\n]", &(b2read->_isbn), &(b2read->_price), &(b2read->_year), &(b2read->_qty), b2read->_title); 
+0

'\ r'不是換行符。 – Olaf

+0

https://en.wikipedia.org/wiki/Carriage_return – gwangsoo

+0

很難看出這將如何解釋OP的問題。如果在輸入文件中換行符是\ r \ n,那麼錯過的\ r將導致光標移動到當前行的開始位置,而不是前進到下一行的開始位置。有些東西似乎對輸入文件,在線測試環境或兩者都有點腥意。 –

-1

它與'清除輸入緩衝區'無關。 fscanf()根據您指定的內容消耗它。它不需要'清除',它需要正確掃描。不知何故,你在標題中註明了行結束符。更正您的fscanf()格式字符串。

-1

fscanf時,有一點遺漏。您應該在[^\n]中包含\r。 這將告訴fscanf當遇到回車或換行時,您想要停止閱讀。

像這樣:

rv = fscanf(fp, "%d;%f;%d;%d;%20[^\n\r]", &(b2read->_isbn), &(b2read->_price), &(b2read->_year), &(b2read->_qty), b2read->_title);