Sep 24, 2013 at 2:51am UTC
I have to parse a text file and each row is completely different from the previous one. I know how to parse a file when each row has the same format but I have no idea how to do it this way. My file has this format.
String Float
String Int
String float float float
String float float float
float
string
float float float
int.
Any help? I really have no idea where to start.
Thank you.
Sep 24, 2013 at 3:29am UTC
What are you supposed to do with it after you parse it? If your goal is just to parse and do nothing else...it doesn't make sense.
Sep 24, 2013 at 3:31am UTC
I have to store the values and use them later.
I guess my question is ho to read line by line and store each line differently.
Sep 24, 2013 at 9:47am UTC
so different line uses different input
why not use getline?
it takes string per line
if you need to divide them into array of words,
then divide them up later on,,,
if there is a pattern for this seemly random input then you can use something more efficient, well that all depends on how will you use the input ...
Sep 24, 2013 at 10:21pm UTC
No, there is no pattern.
So I would get the lines using:
while (!file.eof())
getline(file, line);
but then every line would get stores in the variable line, so at the end of the loop I would just end up with the last line right? How do I store each line separately?
Thanks for the replies !
Sep 24, 2013 at 10:22pm UTC
You could push the line to the back of a vector of strings ;)
Sep 24, 2013 at 10:29pm UTC
okay ! and is there a way to do it without using vectors? Like just variables? I feel it would be easier to use them later like that
Sep 24, 2013 at 10:32pm UTC
The easiest way to do it is using std::vector . Alternatives are more challenging and easier to get wrong. Also, std::vector is "just variables".
Last edited on Sep 24, 2013 at 10:33pm UTC
Sep 24, 2013 at 10:41pm UTC
Okay, I am going to try your way.
Thanks :)