从矢量中的文件中读取数据

Read data from a file in vector

本文关键字:读取 数据 文件      更新时间:2023-10-16

我的任务是将数据从文件中读取到向量中:

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()并不能保证读取会成功。 相反,我们尝试读取,然后测试输入流是否处于失败状态。 循环之后,我们可以(如果需要(实际检查我们是否到达了文件末尾,而不是一些失败情况 - 我在这个简单的程序中省略了这一点。