?
Solved

what wrong with line of code

Posted on 2003-03-19
6
Medium Priority
?
177 Views
Last Modified: 2010-04-01
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'))
    {
       
        ......
    }
0
Comment
Question by:Vassilevich
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 8166727
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
 
LVL 8

Accepted Solution

by:
mnashadka earned 1000 total points
ID: 8166754
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
 
LVL 8

Expert Comment

by:mnashadka
ID: 8166776
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:AlexFM
ID: 8166946
OK, my previous post should be ignored.

PLEASE DO NOT ACCEPT THIS POST AS AN ANSWER.
0
 

Author Comment

by:Vassilevich
ID: 8166950
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
 
LVL 8

Expert Comment

by:mnashadka
ID: 8167043
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question