阅读小记3(《C编程专家》)

gets()不检查缓冲区空间。多余的字符将覆盖原来的栈的内容。

fgets()的第二个參数说明最大读入的字符数。

假设这个參数值为n,那么fgets()就会读取最多n-1个字符或读完一个换行符为止。两个条件满足随意一个结束。
fgets()读取到换行符,就会把它存到字符串里,而不是想gets()那样丢弃它。


fgets()的第三个參数说明读哪个文件。从键盘上读数据时,能够使用stdin(代表standard input)作为參数。


char line[512];

fgets(line, sizeof(line), stdin);
cout<<line<<endl;


FILE * pFile;
char mystring [100];
errno_t err= fopen_s (&pFile, "data.dat" , "r");
if ( err == 0 )
{
if ( fgets (mystring , 100 , pFile) != NULL )
cout<<mystring<<endl;
fclose (pFile);
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

posted @ 2015-07-03 21:16  zfyouxi  阅读(171)  评论(0编辑  收藏  举报