gpt4 book ai didi

c++ - 从文件中读取数据到struct并将struct添加到 vector (以创建struct的 vector )

转载 作者:行者123 更新时间:2023-12-03 07:07:10 25 4
gpt4 key购买 nike

之前已经问过这个问题,但是其他问题/答案使用的是我在C++中还不熟悉的概念。

我需要将文件中的数据读取到结构 vector 中。

我有以下代码,但是对于要放入(....)的内容感到困惑,即如果我具有正确的逻辑。

#include <iostream>
#include <string>
#include <fstream>
#include <vector>

struct Parts{
std::string partNum;
char partClass;
int stock;
float cost;
};

bool readFile(std::vector <Parts>&);
int displayMenu();

int main(){
std::vector <Parts> pVector;

readFile(pVector);

if (!readFile(pVector)){
std::cout << "Error reading file!" << std::endl;
}
else{
displayMenu();
}
return 0;
}

bool readFile(std::vector <Parts> &pVector){
std::ifstream inputFile("parts.txt");
if (inputFile.fail()){
std::cout << "Error reading file!" << std::endl;
return false;
}
else{
while (....){
pVector.emplace_back(....);
}
return true;
}
}

来自文件的示例行:

P-42936 A 18 129.79
P-43179 A 47 35.60
P-43264 B 31 103.81
P-43367 B 5 32.39
P-43378 A 46 6.38
P-43622 A 10 155.36

最佳答案

你想要那个 :

bool readFile(std::vector <Parts> &pVector){
std::ifstream inputFile("parts.txt");

if (inputFile.fail()){
std::cout << "Error reading file!" << std::endl;
return false;
}
else {
Parts part;

while (inputFile >> part.partNum >> part.partClass >> part.stock >> part.cost)
pVector.emplace_back(part);
return true;
}
}

主要是您两次调用read函数:

   readFile(pVector);

if (!readFile(pVector)){


很可能必须删除第一个电话

为Parts定义 operator >>而不是让代码在readFile中执行此操作也可能很有趣

因此:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

struct Parts{
std::string partNum;
char partClass;
int stock;
float cost;
};

std::istream & operator >>(std::istream & is, Parts & part) {
if (is >> part.partNum >> part.partClass >> part.stock)
is >> part.cost;

return is;
}

bool readFile(std::vector <Parts>&);
//int displayMenu();

int main(){
std::vector <Parts> pVector;

if (!readFile(pVector)){
std::cout << "Error reading file!" << std::endl;
}
else{
//displayMenu();
// to check, of course operator << can be defined too
for (auto p : pVector)
std::cout << p.partNum << '/' << p.partClass << '/' << p.stock << '/' << p.cost << std::endl;
}
return 0;
}

bool readFile(std::vector <Parts> &pVector){
std::ifstream inputFile("parts.txt");

if (inputFile.fail()){
std::cout << "Error reading file!" << std::endl;
return false;
}
else {
Parts part;

while (inputFile >> part)
pVector.emplace_back(part);
return true;
}
}

编译与执行:
pi@raspberrypi:/tmp $ g++ -Wall r.cc
pi@raspberrypi:/tmp $ cat parts.txt
P-42936 A 18 129.79
P-43179 A 47 35.60
P-43264 B 31 103.81
P-43367 B 5 32.39
P-43378 A 46 6.38
P-43622 A 10 155.36
pi@raspberrypi:/tmp $ ./a.out
P-42936/A/18/129.79
P-43179/A/47/35.6
P-43264/B/31/103.81
P-43367/B/5/32.39
P-43378/A/46/6.38
P-43622/A/10/155.36
pi@raspberrypi:/tmp $

关于c++ - 从文件中读取数据到struct并将struct添加到 vector (以创建struct的 vector ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61448546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com