what wrong with line of code

std::istringstream in(string);

Is this wrong ?
while(!in.eof())
    {
        std::getline(in,l_eachClient,'\n');
        ......
    }

Is better to use this way  bellow  ? Why ? prons and cons

while(std::getline(in,l_eachClient,'\n'))
    {
       
        ......
    }
VassilevichAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
std::getline returns stream reference in any case, even if end of file is reached. Second code fragment creates endless loop.

I beleive that first code fragment is OK, if code after getline is ready to handle 0 characters.
0
mnashadkaCommented:
The second way is usually better depending upon the logic of your code.  If your string ends in a \n (end line character), the first while loop will go through the loop an extra time and you will have to check to see if the string (l_eachClient) is empty, whereas the second while loop will return false the last time and you won't have to do that extra check.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mnashadkaCommented:
AlexFM, the stream reference is returned, but since getline is used in a boolean check, it uses the implicit ios_base void * cast and will return NULL when it reaches the end of the stream.  This will cause the loop to end, so the second code fragment does not create an endless loop.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

AlexFMCommented:
OK, my previous post should be ignored.

PLEASE DO NOT ACCEPT THIS POST AS AN ANSWER.
0
VassilevichAuthor Commented:
Out of interest ?

Can this expression go to endless loop if somthing wrong
with object in :

    std::istringstream in(sub_line);

    while(!in.eof())
    {
        std::getline(in,mutch_word,' ');
    }

0
mnashadkaCommented:
It shouldn't go into an endless loop.  You might do something bad if you try to use the string (mutch_word) that was read at the end of the file, though.

while(!in.eof())
{
  std::getline(in, mutch_word, '');
  char c = mutch_word[0]; // This could cause a core or runaway process or something like that
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.