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);
	               }
	         }
	    }

...

Recommended Answers

All 3 Replies

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);
}

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.