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

C++ 将文本文件中的数据读入C++;结构,c++,struct,C++,Struct,这是一个从文本文件中读取书籍目录的程序(文本文件的每一行包含一个不同的字段,顺序正确,适用于每本书)。出于某种原因,这种例行程序会无限期地继续下去。“eof”错误捕获似乎无法捕获文件的结尾。谁能告诉我怎么了?请记住我是C++世界中真正的新手,但不是编程新手。 首先,书籍的结构定义 // structure definition struct Book { string isbn; string title; string author; string publishe

这是一个从文本文件中读取书籍目录的程序(文本文件的每一行包含一个不同的字段,顺序正确,适用于每本书)。出于某种原因,这种例行程序会无限期地继续下去。“eof”错误捕获似乎无法捕获文件的结尾。谁能告诉我怎么了?请记住我是C++世界中真正的新手,但不是编程新手。 首先,书籍的结构定义

// structure definition
struct Book
{   string isbn;
    string title;
    string author;
    string publisher;
    int quantity;
    float price; };
我已经包含了正确的头文件。下面是似乎不起作用的例行程序。我放了一些cout>newBook.isbn; instream>>newBook.title; instream>>newBook.author; instream>>newBook.publisher; instream>>newBook.quantity; instream>>newBook.price; //将这本书添加到目录中 库存。推回(新书本); } 流内关闭(); showMessage(to_字符串(inventory.size())+“书籍已成功添加到库存”); } //如果打开文件失败 其他的 showMessage(“无法打开文件。请确保文件名正确”); }
您可以这样编写程序

#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");
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//结构定义
结构书
{字符串isbn;
字符串标题;
字符串作者;
字符串发布器;
整数;
浮动价格;
};
无效填充簿(常量标准::字符串和行、簿和簿)
{
std::stringstream是(行);
std::字符串tempStr;
短cnt=0;
while(std::getline(is,tempStr,'))
{
如果(cnt==0)
book.isbn=tempStr;
else if(cnt==1)
book.title=tempStr;
else如果(cnt==2)
book.author=tempStr;
否则如果(cnt==3)
book.publisher=tempStr;
else if(cnt==4)
book.quantity=atoi(tempStr.c_str());
否则如果(cnt==5)
book.price=atof(tempStr.c_str());
++碳纳米管;
}
}
void readInventory(字符串文件名、向量和清单)
{
流内流;
//尝试打开该文件
instream.open(文件名);
//检查文件是否已打开
如果(流内)
{
//弦线;
//逐行读取文件
std::字符串行;
while(std::getline(流内,行'\n'))
{
新书;
fillBook(行、新书);
//将这本书添加到目录中
库存。推回(新书本);
}
流内关闭();

你可以这样写程序

#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");
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//结构定义
结构书
{字符串isbn;
字符串标题;
字符串作者;
字符串发布器;
整数;
浮动价格;
};
无效填充簿(常量标准::字符串和行、簿和簿)
{
std::stringstream是(行);
std::字符串tempStr;
短cnt=0;
while(std::getline(is,tempStr,'))
{
如果(cnt==0)
book.isbn=tempStr;
else if(cnt==1)
book.title=tempStr;
else如果(cnt==2)
book.author=tempStr;
否则如果(cnt==3)
book.publisher=tempStr;
else if(cnt==4)
book.quantity=atoi(tempStr.c_str());
否则如果(cnt==5)
book.price=atof(tempStr.c_str());
++碳纳米管;
}
}
void readInventory(字符串文件名、向量和清单)
{
流内流;
//尝试打开该文件
instream.open(文件名);
//检查文件是否已打开
如果(流内)
{
//弦线;
//逐行读取文件
std::字符串行;
while(std::getline(流内,行'\n'))
{
新书;
fillBook(行、新书);
//将这本书添加到目录中
库存。推回(新书本);
}
流内关闭();

std::coutProblem 1.
instream.eof()
-请参见.PS:从一开始就删除生活故事-并不是将文件中结构的所有数据插入一行,即isbn,标题都是同一行?请检查这个问题,我也会看看文件是如何创建的。您可能有作者或标题中包含多个单词。问题1。
instream.eof()
-请参见.PS:从一开始就删除生活故事-并不是说将文件中结构的所有数据插入一行,即isbn,标题都是同一行?检查一下这个问题,我也会看看文件是如何创建的。你可能有作者或标题中包含多个单词。谢谢你,mystic_coder。你的代码刚刚工作很好!谢谢你,神秘的程序员。你的代码工作得很好!