Thread: Why while loop is used?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    48

    Why while loop is used?

    This is the code from a book.
    Why while loop is used?
    Code:
        while ((bytesIn = fread (rec, 1, BUF_SIZE, inFile)) > 0) {
            bytesOut = fwrite (rec, 1, bytesIn, outFile);
            if (bytesOut != bytesIn) {
                perror ("Fatal write error.");
                fclose(inFile); fclose(outFile);
                return 4;
            }
        }
    I think the code will be same as using without while loop.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You think that without a loop it is still going to repeat reading? Why would you think that?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    48
    Code:
    count = fread( buffer, sizeof( char ), 11, stream );
            cout<<buffer;
            if( ferror( stream ) )      {
                perror( "Read error" );
                break;
            }
    try this. This will print complete stream.

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    That's C++. This is a C forum.
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    48
    ok just do
    printf("%s",buffer);
    instead of cout

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by freiza View Post
    Code:
    count = fread( buffer, sizeof( char ), 11, stream );
            cout<<buffer;
            if( ferror( stream ) )      {
                perror( "Read error" );
                break;
            }
    try this. This will print complete stream.
    No it won't. It will read 11 bytes. If the stream has 100000, you won't get more than 11.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    48
    Code:
    count = fread( buffer, sizeof( char ), 100000, stream );
    
            cout<<buffer;
            if( ferror( stream ) )      {
                perror( "Read error" );
                break;
            }
    I think this will read 100000 bytes from the stream. (then what is the use of while)
    I don't want to sound rubbish. I truly want to know thank you

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    And what happens if instead I have 10x that amount? Then again, it no longer works. Also, you have to have a buffer that is massive, instead of a small one using a loop. That's why you use a loop: to repeat doing something until you don't need to anymore.

    I don't know why you feel the need to make this work without a loop.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    48
    I got your point thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. change var inside loop and run loop again
    By d387420489 in forum C Programming
    Replies: 5
    Last Post: 07-29-2011, 01:19 AM
  2. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  3. The Infinit loop that doesn't loop.
    By errigour in forum C Programming
    Replies: 1
    Last Post: 11-09-2010, 11:31 AM
  4. Help.. newbie for loop..stuck with the loop..
    By jochen in forum C Programming
    Replies: 15
    Last Post: 10-01-2007, 12:31 AM
  5. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM