I'm knew to file I/O and was wondering if I could get help for my scenario:
I have a bunch of html files, and I want my program to open each one, and find the number that comes after "Number of hits:" in line 67 (for each one). I would like for an array like this in the end:
[FILENAME, number-of-hits, FILENAME2, number-of-hits, ....]
All replies appreciated. Thanks.
What do you need help on?
How can I go to a specific line in the file, search for the string "Number-of-hits:" in that line, and then take the number that comes after "Number-of-hits" and store that into an array, for each file.
>>How can I go to a specific line in the file
You could call getline() until you get to the line you're looking for. Alternately, you could read a single character at a time, until you reach a '\n', which would signal a new line.
>>search for the string "Number-of-hits:" in that line
Store the line in a std::string, and call find().
>>take the number that comes after "Number-of-hits"
If the first part of the line is "Number-of-hits", call substr() using the length of "Number-of-hits" as the offset. That'll get you the number as a string. Then you can call atoi(sub.c_str()), or use a stringstream, or sscanf, or anything else you choose to convert the string to an integer.
>>and store that into an array, for each file.
Store it like you would in any other array...