I'm having trouble reading in values from a file. I know how to get all of the values in the file, but I only need some of them.

This is my code (a snippet) so far.

ifstream myStream( file ); // file is the file name .  
while (true)
{
std::getline( myStream, line '\n');  
std::getline(myStream, line '\n');	
std::istringstream myStream( line );
			
int i;
myStream >> i;

std::vector<int> myContainer;
myContainer.push_back( i );
}

The file has:

label1
1 2 3 4 5

label2
1 2 3 4 5

The code get all the values, but I just need the values under label1 for example. Does anyone know how to do this? Many thanks.

Read in the lines using a while loop and getline and compare them with "label1." Once you find it, read those next 5 values in using a for loop. Once you've done that, break out of the loop.

Thanks Jonsca. Since posting I have experimented with that, but I can't get the right values. I keep getting either the first element only of label1 or label 2, or I get a vector made up of label1 and label2 columns. If you have any more advice, that would be good. This is not homework. I'm an old fart inexperienced with streams, needing info from a file to get a calculation to work.

Here's some pseudocode to move you along (even though it's not homework, I don't want to take away from all your fun)

//Get rid of all of the getlines and the while loop
std::string temp;
int tempi;
std::vector<int> vec;  //or whatever the type actually is
while(getline(MyStream,temp))
{
    if(temp == "label1")
    {
        for(from 0 to <5)
        {
            //read in a temp int using >> (no stringstream necessary)
            vec.push_back(tempi);
             
        }

    }
}

Edit: changed the data back to ints for consistency with your post

Thanks again. I have to make my if statement if(temp != "label1") to get any kind of output at all (still not right though; getting random elements rather than what I want).

I am still using stringstream (tried cin, which I think is what you were hinting at, but no output). I also experimented more with the code I originally posted, by creating another stringstream to mine the first one. No output! Streams and me are like oil and water.

I have some possible compiler issues that could be complicating things. I noticed that a buddy can run certain stream code I've made, but I can't. Well, I will keep trying. Thanks for your suggestions.

Post what code you've tried and I can take a look at it.

Instead of cin, use your stream directly.

MyStream >> tempi;

I did try doing that. I keep ending up with no output. I thought I understood getline, but maybe not. Here is an example of what I tried.

ifstream myStream( file ); // file is the file name.
      std::string piece;
      while (true)
      {
        if(line == "testTwo")
        {
      std::getline( myStream, line '\n');
      std::getline(myStream, line '\n');
      std::istringstream myStream( line );

      std::getline( myStream, piece );
      std::istringstream pieceStream( piece );
      std::getline(pieceStream, line, '\'');
       
      int i;
      pieceStream >> i;

      std::vector<int> myContainer;
      myContainer.push_back( i );
      }
}

Here's my prior code with the pieces filled in:

std::ifstream ifs("readtolabel.txt");
std::vector<int> ivec;
std::string tempstring;
int tempint;
const int numbersperrow = 5;
while(std::getline(ifs,tempstring)) //let the getline drive the loop
{
   if(tempstring == "label3")
   {  //once we've read it, the ifstream has moved to the next line
	for(int i = 0;i<numbersperrow;i++)
	{
  	   ifs >> tempint;
	   ivec.push_back(tempint);
	}
   }

}

Thanks for your forbearance. The tip about fstream moving to the next line solves a big mystery for me. I get values, but they are all 0.

The file I used looks like this:

label1
1 2 3 4 5

label2
6 7 8 9 10

label3
13 14 15 16 17

And that's the output I got from the vector "13 14 15 16 17"

Trace through your code (perhaps with a debugger) and see if the values are being read in before you add them to the vector.

All else fails, strip your code down to this while loop, get it working, and build back up from there...

Thanks. I'll follow your advice.

My values are like this: '1' '2', so I think I will need to move the getline out below while and do some more operations to get rid of the symbols. The problem I have is that even when I use the if statement to look for a particular label (like label1 from my original post), for some reason ALL of the values in the file are read in, not just the ones below the label.

for some reason ALL of the values in the file are read in

Apologies, there should be a break; between lines 14+15 of my last snippet there. That will stop the reading once those 5 values are read.

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.