将数据从文本文件读取到C++结构中
reading data from a text file into a C++ struct
这是一个程序,它应该从文本文件中读取书籍清单(文本文件的每一行都包含不同的字段,以正确的顺序,用于每本书(。 出于某种原因,例程无限期地继续下去。 "eof"错误捕获似乎没有捕获文件的末尾。 谁能告诉我出了什么问题? 请记住,我是 c++ 世界中真正的新手,但不是编程新手。
一、书籍的结构定义
// 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");
}
你可以这样写程序。
#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");
}
相关文章:
- 如何循环打印顶点结构
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 孤立代码块在结构中引发异常
- 有什么方法可以遍历结构吗
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 无法将结构注册为增强几何体3D点
- 多成员Constexpr结构初始化
- C++将文本文件中的数据读取到结构数组中
- 如何重构类层次结构以避免菱形问题
- 如何在C++中序列化结构数据
- std::vector的包装器,使数组的结构看起来像结构的数组
- 没有为自己的结构调用列表推回方法
- 奇怪的结构&GCC&clang(void*返回类型)
- 在 c++ 中拥有一组结构的正确方法是什么?
- vscode g++链路故障:体系结构x86_64的未定义符号
- C++概念:如何使用'concept'检查模板化结构的属性?