【问题标题】:How to clear input buffer when scanning values from a text file in C?从C中的文本文件中扫描值时如何清除输入缓冲区?
【发布时间】:2017-07-21 01:24:06
【问题描述】:
#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");
    }
}

文本文件里面是:

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 “换行符”?

【问题讨论】:

  • 这段代码看起来不错,在我的系统上按预期工作。无法复制;您确定这与您运行代码的在线测试环境无关吗?
  • 你说得对@DavidBowling,它在 Visual Studio 上工作,但在 repl.it 环境中编译不同。我使用 repl.it 来测试我的输出,而不是每次我想测试某些东西时都必须创建一个新的 VS 实例。

标签: c printf text-files scanf input-buffer


【解决方案1】:

它与“清除输入缓冲区”无关。 fscanf() 根据您指定的内容使用它。它不需要“清除”,它需要正确扫描。不知何故,你在标题中得到了行终止符。更正您的 fscanf() 格式字符串。

【讨论】:

    【解决方案2】:

    fscanf 时缺少一点东西。您应该在[^\n] 中包含\r。 这将告诉fscanf,当遇到回车或换行时,您想停止阅读。

    像这样:

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

    【讨论】:

      【解决方案3】:

      “哈利波特”和“怪奇物语”的区别在于“怪奇物语”在文本文件中没有换行符。 “哈利波特”和“饥饿游戏”似乎已经离开了“\r”。

      试试这个。

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

      【讨论】:

      • \r 不是换行符。
      • 很难看出这将如何解释 OP 的问题。如果输入文件中的换行符是\r\n,那么错过的\r 将导致光标移动到当前行的开头,而不是前进到下一行的开头。输入文件、在线测试环境或两者都有问题......
      猜你喜欢
      • 2012-05-23
      • 1970-01-01
      • 2011-12-15
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      相关资源
      最近更新 更多