从矢量中的文件中读取数据
Read data from a file in vector
我的任务是将数据从文件中读取到向量中:
21000 Landhau Nolte brown
19000 Modern_fit Hoeffner magnolie
14700 Pure_Style Wellmann black
这是我的尝试,但回推不起作用。我已经在堆栈溢出中查看了一些示例,但不知何故它不起作用。
functions.h
:
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
struct Kitchen {
double price;
string name;
string manufacturer;
string color;
};
main.cpp
:
#include "functions.h"
int main(){
vector<Kitchen> Kitchens;
fstream myFile;
myFile.open("kitchen.txt", ios::in);
if (myFile.is_open()) {
while (!myFile.eof()) {
double price;
string name;
string manufacturer;
string color;
myFile >> price >> name >> manufacturer >> color;
Kitchens.push_back(price, name, manufacturer, color);
}
myFile.close();
}
else cout << "not opened." << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
我做错了什么?
结构是一种聚合类型,但要将结构对象推送到结构向量中,即使它可以是临时的,也必须创建一个:
#include <iostream>
#include <vector>
using namespace std;
struct Kitchen {
double price;
string name;
string manufacturer;
string color;
};
int main() {
std::vector<Kitchen> kt;
kt.push_back(Kitchen{21000,"Landhau","Nolte","brown"});
return 0;
}
此外,只需稍作修改并在厨房结构中使用参数化构造函数,您就可以避免push_back的内部复制/移动操作并直接使用emplace_back。
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
struct Kitchen {
double price;
string name;
string manufacturer;
string color;
Kitchen(double p,
const string& n,
const string &m,
const string &c):price(p),name(n),manufacturer(m),color(c) {}
};
int main(){
vector<Kitchen> Kitchens;
fstream myFile;
myFile.open("kitchen.txt", ios::in);
if (myFile.is_open()) {
while (!myFile.eof()) {
double price;
string name;
string manufacturer;
string color;
myFile >> price >> name >> manufacturer >> color;
Kitchens.emplace_back(price, name, manufacturer, color);
}
myFile.close();
}
else cout << "not opened." << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
你做错了什么,就是试图将 4 个随机变量传递给一个实际上只接受一个变量的push_back
,而这个变量是向量的值类型。
Kitchen k;
myFile >> k.price >> k.name >> k.manufacturer >> k.color;
Kitchens.push_back(k);
push_back
需要Kitchen
。该代码提供了Kitchen
片段。尝试一下emplace_back
或实例化一个临时Kitchen
以传递到push_back
。
让我们正确地做到这一点。
从标头开始,不要包含超出该标头中定义所需的内容,也不要将std
中的所有标识符导入全局命名空间。
#include <string>
struct Kitchen {
double price;
std::string name;
std::string manufacturer;
std::string color;
Kitchen(double price, std::string name,
std::string manufacturer, std::string color)
: price{price}, name{name}, manufacturer{manufacturer}, color{color}
{}
};
我添加了一个简单的构造函数,因为我们稍后emplace_back
需要它。
现在实现main()
。 对于可重现的示例,最好从字符串流中读取,而不是不得不弄乱文件:
#include <vector>
#include <sstream>
#include <iostream>
int main()
{
std::vector<Kitchen> kitchens;
std::istringstream file("21000 Landhau Nolte brownn"
"19000 Modern_fit Hoeffner magnolien"
"14700 Pure_Style Wellmann blackn");
{
double price;
std::string name;
std::string manufacturer;
std::string color;
while (file >> price >> name >> manufacturer >> color) {
kitchens.emplace_back(price, name, manufacturer, color);
}
}
std::clog << "Read " << kitchens.size() << " kitchens from inputn";
}
请注意,此处!eof()
并不能保证读取会成功。 相反,我们尝试读取,然后测试输入流是否处于失败状态。 循环之后,我们可以(如果需要(实际检查我们是否到达了文件末尾,而不是一些失败情况 - 我在这个简单的程序中省略了这一点。
相关文章:
- 如何在ros2中从rosbag2读取数据
- OpenCV 和从 OutputArrays 或 (Mat) 读取数据
- 使用C++从磁卡读卡器MT188读取数据
- HDF5 简单读取数据集失败
- 嵌套结构,从由空行分隔的文件中读取数据
- 不要从输入队列套接字读取数据
- 从文件中读取数据,操作员>>
- 如何从excel文件中读取数据并存储到变量中?
- C++:从文件中读取x y数据会产生无限循环吗
- 无法通过FT232RL读取数据
- 如何从QNetworkReply读取数据?
- C++ 逐行从文件(包含空格)读取数据
- 通过显示一行星号来创建条形图,该条形图将读取数据的输入文件
- 我们可以从 TXT 文件中读取数据并使用 C 和 C++ 将其保存到 SQL 数据库吗?
- 从多个 tcp 连接读取数据
- 从文本文件中读取数据并删除所有换行符空格,并在 C++ 控制台中显示
- Qt TCP服务器不从客户端读取数据
- 累积从 C++11 中的文件中读取数据的整数
- 从 txt 文件中读取数据的最简单方法
- 从 txt 文件中读取数据的 Getline