mahurshi@gmail.com wrote:[color=blue]
> I am trying to read a file full of numbers followed by spaces (and then
> do some cool stuff with it)
>
>
> My input file looks like this
>
> 1 0 1 0 1 0 1
> 1 1 0 0 1 1 0
>
>
> (the number of columns is arbitrary, the number of lines is arbitrary.
> but all lines have same number of columns)
>
>
> Now, when I run my program (compiled using g++) I get "Aborted" at the
> very end.
> I get all my outputs written to my output file perfect. The program is
> just crashing after it did its job.
>
> Now, I traced thru the thing I found out it is crashing because it is
> going thru one more iteration (when it is not supposed to) .. see "HEY
> HEY HEY"
>[/color]
It must be commonest newbie mistake.
[color=blue]
> while (!inputfile.eof())
> {
> cout << "HEY HEY HEY\n";
> /* clear the stiumuls vector */
> stimulus.clear();
>
> /* read first line of input */
> inputfile.getline(buf, 255);[/color]
This is the wrong way to read a file
[color=blue]
> while (inputfile.getline(buf, 255))
> {
> cout << "HEY HEY HEY\n";
> /* clear the stiumuls vector */
> stimulus.clear();
>[/color]
This is the right way.
eof() tells you why the last read failed. It does not tell you that the
next read is going to fail. That is why you are going round the loop one
too many times.
john