getline( file, line ) until line == "anotherName";
then do
file.ignore();
file >> new_int;
file.ignore();
vector.push_back(new_int);
while gile.get() != '\n'
I assume the problem is because >> finds a '
Don't know why that would happen. Post your whole function.
Also, I assumed that the first symbol in the line would be ' ,that the quoted numbers are separated with single spaces and that there are no spaces between the last ' and the \n.
It's fine how you wrote that 'until'. Another way would be while( getline( theStream, line) && line != "anotherName" );
I tried two different methods and they both produce vectors containing only the first element of the line I want. I just can't figure out where I'm going wrong.
Here they both are.
(1) Your way. I had to comment out the final line, "while theStream.get() != '\n';" because it produced no results. Maybe I made a mistake with this?
The main problem is that you only have one loop when two are needed.
Though there are other problems (perhaps proper indentation would help you find them)
I'll use the 1st one:
int main()
{
std::string line;
ifstream theStream( path to the text file );
int new_int;
std::vector<int> container;
while (std::getline( theStream, line )) //while there are lines in the file, read one
{
if(line == "anotherName") // if that line equals "anotherName",
{
theStream.ignore();
theStream >> new_int; //read a single number from the file
theStream.ignore();
container.push_back(new_int); //and push it into a vector
//and then print the contents of that vector.
std::copy(container.begin(), container.end(), std::ostream_iterator<int>(std::cout, "\n"));
}
}
return 0;
}
How can you have more than one element if the input code is executed only once for each "anotherName" ?
In my pseudo-code the 'while' was the end of a 'do-while' loop. Put the input into a loop and try again.
hamsterman. Thanks so much for your patience, and apologies for my sloppy indentation! What an idiot I am. My understanding of what was going on with ifstream was incorrect.
Was this another of the problems that you referred to? In the output, the first character (not element) is missing. For example, the the first element is printed out as 2 when I change my sample data file anotherName to:
anotherName
'22' '33' '55'
If you are not yet at the end of your rope, any heads-up would be appreciated, but no pressure! You have already gone above and beyond the call of duty! This is not homework, by the way. I'm old and trying to learn C++ for dealing with experimental results.
//theStream contains '22' '33'
while ( theStream.get() != '\n' )//.get() finds a ' and removes it
{
theStream.ignore();//.ignore finds 2 and removes it
theStream >> new_int;//>> reads the remaining 2
theStream.ignore();//.ignore finds ' and removes it
container.push_back(new_int);
//on the next cycle .get will find a whitespace (or a newline)
//, .ignore will find a ' and >> will read a full number
}
A solution:
1 2 3 4 5 6 7 8 9 10
//theStream contains '22' '33'
do
{
theStream.ignore();//.ignore finds ' and removes it
theStream >> new_int;//>> reads the full number
theStream.ignore();//.ignore finds ' and removes it
container.push_back(new_int);
}
while ( theStream.get() != '\n' )//.get() finds a whitespace of a newline and removes it
//the next cycle will be identical
But this is bad if the line is empty (.ignore will read a newline, >> will try to read something but won't find any digits and thus fail). To prevent this you can make line 2 if(theStream.peek() != '\n') do //... (.peek() returns the next char in the stream without removing it). This is, of course, only needed it a line can be empty.
hamsterman, thanks again. I would not have figured this out and I'm very grateful for your help. This solved the problem and it has been very educational.