Reputation: 43
So my problem is, when i read the file the "ki" , "kivel" and the "meddig" variables are good, but the "mettol" variable seems like it is disappeared.
struct Haboru {
string ki;
string kivel;
int mettol;
int meddig;
};
int main()
{
Haboru haboruk[10];
int k = 0;
ifstream haboru;
haboru.open("haboruk.txt");
// The rows are in "haboruk.txt" like these:
// Xhosa Zulu 1696 1736
// Zulu Ndebele 1752 1782
// Zulu Sotho 1756 1772
while(!haboru.eof())
{
haboru >> haboruk[k].ki >> haboruk[k].kivel >> haboruk[k].mettol >> haboruk[k].meddig;
k++;
}
}
The output is this:
Upvotes: 1
Views: 67
Reputation: 96790
Using !file.eof()
as a condition to extract is not correct. You have to perform the extraction, and then check if the file is valid. But even using !file.eof()
afterwards is still not correct:
Let's make this simpler by creating an inserter for a Haboru
object:
std::istream& operator>>(std::istream& is, Haboru& haboruk)
{
if (!is.good())
return is;
is >> haboruk.ki;
is >> haboruk.kivel;
is >> haboruk.mettol >> haboruk.meddig;
return is;
}
Then you can create your vector (or std::array
C++11) and use the inserter for each element:
std::vector<Haboru> haboruks;
Haboru haboruk;
while (haboru >> haboruk)
{
haboruks.push_back(haboruk);
}
Or...
std::vector<Haboru> haboruks((std::istream_iterator<Haboru>(haboru)),
std::istream_iterator<Haboru>());
Upvotes: 3