将文件中的数据读取到struct中,并将struct添加到向量中(以创建structs的向量)



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

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

我有以下代码,但我对在(…(中放入什么感到困惑,也就是说,如果我有正确的逻辑。

#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;
}
}

main中,您调用两次读取函数:

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 $ 

我建议为operator>>operator<<:添加过载

#include <iostream>
#include <string>
#include <vector>
struct Part {            // I renamed it because it only holds info about one part
std::string partNum;
char partClass;
int stock;
float cost;
};
std::istream& operator>>(std::istream& is, Part& p) {
return is >> p.partNum >> p.partClass >> p.stock >> p.cost;
}
std::ostream& operator<<(std::ostream& os, const Part& p) {
return os << p.partNum << ' ' << p.partClass << ' ' << p.stock << ' ' << p.cost;
}

这使得提取或打印一个Part变得容易:

bool readFile(std::vector<Part>& pVector){
std::ifstream inputFile("parts.txt");
if(inputFile) {
Part tmp;
while(inputFile >> tmp)        // extract one Part at a time using operator>>
pVector.emplace_back(tmp);
return true;
} else {
std::cout << "Error reading file!" << std::endl;
return false;
}
}

最新更新