-
read from file problems
hello
im trying to read data from a file
the file appears like
Code:
1
Dog
Tim
Male 4 4.7 5
0 0 0 0 0
3
Cat
Missy
Female 3 4.0 2
1 0 0 0 0
how can i read these in ?
Code:
ifstream c(file.c_str());
c.getline(id,4);
c.getline(type,50);
c.getline(name,50);
c >> gender;
c >> age;
c >> weight;
c >> total_visits;
while( c >>points[x] )
x++;
what happens is it reads in the Dog entry fine, but when it gets to the Cat entry, it doesnt seem to be reading in the Cat ID which is 3..
?
-
I believe your problem is here:
Code:
while( c >>points[x] )
x++;
You keep reading in the points until nothing is read in, when what you should be doing is reading in only <total_visits> number of points (I'm assuming total_visits is the number of points directly below).
So just change your code to match that methodology and see if that fixes it.
-
mmm?
that while loop is trying to read in these 5 numbers
1 0 0 0 0
-
if those 5 numbers are always five numbers then you can do this:
Code:
x = 0;
while(x < 5)
cin >> points[x++];
because you know the number of points to read in. As jverkoey said
while(cin >> points[x])
x++;
will read in all data until end of file or an error occurs. You only want to read in 5 data points, not the rest of the file, potentially. In your case, when the program has read the five ints, it will attempt to read the three, which it does successfully, but now it tries to put the 3 in points[], and depending on the size of points[] it may overwrite the end of the array, and it may crash the program. If the program doesn't crash there, it will then try to read the C in cat, which isn't an int, and that may well cause the program to crash, if points is an array of ints as opposed to an array of char.
In addition, you have the classic problem that happens when mixing >> and getline() in the same program. It turns out that data doesn't go directly from the keyboard/file to variables. It goes to an intermediate repository affectionately known as the input buffer, where it waits for the input method/operator to retrieve it. The >> operator will leave the terminating char in the input buffer, and ignore it when it goes back next time for another piece of data. getline() removes the terminating char from the input buffer. The default terminating char for getline() is the newline char. In the Dog data, getline() comes before >>. However, after the 5 numbers at the end of Dog are read in by >>, the terminating char for the 5th number is probably a new line char, which will be left in the input buffer. You then go back to the top of a loop, I'm sure, and call getline() to start reading data for Cat. However, the first thing getline() sees is a newline char, so it doesn't put anything in id. It removes the newline char, and then reads in the next line, which is Cat, and everything is okay again, except nothing got put into id.
To fix this problem, it's not a bad idea to always clear the input buffer before any call to getline(). There are a number of ways to do that. They all involve ignore() method. A reasonably effective way is like this:
cin.ignore(256, '\n');
again the second parameter, which is the terminating char, defaults to newline so you could do this if that's what you want.
cin.ignore(256);
There are more sophisticated ways to do the same thing, but this syntax works the vast majority of the time.
-
-
i've tried using the cin.ignore
but i get this error
Code:
error: `ignore' undeclared (first use this function)
??
i've included the header file <iostream>
do i need to include any otheres?
-
Errr... posting the code would help.
Did you type cin.ignore, or just ignore?