If I open a new file for input, and I call input >> listSize; outside of a while loop and then continue calling input >> anothervariable will it automatically progress through the file or will it read the first line again?
Example:
input >> listSize;
BaseStudent* studentlist = new BaseStudent[listSize.atoi()];
while (!input.eof())
{
input >> anothervariable; // I want this to start on the second line, not the first
}
The input file looks like this and we can code to the pattern (ignore the extra blank lines):
12
Bunny, Bugs
Math 90 86 80 95 100 99 96 93
Schmuckatelli, Joe
History 88 75 90
Dipwart, Marvin
English 95 76 72 88
Crack Corn, Jimmy
Math 44 58 23 76 50 59 77 68
Kirk, James T.
English 40 100 68 88
Lewinsky, Monica
History 60 72 78
Nixon, Richard
English 35 99 70 70
Lincoln, Abraham
History 59 71 75
Clinton, William
Math 43 55 25 76 50 58 65
Duck, Donald
English 34 100 65 65
Duck, Daffy
History 55 70 70
Bush, George
Math 44 54 29 75 50 55 60
Others have already pointed out that the answer to your question is “yes”, so I won’t worry about that part.
As for the rest, I think I’d write it a bit differently. Your file obviously represents structured data, and I’d write the code to reflect that fact reasonably directly. I’d start by defining a structure reflecting the data in the file:
Then, I’d write a function to read one of those items from the file:
In C++, however, it’s really easier to just read whatever amount of data is there, than to prefix the data with the count. Given that count is present, the easiest thing to do is probably to just read and ignore it:
Then we can read the real data pretty easily:
…or, we can use C++’s handy-dandy
istream_iterators to simplify the code even more:That’s it — it defines the vector and initializes it from the input file, all in one (fairly) simple operation.