Reputation: 81
I am currently trying to read a textfile(item.txt) which is in this format itemId:itemDescription:itemCategory:itemSubCategory:amountPerUnit:itemQuantity:date what I want is to read the textfile and store it inside a vector according to my expected output.
Upvotes: 0
Views: 741
Reputation: 409136
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
}
Upvotes: 2