Linux -Blue forest free software | Return to home page | Site Map | Search WWW | Contact Us |
Your current position : Homepage > Free Software > Technological exchanges >Application Programming


    

Blue Forest http://www.lslnet.com at 10:18 on June 6, 2006


Puts () function is always the last line of multi-output? (Code)

I code this :

[code]
//file Puts.c
#include "Stdio.h>;
#include "Stdlib.h>;
#include "String.h>;

Int main (int argc, argv char**)
{
If (argc!=2) (
Printf ( "I need a single argument. \n ");
Exit (1);
        }

FILE *fp;
Char s[80];

If ((fp=fopen (argv[1], "r")) ==NULL) (
Printf ( "Sorry, can not open file %s. \n "Argv[1]);
Exit (1);
        }

While (!feof (fp)) (
Fgets (s,79, fp);
Puts (s);
        }

4003rd fclose (fp);
Return 0
}
/*there Is a bugs. When a file was closed, it will be flushed, so the
* The last line will be printed twice.*/
/*Ok, What can I do to avoid this problem? */
[/code]

When I test,. /a.out Puts.c, the output of which is this :

[code]
//file Puts.c

#include "Stdio.h>;

#include "Stdlib.h>;

#include "String.h>;



Int main (int argc, argv char**)

{

If (argc!=2) (

Printf ( "I need a single argument. \n ");

Exit (1);

        }



FILE *fp;

Char s[80];



If ((fp=fopen (argv[1], "r")) ==NULL) (

Printf ( "Sorry, can not open file %s. \n "Argv[1]);

Exit (1);

        }



While (!feof (fp)) (

Fgets (s,79, fp);

Puts (s);

        }



4003rd fclose (fp);

Return 0

}

/*there Is a bugs. When a file was closed, it will be flushed, so the

* The last line will be printed twice.*/

/*Ok, What can I do to avoid this problem? */

/*Ok, What can I do to avoid this problem? */
[/code]


I do not understand is : why so many firms bullish or bearish? 2, the last line is always output twice Why?

I thought it was the start of the final fclose renewable buffer, but it still exports twice Zhushidiao only know puts problems.

Please experts have educated us! !

Puts () function is always the last line of multi-output? (Code)

Fgets Huanxingfu is issued as a sign the end of this first Huanhengfu issue.
In addition to exporting information Huanhangfu puts its acquiescence, this second Huanhangfu issue, it seems an empty row. [code]
While (fgets (. . . )> NULL)
{
If (buff [strlen (buff) -1], toward ====== '\n')
Buff [strlen (buff) -1] = '\0';
Puts (buff);
}[/code]

Puts () function is always the last line of multi-output? (Code)

Thank you, windflowers Brother!

I checked a APUE recommended not to use gets and puts, it can only fgets and fputs. Made the original book, ashamed :)

Understand-not just the first two issues

Puts () function is always the last line of multi-output? (Code)

This is because the document is because feof judge concluded at the end of the documents and determine whether Huanhangfu fgets is concluded that when the last line of the document point-Huanhangfu, whlie sentences established fgets (s,79, fp)'s value has not changed. Therefore, the output string plots.


You can check the time of each cycle fgets () function return value will know.



 Privacy Policy  Copyright © 1999-2000 LSLNET.COM. All rights reserved. Blue Forest website owners. E-mail : Webmaster@lslnet.com