I am currently trying to read in a file (ASCII text) and trying to extract some specific information from it. I am using ifstream for input. Currentlly, I am able to get most of the information, but I am a little stumped on the ''best'' (preferablly easiest) way of getting at the last bit. The problem is there are 3 numbers seperated by a ''/''. For example
1/2/3
10/255/12
My problem lies in trying to read just the first number (1 and 10 from the example) I don''t really care about the other two numbers(for now).
I have come up with a basic way of getting the number, but it don''t seem to be very good(more like brute force). My first thought was to peek at the next character and see if it''s a number or a ''/''. If it''s a number, I would get that character, and put it into a string, then keep repeating till I had the ''/''. This would give me a string that had a number. I could keep count of how many times I did that. Then, take the string and loop through and do
sum++((10^index)*number in string);
and just decrement index untill 0.
Seems a bit complex(and messy) to do something that seems so simple. I don''t know if there is a better way, or if there is some option to do this with fstream. I know I could use the getline function and just give it the string buffer and the max letters to read (should be safe with 12) and then the ''/'' as the last param, but then I would have to convert it again. Any suggestions would be helpfull and very much appreciated. Thanks for you time, and sorry about the long post!
This may not be helpful since you said you wanted to do this with fstream, but I know that scanf will do this very easily for you. Here''s an example.
#include <cstdio>// the function you''re doing this stuff in{ FILE* fp = fopen("filename.txt", "rt"); if(fp == NULL) ... // if the file wasn''t opened, do something /* read values until we''ve done so whatever number of times or we haven''t hit the file''s end */ for(int i = 0; i < whatever && !feof(fp); ++i) { int val[3]; scanf(fp, "%d/%d/%d", &val[0], &val[1], &val[2]); ... // stuff } ... // stuff fclose(fp); // close the file}
What scanf does is that it''ll look for certain formatting and extract the appropriate values to the variables you specify. In the above example, each %d represents an integer. After the string passed to scanf you specify the variables the values will be read to. I hope this helps somewhat.
Thanks for the reply. I''ve never used scanf too much, except for getting keyboard input at school. Right now, I have it loop through and count how much data I have in the file(since I don''t know for sure) and then go back to the top and read in the information. Is it possible to read in an entire line using scanf? For example, the first line in the file is just a comment and I don''t need it, so I do this:
file.getline(sTemp,255,''\n'');
If the comment was ''## This is a comment'', the getline would put the whole line into sTemp and move me down to the next line. Is there an equivilent(sp?) using scanf? I was under the impression that in order to read in the comment line I would have to do this:
I haven''t really looked into scanf too much, but right now I''m at work so I can''t do much till I get home. I will keep it in mind, though. Worse comes to worse, I could use fstream for the first part and then scanf for the second.
There is also sscanf. This is just scanf that reads from a string instead of stdin.
So... Read in your data using ifstream if that''s what you want to do. You''re probably reading into a std::string which is fine. Now get the C-style string (uh, c_str I think) and pass it to sscanf to do your parsing.