I want to read some data from a text file of unknown length using C++. If the file
is space delimited this is quite easy. I can use infile to break each line up
into tokens divided by the spaces and then load them into temp variables.
If our data file is structured as follows:
I can use the following code to load the data and print each value to the terminal:
Which results in:
If more data is included on each row it is easy to expand this to add in more variables, which
can be operated on however you wish now that they are in memory. But what if our data is
not space delimited?
Using the file commas.txt:
Will result in us getting no data printed to the terminal, as 1,4 is identified as a
single token, but is an invalid integer and so is ignored.
So we need to use a slightly different approach,
which explicitly defines the comma as our delimiter: