简体   繁体   English

从文本文件读取数据到C ++结构

[英]reading data from a text file into a C++ struct

This is a program which is supposed to read an inventory of books from a text file (each line of the text file contains a different field, in the correct order, for each book). 这是一个应该从文本文件中读取书籍清单的程序(文本文件的每一行都以正确的顺序为每本书包含一个不同的字段)。 For some reason the routine continues indefinitely. 由于某种原因,例程将无限期地继续。 The "eof" error trapping seems to not catch the end of file. “ eof”错误陷阱似乎无法捕获文件末尾。 Can anybody give me an idea what is wrong? 谁能告诉我什么地方出了问题? Please remember I am a true newbie in the c++ world, but not a newbie to programming. 请记住,我是c ++领域的真正新手,但不是编程的新手。

First, the structure definition for Book 一,Book的结构定义

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

I have included the proper header files. 我已经包含了正确的头文件。 Below is the routine that seems to not be working. 以下是似乎无效的例程。 I put in some cout << "made it to line 1" endl; 我放了一些提示<<“到达第1行” endl; code to see the progress of the routine, and it simply repeats fetching data from the text file. 代码以查看例程的进度,并且只需重复从文本文件中获取数据即可。 The text file has just 6 books in it, so it should end reading through the text file rather quickly. 文本文件中只有6本书,因此应该很快结束文本文件的阅读。

Thank you, kindly, for any assistance! 谢谢您的协助!

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");
}

You can write program like this . 您可以编写这样的程序。

#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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM