February 18th, 2002, 05:15 AM
ifstream - text input into char and int
I'm having difficulty correctly parsing data from a text file. The text file is roughly divided into two colums. The first column contains a char, the second column an int. I defined a variable inFile as type ifstream. I can pull all the data line by line using:
while (getline(inFile, s))
Unfortunately, I don't have a clue as to how I can correctly extract the required information from this point.
I suppose its possible to pull the data one char at a time.
inFile >> chara >> intb;
Unfortunately, when I do this, my program spits out erroneous data.
Am I approaching this problem correctly or am I way off and there is a simpler, cleaner way to parse data from a text file? Any pointers would be helpful.
February 18th, 2002, 11:05 AM
You can always try to break each string into tokens. This is assuming you know what is being used as the file's delimiter.
Check out the strtok function. I would play around with it a good deal before using it in a real program though because it can be dangerous if used incorrectly. For the safest path, copy the variable to a temp variable and perform the strtok on it.