Reputation: 53
I want to read the exact below line from text file by using ifstream associated with getline function
KM78457 , C1 , Testing , ZMY290HR6UP-B ,GHTTTTTTT , 0.1268 , 32 , 4.06 ,
But It should be able to read in start from " , C1 , Testing , ZMY290HR6UP-B ,GHTTTTTTT , 0.1268 , 32 , 4.06 ," therefore there is still missing because all failed to capture the words "KM78457" at the most front line.
std::ifstream fi;
std::string streamline;
fi.open("C:/exp_test.txt",std::ios::in );
while (!fi.eof())
{
fi.clear();
fi.seekg(0,std::ios::cur);
fi >> newline;
std::getline(fi,streamline);
std::cout << streamline ;
}
Can anyone give me a help, Thanks.
Upvotes: 0
Views: 63
Reputation: 96865
The problem is that you're using unformatted input directly after a formatted extraction. By default, formatted extractors do not ignore or discard the residual newline that appears after the user submits input. You need to clear tha newline manually using std::ws
.
Moreover, using !eof()
has a condition for input is always wrong. It is wrong beause you are checking the state of the stream before performing input which can cause problems if you use the result of a failed extraction.
Use the input operation itself as a condition. It will perform the read and then check the state of the stream, returning true or false if the stream encountered errors.
while (std::getline(fi >> newline >> std::ws, streamline))
{
std::cout << streamline;
}
Upvotes: 1