|
From: Vince P. <Vin...@en...> - 2002-01-10 16:01:12
|
Preamble
Hello I'm learning C++ and following examples in a text I found a good =
book on the web. This is a Chapter 9 exercise (of 30 + chapters) and up to =
now I have had no problems I could not solve.
I'm not yet familiar with the debugger (perhaps I should be working on =
that).
Problem:
I'm trying to read in a file of data using the following code and data =
file (below):
#include <iostream.h>=20
#include <stdlib.h>
#include <fstream.h>=20
int main()
{
ifstream infile("test.dat", ios::in | ios::nocreate);
int count =3D0;
=20
if (!infile.good()) {
cout << "Sorry ! Couldn't open the file." << endl;
exit(1);
}
=20
int pclass;
=20
infile >> pclass;
=20
while(pclass !=3D -1) {
count++;
int a1, a2, a3, a4, a5;
infile >> a1 >> a2 >> a3 >> a4 >> a5;
infile >> pclass;
}
=20
cout << "I think there were " << cout=20
<< "records in that file." <<endl;
=20
return 0;
}
test.dat:
1 0 0 0 0 0 0
1 1 0 0 0 0 0
1 0 1 0 0 0 0
1 0 0 1 0 0 0
1 0 0 0 1 0 0
1 0 0 0 0 1 0
1 0 0 0 0 0 1
1 1 0 1 0 0 0
1 0 1 1 0 0 1
1 0 1 1 1 0 0
1 0 1 1 1 1 0
1 0 1 0 1 1 0
0 0 0 0 0 0 0
0 1 0 0 0 0 0
0 0 1 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 1 0 0
0 0 0 0 0 1 0
0 0 0 0 0 0 1
0 1 0 1 0 0 0
-1
The code compiles and link with no errors but does not give me any output =
and does not allow any input; simply a console window with a blinking =
curser.
I have the data file in the same directory as the executable.
Oh yes, I'm working under WinNT.
Any assistance/advice towards a solution is greatly appreciated.
Thank you,
Vince.
|