I want to write a program that reads lines of text from a file. Each line contains several strings, one integer and one double value. The number of lines is not known in advance (i.e., the program needs to read lines until end of file) and the length of each line is not known as well. The while loop checks if the line ordered in following manner:
-- string -- integer -- double --- string
Cat. 3: 18.00 Kr. [ Tvål ]
Cat. 1: 14.50 Kr. [ Äppelmos Bob ]
Cat. 1: 12.00 Kr. [ Coca Cola 2 lit. ]
Cat. 1: 18.00 Kr. [ Kroppkakor 4 st. ]
The problem is the last string contains several spaces, so the program does not take it as a one entire string. The last string is accepted like several strings and I see on my screen only Cat. 3: 18.00 Kr. instead of whole list of lines.
I tried to handle program like this:
double doubleValue;
int intValue;
string str1, str2, str3;
ifstream infile("Register.txt", ifstream::in);
while (infile >> str1 >> intValue >> str2 >> doubleValue >> str3)
{
cout << intValue << " " << doubleValue << endl;
}
Thanks in advance.
std::regex
) to get the data from it - solving things like 'one ore more spaces' is bascially what regex is maded for – stijn:
and for the[
and]
characters. That will much more reliably delimit your input fields. – Edward