C fgets问题
struct DVDInfo *ReadStruct( void ) {
struct DVDInfo *infoPtr;
int num;
char line[ kMaxLineLength ];
char *result;
infoPtr = malloc( sizeof( struct DVDInfo ) );
if ( NULL == infoPtr ) {
printf( "Out of memory!!! Goodbye!\n" );
exit( 0 );
}
printf( "Enter DVD Title: " );
result = fgets( line, kMaxLineLength, stdin );
line[ strlen( line ) - 1 ] = '\0';
infoPtr->title = MallocAndCopy( line );
printf( "Enter DVD comment: " );
result = fgets( line, kMaxLineLength, stdin );
line[ strlen( line ) - 1 ] = '\0';
infoPtr->comment = MallocAndCopy( line );
do {
printf( "Enter DVD Rating (1-10): " );
scanf( "%d", &num );
Flush();
}
while ( ( num < 1 ) || ( num > 10 ) );
infoPtr->rating = num;
printf( "\n----------\n" );
return( infoPtr );
}
上面的变量“结果”的目的是什么?没有用它做任何事情。从 fgets 返回的指针被存储到其中,但仅此而已,它没有任何用途。
struct DVDInfo *ReadStruct( void ) {
struct DVDInfo *infoPtr;
int num;
char line[ kMaxLineLength ];
char *result;
infoPtr = malloc( sizeof( struct DVDInfo ) );
if ( NULL == infoPtr ) {
printf( "Out of memory!!! Goodbye!\n" );
exit( 0 );
}
printf( "Enter DVD Title: " );
result = fgets( line, kMaxLineLength, stdin );
line[ strlen( line ) - 1 ] = '\0';
infoPtr->title = MallocAndCopy( line );
printf( "Enter DVD comment: " );
result = fgets( line, kMaxLineLength, stdin );
line[ strlen( line ) - 1 ] = '\0';
infoPtr->comment = MallocAndCopy( line );
do {
printf( "Enter DVD Rating (1-10): " );
scanf( "%d", &num );
Flush();
}
while ( ( num < 1 ) || ( num > 10 ) );
infoPtr->rating = num;
printf( "\n----------\n" );
return( infoPtr );
}
What is the purpose of even having the variable "result" above? Nothing is done with it. The pointer returned from fgets is stored into it, but that is it, it has no purpose.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该测试该结果是否为 NULL,以检查 EOF 条件或错误,而不是仅仅忽略它。另外,如果不检查结果,您将在线执行 strlen,这可能包含未初始化的数据,因为 fgets 失败。实际上,您应该在 fgets 之后:
如果第一个 fgets 成功而第二个 fgets 失败,您可能仍然会出现泄漏,因为对结构的指针成员进行了额外的分配。不幸的是,由于该结构未初始化为零,因此您无法检查这些指针是否为 NULL。因此,也许使用 calloc 而不是 malloc 或至少将所有结构指针成员初始化为 NULL,会是一个更好的主意。
You should test that result for NULL, to check for an EOF condition or an error, instead of just ignoring it. Also, by not checking result, you are doing an strlen on line, which could have uninitialized data, because fgets failed. Really, you should have, after the fgets:
You might still have leaks, if the first fgets succeeds and the second fails, because there are additional allocation to pointer members of the structure. Unfortunately, because the struct was not initialized to zero, you can't check those pointers for NULL. So, perhaps using calloc instead of malloc or at least initializing all structure pointer members to NULL, would have been a better idea.
似乎有人开始实施错误检查,但最终却搞砸了。返回值应与
NULL
进行比较,如果相等则报告错误。It seems as though someone started to implement error checking, but botched it in the end. The return value should be compared with
NULL
, with an error reported if equal.最有可能的是,编译器发出了有关被忽略的函数返回值的警告。程序员并不关心
fgets
的返回值,只需在result=
中添加即可让编译器不再纠缠它。正确的解决方案是检查返回值以确保函数成功完成。Most likely, the compiler threw a warning about a function return value that was ignored. The programmer didn't care about the return value of
fgets
and simply added in theresult =
to make the compiler quit nagging about it. The correct solution would be to check the return value to make sure the function completed successfully.