fgets 函数得问题
# include<stdio.h>
# include<string.h>
# define MAX_SIZE 200
int main()
{
char buffer[ MAX_SIZE ];
int i = 0;
FILE *in;
in = fopen("temp.c","r");
if( !in ){
fprintf(stderr, "Open file %s failed!\n","temp.c");
exit( -1);
}
memset( buffer, '\0', sizeof( buffer ) );
while( !feof( in ) ){
(void *)fgets( buffer, MAX_SIZE, in );
#ifdef TEST1
for(; i < sizeof( buffer ); i ++)
printf("%c",buffer[ i ] );
#endif
#ifdef TEST2
printf("%s",buffer);
#endif
}
# ifdef TEST3
while( fgets( buffer, MAX_SIZE, in ) != NULL )
printf("%s",buffer );
# endif
printf("%d",strlen(buffer));
fclose( in );
exit( 0 );
}
==========================================
/* temp.c:
hello world! This is Linux World!
*/
==========================================
上面是fgets 得测试代码和 temp.c文件得内容
其中 temp.c 得内容小于 MAX_SIZE 并且是一行写完得
我得问题是 :
测试 TEST2 时:
会得到两行输出
而测试其他得都是一行输出??
thanks!