C++ csv 文件无法正确加载到矢量,它似乎知道对象在那里但不显示它们
C++ Csv file doesn't load to vector properly, it seems to know the objects are there but does not display them
我一直在尝试将*.csv
文件读取到vector
,但是它似乎不起作用,它的行为是有对象但不显示它们,如果它们是空的,它就会起作用。
这是加载*.csv
文件的函数。
void loadShoes()
{
fstream shoes;
shoes.open("shoes.txt", ios::in);
string shoeId;
string valueId;
while (getline(shoes, shoeId, ','))
{
getline(shoes, valueId, ',');
ShoeMap[shoeId] = valueId;
if (shoeId == "ShoeLaceStyle")
{
thefootwear.addShoe(ShoeMap);
};
}
}
此代码来自调用函数以加载到vector
中然后在simple UI
中显示的main
。
else if (userInput == 3)
{
for (int i = 0; i < thefootwear.vecNewShoe.size(); i++)
{
cout << i + 1 << " - " << thefootwear.vecNewShoe[i]->getShoeID() << "n";
}
cout << "nWhich Shoe would you like to view from the store?n";
cin >> userInput1;
cout << "ShoeID - " << thefootwear.vecNewShoe[userInput1 - 1]->getShoeID() << "n" << "ShoeName - "
<< thefootwear.vecNewShoe[userInput1 - 1]->getShoeName() << "n" << "ShoeType - "
<< thefootwear.vecNewShoe[userInput1 - 1]->getShoeType() << "n" << "ShoeSize - "
<< thefootwear.vecNewShoe[userInput1 - 1]->getShoeSize() << "n" << "ShoeSoleStyle - "
<< thefootwear.vecNewShoe[userInput1 - 1]->getShoeSoleStyle() << "n" << "ShoeColour - "
<< thefootwear.vecNewShoe[userInput1 - 1]->getShoeColour() << "n" << "ShoeLaceStyle - "
<< thefootwear.vecNewShoe[userInput1 - 1]->getShoeLaceStyle() << "n";
}
这是在Footwear
类中添加具有所需variables
的new shoe
method
和function
。
Shoe* Footwear::addShoe(map<string, string> ShoeMap)
{
Shoe* newShoe = new Shoe(ShoeMap["ShoeID"], ShoeMap["ShoeName"], ShoeMap["ShoeType"], ShoeMap["ShoeSize"], ShoeMap["ShoeSoleStyle"], ShoeMap["ShoeColour"], ShoeMap["ShoeLaceStyle"]);
vecNewShoe.push_back(newShoe);
return newShoe;
}
当我运行该程序时,它显示*.csv
文件中当前正确的shoes
数,但是它无法显示其相应的Shoe ID
因此我无法访问与Shoes
相关的变量。
说到您的需求而不是您的具体问题:考虑不要编写自己的自定义 CSV 解析器,而是使用预先存在的 C++ CSV 解析器库。例如,GitHub上有几个流行的。
这还有一个额外的好处,那就是对意外的输入设计(例如带引号的数字(更加可靠。
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 使用迭代器时如何访问对象在向量中的位置?
- "main"函数堆栈中的对象在第一个任务运行时被覆盖 (FreeRTOS)
- 准确了解对象在内存中的映射方式
- 为什么我的 constexpr 对象在我的函数中不是 constexpr?
- 类对象在 c++ 中是否具有数据类型?
- 在函数中声明的 ifStream 对象在发布模式下不起作用
- 堆分配对象中的堆栈对象在 c++ 中在哪里分配?
- 临时对象在C++中是不可避免的吗?
- scanf( "%d" , array[i] + 1)是什么意思?为什么+1在那里?
- ::grpc::ServerReaderWriter 对象在另一个线程中一段时间后无法调用
- 在 if 语句中声明时如何访问类对象?(在 C++ 中)
- delete如何知道对象在内存中的起始位置
- C++ csv 文件无法正确加载到矢量,它似乎知道对象在那里但不显示它们
- 如何修复对象在释放后可能修改的malloc错误
- 是否可以将多个结构作为一个数据包存储在一个函数中,然后传递给其他函数并在那里提取?
- 对象在称为指向父类的指针时会丢失其成员的数据
- 类对象在 int main() 中'undefined'
- Fread C++ in C#.我知道我需要字节缓冲区,但并不完全在那里
- 我为Pair对象分配内存,但未能将对象放置在那里