Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

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'))
    {
       
        ......
    }
0
Vassilevich
Asked:
Vassilevich
  • 3
  • 2
1 Solution
 
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
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now