将文本文件中的数据读入C ++结构

时间:2017-07-22 19:59:31

标签: c++ struct

这是一个程序,它应该从文本文件中读取书籍清单(文本文件的每一行包含一个不同的字段,按正确的顺序,每本书)。由于某种原因,例行程序无限期地继续。 “eof”错误陷阱似乎无法捕获文件的结尾。谁能让我知道出了什么问题?请记住我是c ++世界中真正的新手,但不是编程的新手。

首先,Book的结构定义

// structure definition
struct Book
{   string isbn;
    string title;
    string author;
    string publisher;
    int quantity;
    float price; };

我已经包含了正确的头文件。以下是似乎无法正常工作的例程。我放了一些cout<< “使它成为第1行”;用于查看例程进度的代码,它只是重复从文本文件中获取数据。文本文件中只有6本书,所以它应该很快就读完文本文件。

谢天谢地,感谢您的帮助!

void readInventory(string fileName, vector<Book>& inventory)
{
    ifstream instream;
    // tries to open the file
    instream.open(fileName);

    // checkes if the file is open
    if (instream)
    {
        // string line;
        // reads through the file line by line
        while (!instream.eof())
        {
            Book newBook;

            // read book from text file
            instream >> newBook.isbn;
            instream >> newBook.title;
            instream >> newBook.author;
            instream >> newBook.publisher;
            instream >> newBook.quantity;
            instream >> newBook.price;

            // add this book to inventory
            inventory.push_back(newBook);
        }

        instream.close();
        showMessage(to_string(inventory.size()) + " books were successfully added to inventory");
    }

    // if failed to open the file
    else
        showMessage("Cannot open the file. Make sure the file name is correct");
}

1 个答案:

答案 0 :(得分:0)

你可以写这样的程序。

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <queue>
#include <fstream>
#include <sstream>

using namespace std;
// structure definition
struct Book
{   string isbn;
    string title;
    string author;
    string publisher;
    int quantity;
    float price;

};

void fillBook(const std::string& line,Book& book)
{
    std::stringstream is(line);
    std::string tempStr;
    short cnt = 0;

    while(std::getline(is, tempStr, ' '))
    {
        if(cnt ==0)
            book.isbn =tempStr;
        else if(cnt ==1)
            book.title = tempStr;
        else if(cnt ==2)
            book.author = tempStr;
        else if(cnt ==3)
            book.publisher= tempStr;
        else if(cnt ==4)
            book.quantity = atoi(tempStr.c_str());
        else if(cnt == 5)
            book.price = atof(tempStr.c_str());

        ++cnt;
    }

}

void readInventory(string fileName, vector<Book>& inventory)
{
    ifstream instream;
    // tries to open the file
    instream.open(fileName);

    // checkes if the file is open
    if (instream)
    {
        // string line;
        // reads through the file line by line
        std::string line;
        while (std::getline(instream, line, '\n'))
        {
            Book newBook;

            fillBook(line,newBook);

            // add this book to inventory
            inventory.push_back(newBook);
        }

        instream.close();
        std::cout<<(to_string(inventory.size()) + " books were successfully added to inventory");
    }

    // if failed to open the file
    else
        std::cout<<("Cannot open the file. Make sure the file name is correct");
}