Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Why while loop is used?

loop

  • Please log in to reply
8 replies to this topic

#1 freiza

freiza

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 56 posts

Posted 27 October 2011 - 03:24 PM

This is the code from a book.
Why while loop is used?


int  count=0, total = 0;
	char bufferd[100];
	FILE *stream;
	//FILE *outstream;

	if( (stream = fopen( "feof.txt", "a+" )) == NULL )
		exit( 1 );
	

	/* Cycle until end of file reached: */
	while( !feof( stream ) )
	{
		
		count = fread( buffer, sizeof( char ), 100, stream );
		cout<<buffer;
		if( ferror( stream ) )      {
			perror( "Read error" );
			break;
		}

		/* Total up actual bytes read */
		total += count;
	}

I think the code below performs the same action without while loop.
int  count=0, total = 0;
	char bufferd[11];
	FILE *stream;
	//FILE *outstream;

	if( (stream = fopen( "feof.txt", "a+" )) == NULL )
		exit( 1 );
	

	
		
		count = fread( buffer, sizeof( char ), 100, stream );
		cout<<buffer;
		if( ferror( stream ) )      {
			perror( "Read error" );
			
		}

		/* Total up actual bytes read */
		total += count;
	

  • 0

#2 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 27 October 2011 - 04:07 PM

If the while loop is used, then there must be a good reason for it to be used. Think of it this way, if you were the computer, how would you read a file and display the data read? Would you read the whole file into memory, and then send the buffer to the screen? If so, how do you know how many bytes to read? How would you go about it if the file was larger than 4 GB (you can only access 4 GB at a time, in 32-bit mode)? Would you let your program fail then? Or maybe you would read smaller pieces at a time, and work on those smaller pieces?

The point of the while loop is to split the file into smaller chunks. That code reads 100 bytes at a time, and sends them to the display; it breaks from the loop when it reaches the end of the file.

If you tested the latter code, and it worked, then the reason would be that the file you tested it on is not over 100 bytes in size; if you test that code on a, say, 512-byte file, it would not work - it would only read and display the first 100 bytes.

The reason it's 100 bytes, and not one byte at a time, is that reading one byte at a time takes VERY long; you can even try it out, if you need more proof. If you want to try it out, make a program that would copy a file, byte by byte, and test it on a 3 GB, or so, file. Then try a few hundred bytes at a time, and compare the profiles (profile= time it takes to complete process).
  • 0

#3 Ancient Dragon

Ancient Dragon

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 378 posts

Posted 27 October 2011 - 05:41 PM

>>while( !feof( stream ) )

If you got that from a book then you had better get yourself a different book because that is just wrong. :crying: Why is it wrong? Because it will cause the last line of the file to be read twice. Here is a better while loop that avoids that problem, and notice that feof() is not used because its not needed. Also not that sizeof(char) is guaranteed by the C/C++ standards to be 1, so there is no point in using the sizeof operator.
while(  fread( buffer, 1, sizeof(buffer), stream ) > 0)
{

}

  • 0
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#4 fread

fread

    Programming God

  • Senior Member
  • PipPipPipPipPipPip
  • 897 posts
  • Location:Trinidad and Tobago
  • Learning:C, Java, C++, C#, PHP, Python, PL/SQL

Posted 27 October 2011 - 05:47 PM

@Ancient Dragon: does it make sense to test the stream for error after you perform the read?
  • 0

#5 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 27 October 2011 - 07:26 PM

@Ancient Dragon: The code uses the 'count' variable as well, so wouldn't it make sense to do this?:
while(  [COLOR="#FF0000"](count=[/COLOR] fread( buffer, 1, sizeof(buffer), stream ) [COLOR="#FF0000"])[/COLOR] > 0)
{

[COLOR="#FF0000"]total += count; [/COLOR]
}


EDIT: Oh, and for this:

...
Because it will cause the last line of the file to be read twice.
...


How can you read the last line twice if you're reading sizeof (buffer) bytes at a time, not line by line?
  • 0

#6 freiza

freiza

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 56 posts

Posted 28 October 2011 - 01:42 AM

Thank you very much.
  • 0

#7 freiza

freiza

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 56 posts

Posted 28 October 2011 - 02:02 AM

Thank you. But I didn't get how it is going to read the last line/chunk twice?
  • 0

#8 Ancient Dragon

Ancient Dragon

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 378 posts

Posted 28 October 2011 - 04:15 AM

It reads the last line twice because EOF does not get set until after a read is performed. Here is a much more detailed explanation.

>>@Ancient Dragon: does it make sense to test the stream for error after you perform the read?

It might in some cases, but generally not. There is no good way to check for error. If the value returned by fread() is not the same as the value requested then that doesn't necessarily mean there was a read error. Could have been just end of file.
  • 0
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#9 fread

fread

    Programming God

  • Senior Member
  • PipPipPipPipPipPip
  • 897 posts
  • Location:Trinidad and Tobago
  • Learning:C, Java, C++, C#, PHP, Python, PL/SQL

Posted 28 October 2011 - 05:37 AM

I think i wrote a lot of code with that loop pattern. In truth i still use that very same loop. I'll try to commit this to memory.
  • 0





Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download