Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8165477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:32:46+00:00 2026-06-06T19:32:46+00:00

This is a working snippet of a while loop : while(total_bytes_read != fsize){ while((nread

  • 0

This is a working snippet of a while loop:

while(total_bytes_read != fsize){
    while((nread = read(sockd, filebuffer, sizeof(filebuffer))) > 0){
        if(write(fd, filebuffer, nread) < 0){
            perror("write");
            close(sockd);
            exit(1);
        }
        total_bytes_read += nread;
        if(total_bytes_read == fsize) break;
    }
}

This is an example of a NON working snippet of a while loop:

while(total_bytes_read != fsize){
    while((nread = read(sockd, filebuffer, sizeof(filebuffer))) > 0){
        if(write(fd, filebuffer, nread) < 0){
            perror("write");
            close(sockd);
            exit(1);
        }
        total_bytes_read += nread;
    }
}

And also this, is an example of a NON working snippet of a while loop:

while(total_bytes_read < fsize){
    while((nread = read(sockd, filebuffer, sizeof(filebuffer))) > 0){
        if(write(fd, filebuffer, nread) < 0){
            perror("write");
            close(sockd);
            exit(1);
        }
    }
     total_bytes_read += nread;
}

I would like to know why into the 2 snippet above when total_bytes_read is equal to fsize the loop won’t exit :O
Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:32:47+00:00Added an answer on June 6, 2026 at 7:32 pm

    The outer loop in snippets 2 and 3 will not exit because the inner loop does not exit: the total_bytes_read != fsize loop never gets a chance to check its continuation condition.

    Snippet 1 works fine, because you check the same condition inside the nested loop, and break out if the limiting count has been reached.

    You can combine both conditions into a single loop, like this:

    while((total_bytes_read != fsize) && (nread = read(sockd, filebuffer, sizeof(filebuffer))) > 0) {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had this sqlcmd snippet working in batch but when I converted to powershell,
I can't get this working for the life of me. Here is a snippet
This is a little snippet from a little flash game I'm working on: This
I have taken this little snippet straight out of some code I'm working on:
why doesn't this work: Snippet 1: int *a = new int[6]; (*a)[0]=1; while this
So I have this loop in my code that needs two separately working Iterators.
I've got this problem while developing a Wordpress plugin. Here's the code snippet of
Had this working; at one stage. The problem is the following text is now
Update: Got this working by disabling CodeIgniter Profiler which was interfering with the Ajax
I have this (working) CPU code: #define NF 3 int ND; typedef double (*POT)(double

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.