Jump to content

Why while loop is used?

- - - - -

  • Please log in to reply
8 replies to this topic

#1
freiza

freiza

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
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;

	



#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
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).

#3
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
>>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)

{


}


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

#4
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
@Ancient Dragon: does it make sense to test the stream for error after you perform the read?
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#5
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
@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:

Ancient Dragon said:

...
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?

#6
freiza

freiza

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
Thank you very much.

#7
freiza

freiza

    Learning Programmer

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

#8
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
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.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#9
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
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.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users