Inputting string&int from file

Hello everybody,

I am trying to do an assignment for class and the first thing I need to do is read some data from a file. The data is in the following format with two rows but can have many columns:
1,CLEAR,0,CLEAR,15,CLEAR,4...
2,BOULDER,2,CLEAR,9,BOULDER,9

so the first number is the row number and there are only two but the rest are supposed to be pairs of (string,int).

I am trying to use cin or ifstream to read the file and store the data but I am stuck on how to do that.

I tried using something like

struct Pairs{
string name;
int num;
};

But I had trouble reading the data without the commas.
I also tried using maps to make pairs like that but again, I am having trouble reading the pairs without the commas and separating the string and int.

Any help or suggestions would be greatly appreciated.

Thanks
Last edited on
You've shown line 1 ending in ellipses and line 2 without - I am assuming that the ellipses at the end of line one is by way of illustration that a line can have varying number of 'columns' and that line 2 is the accurate representation of the file format.
If above assumption is correct, then count the number of commas on any give line and number of pairs on any line is half the number of commas (the first comma corresponding to line number is discarded and the line ends without a comma, so they cancel each other out).
Once you have the number of pairs on a line you can set up a loop to read the pairs for n-1 times with the comma delimiter and for the the last pair you just read off the pair.first with the comma delimiter and the pair.second directly since it doesn't end in a comma, where n is the number of pairs on the line
Thanks for the reply. Your assumption is correct.
Also, is there a function that can count # of commas on a line. I am not familiar with that. How do you suggest I go about reading the file?

Do you think just doing:
while(cin){

}

is enough? If this is enough, then how can I distinguish between the two rows? Is there another way?

Also, what is a comma delimiter?

Sorry, I am a noob at C++...

Really appreciate your help.
Thanks
cin is type aware.
so

int i;
string s;

cin >> i; //reads an integer
cin >>s; //reads a string

cin handles the work for you.

cin is analagous to file >>
so if you did

ifstream inf;
inf.open("filename");

inf >> i;
inf >> s;
is more or less the same thing.

the trick is that you need to deal with the delimiters.
Personally, I read the whole file into ram and beat it to death with a stick rather than try to cook up convoluted reading routines. I am old, cranky, and lazy, so I would read the whole blasted thing into a char *, replace every comma with zero, and then crunch through it with a loop using 13 or 10 or whatever your end of line delimiter is for your OS. There are other ways, but dealing with small annoying text files, I have found this approach to be the simplest way to hack out a quick solution.



To count any char in a string you can use std::count.
http://www.cplusplus.com/reference/algorithm/count/
You can use std::getline to read the comma separated elements.
http://www.cplusplus.com/reference/string/string/getline/

To check if the element is a number, use std::isdigit.
http://www.cplusplus.com/reference/cctype/isdigit/

To convert the element(std::string) to an int, you can use std::stringstream.
http://www.cplusplus.com/reference/sstream/stringstream/
Thanks everyone! Your suggestions really helped!!
Topic archived. No new replies allowed.