我目前正在尝试读取这个格式为itemId的文本文件(item.txt):itemDescription:itemCategory:itemSubCategory:amountPerUnit:itemQuantity:date我想要的是读取文本文件并根据我的预期输出将其存储在向量中。
You are on the right way, by using std::getline
. But instead you should read the file line by line, and then put the full line in an std::istringstream
, and then you can use std::getline
to tokenize the line.
You can't use the normal input operator >>
as that separates on space.
Example
while (std::getline(readFile, line))
{
std::istringstream iss(line);
std::string temp;
std::getline(iss, temp, ':');
itemId = std::stoi(temp);
std::getline(iss, itemDescription, ':');
std::getline(iss, itemCategory, ':');
std::getline(iss, itemSubCategory, ':');
std::getline(iss, temp, ':');
amountPerUnit = std::stod(temp);
std::getline(iss, temp, ':');
quantity = std::stoi(temp);
std::getline(iss, date, ':');
// Create object and add it to the vector
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.