-2

I got this struct:

 struct human{
 int ma;
 double su;
 string nev;

};

And this part in main() :

    vector<human> t;
    fstream inf("in.txt");

I want read in the information from file by using this :

while(!inf.eof())
   {
       t.push_back(??????);

   }

With t.push_back(??????); I want to put the stuff from file in to my vector struct

 int ma;
 double su;
 string nev;

The number off elements in file are unknown. So the big question is how to fill up my struct by usingpush_back();? Sorry for noob question :) Content looks like this :(in.txt)

123
34.34
Dani
180
70.1
Victor
190
90
julika
200
45
rozika
  • 2
    You will need to read lines from your file. It depends on how each `human` is represented in the file? Is it one line per human or more? You will need to read the line, convert to the appropriate type (int, double) then construct the struct and push it back to the vector. If you can provide a small representative input file, it will clarify whats required. – Paul Rooney May 04 '17 at 00:24
  • 2
    http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – aschepler May 04 '17 at 00:45
  • 1
    And don't use eof as a loop condition http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Jerry Jeremiah May 04 '17 at 01:47
  • You need to show us how the content in file looks like. – Robert C. Holland May 04 '17 at 01:50

1 Answers1

0

Your loop could look like:

for (;;)
{
    human h;
    inf >> h.ma;
    inf >> h.su;
    inf >> h.nev;

    if ( !inf )
        break;

    t.push_back( std::move(h) );
}

Obviously the details of reading the file depend on the file format , which you didn't specify. Never use eof() in a loop condition, actually what you want to be testing is whether or not you successfully read the data you are trying to read. Not whether there was end-of-file at some point.

M.M
  • 130,300
  • 18
  • 171
  • 314