Vince Pileggi escribió:
> 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>
> #include <stdlib.h>
> #include <fstream.h>
>
> int main()
> {
> ifstream infile("test.dat", ios::in | ios::nocreate);
> int count =0;
>
> if (!infile.good()) {
> cout << "Sorry ! Couldn't open the file." << endl;
> exit(1);
> }
>
> int pclass;
>
> infile >> pclass;
>
> while(pclass != -1) {
> count++;
> int a1, a2, a3, a4, a5;
> infile >> a1 >> a2 >> a3 >> a4 >> a5;
> infile >> pclass;
> }
>
> cout << "I think there were " << cout cout << "I think there were";
> << "records in that file." <<endl; cout << "records in file" << endl;
>
> 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.
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
Well, I am too learning, but I did a couple of programs dealing with files. I think your problem is the program doesn't open the archive. The code is right (I added a correction in a line where
you write cout twice in aline, that's not correct, I think), it must work, but it doesn't. Try little modifications: the line if(!infile.good()), change it with if(!infile) and prove if this works
(it shall). If not, remove this check.
Of course, you must write the full path to the file you want write, or it will search it in the folder C:/DevC++/
***NOTE: Don't pay attention to this, at first, but that part of
infile >> pclass;
while(pclass != -1) {
count++;
int a1, a2, a3, a4, a5;
infile >> a1 >> a2 >> a3 >> a4 >> a5;
infile >> pclass;
}
perhaps can do either a bad working, first line would enter the first number from infile, the while compares the first number, then read from infile the second to a1, the third to a2[...], the
sixth to a5 (it enters the first number of the second line where you want the last of the current) without comparing them to -1, then it takes the second number of the second row and compares it
to -1, then the third number into the new a1, etc. If it works with the first modification, check this. So it would be fine you add a line for output the number entered in a1 to a5 each cicle.
**ANOTHER NOTE: When I want read from a file and write in another, it only works if the writing is performed inside a function, out from nest the open outfile togheter with open infile. If not, it
returns program error it must be closed by windows and restarted. Perhaps you don't find this problem in winNT, I use windows 2000, and the console programs doesn't work perfectly.
|