Hello friends,

I'm looking for an short example to read a text file and get lines splitting in 3 vectors. I don't know how I'll do this ...

E.G:

100 NAME1 30
150 NAME2 40
200 NAME3 50

->

vectorId[0] = 100
vectorName[0] = NAME1
vectorAge[0] = 30

Can you help me?

Cheers,

Sads

...

std::string token;
std::istringstream issLine(lineId);

...

 if ( myfile.is_open() )
	    {
	         while ( ! myfile.eof() )
	         {
	               getline(issLine,token, ' ');
	               if(lineAni.size() > 0)
	               {
                         
                         // ERROR IS HERE
	                 substLineId = token;
	                 vectorId.push_back(substLineId);
	                 substLineName = token;
	                 vectorName.push_back(substLineName);
	                 substLineAge = token;
	                 vectorAge.push_back(substLineAge);
	               }
	         }
	    }

...

Whenever you want white-space delimited data extraction from file, simply use ifstream's >> insertion operator... perfect for what you are trying to do:

while(infile >> temp)
{
     myvec.push_back(temp);

     infile >> temp;
     myvec2.push_back(temp);

     infile >> temp;
     myvec3.push_back(temp);
}

Edited 5 Years Ago by Clinton Portis: n/a

Thanks a lot Clinton !!

Cheers,

Sads

Whenever you want white-spaced delimited data extraction from file, simple use ifstream's >> insertion operator... perfect for what you are trying to do.

while(infile >> temp)
{
     myvec.push_back(temp);

     infile >> temp;
     myvec2.push_back(temp);

     infile >> temp;
     myvec3.push_back(temp);
}

Clinton,

Your comment was perfect! The code is pretty good now!

Thanks!

This question has already been answered. Start a new discussion instead.