fgets读取怎么换行呢?

debugdebugger 2012-02-27 05:23:35
程序接受两个命令行参数,第一个是一个字符,第二个是文件名
要求程序只打印文件中包含给定字符的那些行

#include <stdio.h>
#include <stdlib.h>
#define MAX 256
int has_ch(char ,const char *);
int main(int argc,char *argv[])
{
FILE *fp;char arr[MAX],ch;
if(argc!=3)
{
printf("no enough arguments:");
exit(EXIT_FAILURE);
}
ch=argv[1][0];
if((fp=fopen(argv[2],"r"))==NULL)
{
printf("can't open %s",argv[2]);
exit(1);
}
while(fgets(arr,MAX,fp)!=NULL)
{
if(has_ch(ch,arr))
fputs(arr,stdout);
}
fclose(fp);
return 0;
}
int has_ch(char ch,const char *arr)
{
while(*arr)
if(ch==*arr++)
return 1;
return 0;
}



我编译好后再命令行输入 test.exe a txt.txt
其中txt.txt内容为
adsf
dfdfde
asff
adfdfdf

我想问的是
   while(fgets(arr,MAX,fp)!=NULL)
{
if(has_ch(ch,arr))
fputs(arr,stdout);
}

是怎么一行一行读取字符串的
当执行第2次 while(fgets(arr,MAX,fp)!=NULL),fgets是怎么变为从txt.txt第2行开始读取的?还是其他情况
...全文
1069 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2013-05-01
  • 打赏
  • 举报
回复
我也长见识了!谢谢了。
qwer_boo 2012-02-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 pengzhixi 的回复:]

char * fgets ( char * str, int num, FILE * stream );
Get string from stream

Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a……
[/Quote]大牛就是大牛...
k841366951 2012-02-28
  • 打赏
  • 举报
回复
这个是因为文件类成员中有一个当前位置的指针,随着fgets的读取也自动向后移动,所以fgets每次读取的都是下一行。
nanjingnew4 2012-02-28
  • 打赏
  • 举报
回复
我猜楼主想问的是他凭什么去读第二行数据,而不是继续读第一行数据,这个问题我也想过,现在在楼上找到答案了
Ever_lover 2012-02-28
  • 打赏
  • 举报
回复
说白了就是俩个条件,一是遇到换行符停止同时把换行符读取到buffer中,二是 读取一定的字节,读完就ok
AnYidan 2012-02-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 smilencetion 的回复:]
引用 4 楼 pengzhixi 的回复:

char * fgets ( char * str, int num, FILE * stream );
Get string from stream

Reads characters from stream and stores them as a C string into str until (num-1) characters ha……
[/Quote]
是的!
debugdebugger 2012-02-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 pengzhixi 的回复:]

char * fgets ( char * str, int num, FILE * stream );
Get string from stream

Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a……
[/Quote]
谢谢

再请问一下对我这个程序来讲“A newline character makes fgets stop reading
是说stop reading后,进入循环体,执行完然后再继续往下读取直到EOF是吗?
pengzhixi 2012-02-27
  • 打赏
  • 举报
回复
char * fgets ( char * str, int num, FILE * stream );
Get string from stream

Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or a the End-of-File is reached, whichever comes first.
A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str.
A null character is automatically appended in str after the characters read to signal the end of the C string.

守护神2501 2012-02-27
  • 打赏
  • 举报
回复
标准流是用换行符刷新缓冲区的。。。。
debugdebugger 2012-02-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 pengzhixi 的回复:]

因为遇到换行符 fgets就会停止读取。
[/Quote]
所以一开始读完adsf及'\n'后,返回指针不是NULL,进入循环体,
接着往下一个字符读取直到最后读到EOF,返回NULL,退出循环是吗?
pengzhixi 2012-02-27
  • 打赏
  • 举报
回复
因为遇到换行符 fgets就会停止读取。

70,024

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧