【问题标题】:Need help reading data from .txt file into an array C++需要帮助将 .txt 文件中的数据读取到数组 C++ 中
【发布时间】:2017-03-22 21:52:24
【问题描述】:
#include <iostream>
#include <fstream>
#include <string>

struct textbook //Declare struct type
{
    int ISBN;
    string title;
    string author;
    string publisher;
    int quantity;
    double price;
};

// Constants
const int MAX_SIZE = 100;

// Arrays
textbook inventory[MAX_SIZE];

void readInventory()
{
    // Open inventory file
    ifstream inFile("inventory.txt");

    // Check for error
    if (inFile.fail())
    {
        cerr << "Error opening file" << endl;
        exit(1);
    }

    // Loop that reads contents of file into the inventory array.

       pos = 0; //position in the array

       while (

        inFile >> inventory[pos].ISBN
               >> inventory[pos].title
               >> inventory[pos].author 
                   >> inventory[pos].publisher
               >> inventory[pos].quantity 
                   >> inventory[pos].price
          )

    {
        pos++;

    } 

    // Close file 
    inFile.close();

    return;

}

你好,

我需要有关此功能的帮助。此函数的目标是从 txt 文件中读入信息并将其读入教科书的数组结构中。 文本文件本身已经按照正确的循环顺序设置。

我的问题是,对于标题部分,一本书的标题可能是多个单词,例如“我的第一本书”。我知道我必须使用 getline 将该行作为字符串输入'title' 数据类型。

我也在某处遗漏了一个 inFile.ignore() 但我不知道如何将它放入一个循环中。

【问题讨论】:

  • 我们可以看一个示例 txt 文件吗?标题和作者有什么区别?
  • 没有文件样本或猜测就无法回答。猜测:您可以通过读取块和 &amp;&amp; 运算符来测试输入有效性:if (inFile &gt;&gt; inventory[pos].ISBN &amp;&amp; infile.ignore() &amp;&amp; std::getline(inFile, inventory[pos].title) &amp;&amp;...) 但最好将所有内容读取为 strings 和 getline,然后根据需要将 strings 转换为数字事实。
  • inventory.txt 文件:20451 我的第一本书 Mark Lusk Pearson Publishing 40 45.34 9780316 Brown Family Mason Victor Little Brown 36 105.99 1349877 Story of My Life Norah M Jones CreateSpace 独立出版平台 20 18

标签: c++ arrays loops struct fstream


【解决方案1】:

假设输入格式为:

ISBN title author publisher price quantity price

即,数据成员是行空白分隔的,您可以为您的 struct textbook 定义 operator&gt;&gt;,它可能类似于:

std::istream& operator>> (std::istream& is, textbook& t)
{
    if (!is) // check input stream status
    {
        return is;
    }

    int ISBN;
    std::string title;
    std::string author;
    std::string publisher;
    int quantity;
    double price;

    // simple format check
    if (is >> ISBN >> title >> author >> publisher >> quantity >> price)
    {
          // assuming you additionally define a (assignment) constructor  
          t = textbook(ISBN, title, author, publisher, quantity, price);

          return is;
    }

    return is;
}

然后,您只需执行以下操作即可读取您的文件:

std::ifstream ifs(...);

std::vector<textbook> books;

textbook b; 
while (ifs >> b)
{
     books.push_back(b);
}

关于getline()的使用请看answer


【讨论】:

  • 如果您对文件格式的假设是正确的,那么这项任务将非常困难,因为很少有作者和书籍只有一个单词名称。解决这个问题远远超出了提出此类问题的任何人的技能。如果您进一步阅读该问题,OP 会谈到知道他们需要使用 getline。这表明要么存在分隔符,OP 因不提供文件样本而感到羞耻,可用于与getline 一起使用。不过,创建使用operator&gt;&gt; 的建议是正确的,并且可以避免这个答案被否决。
  • 感谢您的反馈。这是学校编程任务的一部分,非常“放手”,所以我可能采取了错误的方法。我认为我需要使用 getline 的原因是因为清单文件上的作者和标题行是一个完整的短语,所以我需要将整个字符串放入数组中。我们还得到了一个提示,不要忘记使用 .ignore() 从缓冲区中删除 \n。
  • @C.Timmerman 我明白你的意思。看看std::vector 而不是数组,因为您使用的是 C++,而不是 C。至于 getline(),它可以包含在实现中,但您应该记住,有多种方法可以做您想做的事,即使没有使用它,我的回答就是一个例子。您的问题需要进一步澄清/帮助吗?
猜你喜欢
  • 1970-01-01
  • 2016-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多