使用strncmp进行fgets行为 [英] fgets behaviour with strncmp

查看:41
本文介绍了使用strncmp进行fgets行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


看似简单的任务遇到一些麻烦。需要具有以下基本CLI功能:


提示符< - \ n

提示符< - \\ \\ n

promptquit< - 应该根据我的片段退出,但不是。


我已经抛出一些随机的fflush()进来了,但没有快乐。我从来没有真正学过ANSI方式ala fgets,fopen等等。所以这对我来说是新的。关于如何实际让程序退出的任何建议

当退出时输入将被赞赏:-)


代码:


#include< stdio.h>


int main()

{


int done = 0;

int len = 0;

char buf [MAXBUF];


printf("");

while(!done)

{

if(fgets(buf,MAXBUF,stdin)< 0)

{

perror(" fgets");

退出(1);

}


len = strlen(buf);

buf [len] =''\''';


fflush(stdin);

if(* buf ==''\ n''' )

printf("");

printf(" debug:%s \ n",buf);

fflush (stdin);

fflush(stdout);

if(!strncmp(buf," quit",len))

done = 1;

}

返回0;

}


谢谢


kb

Hi all,

Having some trouble with a seemingly-simple task. Need to have a
basic CLI functionality as per:

prompt <- \n
prompt <- \n
promptquit <- should quit as per my snippet, but doesn''t.

I''ve thrown a few random fflush()''s in, but no joy. I have never
really learnt the ANSI way ala fgets, fopen, etc.. so this is quite
new to me. Any advice on how I can actually make the program quit
when "quit" is entered would be appreciated :-)

Code:

#include <stdio.h>

int main()
{

int done=0;
int len=0;
char buf[MAXBUF];

printf("");
while(!done)
{
if(fgets(buf,MAXBUF,stdin) < 0)
{
perror("fgets");
exit(1);
}

len=strlen(buf);
buf[len]=''\0'';

fflush(stdin);
if(*buf == ''\n'')
printf("");
printf("debug: %s\n",buf);
fflush(stdin);
fflush(stdout);
if(!strncmp(buf,"quit",len))
done=1;
}
return 0;
}

thanks

kb

推荐答案

6月17日上午11点01分,Krumble Bunk< krumbleb ... @ gmail.c omwrote:
On Jun 17, 11:01 am, Krumble Bunk <krumbleb...@gmail.comwrote:

大家好,


看似简单的任务有些麻烦。需要具有以下基本CLI功能:


提示符< - \ n

提示符< - \\ \\ n

promptquit< - 应该根据我的片段退出,但不是。


我已经抛出一些随机的fflush()进来了,但没有快乐。我从来没有真正学过ANSI方式ala fgets,fopen等等。所以这对我来说是新的。关于如何实际让程序退出的任何建议

当退出时输入将被赞赏:-)


代码:


#include< stdio.h>


int main()

{


int done = 0;

int len = 0;

char buf [MAXBUF];


printf("");

while(!done)

{

if(fgets(buf,MAXBUF,stdin)< 0)

{

perror(" fgets");

退出(1);

}


len = strlen(buf);

buf [len] =''\''';


fflush(stdin);

if(* buf ==''\ n''' )

printf("");

printf(" debug:%s \ n",buf);

fflush (stdin);

fflush(stdout);

if(!strncmp(buf," quit",len))

done = 1;

}

返回0;


}


谢谢


kb
Hi all,

Having some trouble with a seemingly-simple task. Need to have a
basic CLI functionality as per:

prompt <- \n
prompt <- \n
promptquit <- should quit as per my snippet, but doesn''t.

I''ve thrown a few random fflush()''s in, but no joy. I have never
really learnt the ANSI way ala fgets, fopen, etc.. so this is quite
new to me. Any advice on how I can actually make the program quit
when "quit" is entered would be appreciated :-)

Code:

#include <stdio.h>

int main()
{

int done=0;
int len=0;
char buf[MAXBUF];

printf("");
while(!done)
{
if(fgets(buf,MAXBUF,stdin) < 0)
{
perror("fgets");
exit(1);
}

len=strlen(buf);
buf[len]=''\0'';

fflush(stdin);
if(*buf == ''\n'')
printf("");
printf("debug: %s\n",buf);
fflush(stdin);
fflush(stdout);
if(!strncmp(buf,"quit",len))
done=1;
}
return 0;

}

thanks

kb



那里有很多问题,但你的核心问题是buf包含了换行符
。如果你设置(经过适当的检查)buf [len-1]到

''\ 0''并使用strcmp你将解决这个问题。其他几点:


不要fflush(stdin)

包括string.h,stdlib.h

在某处定义MAXBUF

打开你的编译器警告...


-David

Lots of issues there, but your central problem is that buf includes
the newline. If you set (after appropriate checking) buf[len-1] to
''\0'' and use strcmp you will fix that. A few other points:

don''t fflush(stdin)
do include string.h, stdlib.h
do define MAXBUF somewhere
do turn on your compilers warnings...

-David


6月17日下午4:14,David Resnick< lndresn ... @ gmail.comwrote:
On Jun 17, 4:14 pm, David Resnick <lndresn...@gmail.comwrote:

6月17日上午11:01,Krumble Bunk< ; krumbleb ... @ gmail.comwrote:
On Jun 17, 11:01 am, Krumble Bunk <krumbleb...@gmail.comwrote:

大家好,
Hi all,


有看似简单的任务有些麻烦。需要具有以下基本CLI功能:
Having some trouble with a seemingly-simple task. Need to have a
basic CLI functionality as per:


prompt< - \\\


提示< - \ n

promptquit< - 应该根据我的片段退出,但不是。
prompt <- \n
prompt <- \n
promptquit <- should quit as per my snippet, but doesn''t.


我已经抛出一些随机的fflush()了,但没有快乐。我从来没有真正学过ANSI方式ala fgets,fopen等等。所以这对我来说是新的。关于如何实际让程序退出的任何建议

当退出时输入将被赞赏:-)
I''ve thrown a few random fflush()''s in, but no joy. I have never
really learnt the ANSI way ala fgets, fopen, etc.. so this is quite
new to me. Any advice on how I can actually make the program quit
when "quit" is entered would be appreciated :-)


代码:
Code:


#include< stdio.h>
#include <stdio.h>


int main()

{
int main()
{


int done = 0;

int len = 0;

char buf [MAXBUF];
int done=0;
int len=0;
char buf[MAXBUF];


printf("");

while(!done)

{

if(fgets(buf,MAXBUF,stdin)< 0)

{

perror(" fgets");

退出(1);

}
printf("");
while(!done)
{
if(fgets(buf,MAXBUF,stdin) < 0)
{
perror("fgets");
exit(1);
}


len = strlen(buf);

buf [len] =''\''';
len=strlen(buf);
buf[len]=''\0'';


fflush(stdin);

if(* buf ==''\ n'')

printf("");

printf(" debug:%s \ n",buf);

fflush(stdin);

fflush(stdout);

if(!strncmp(buf," quit",len))

done = 1;

}

返回0;
fflush(stdin);
if(*buf == ''\n'')
printf("");
printf("debug: %s\n",buf);
fflush(stdin);
fflush(stdout);
if(!strncmp(buf,"quit",len))
done=1;
}
return 0;


}
}


谢谢
thanks


kb
kb



那里有很多问题,但你的核心问题是buf包括

换行符。如果你设置(经过适当的检查)buf [len-1]到

''\ 0''并使用strcmp你将解决这个问题。其他几点:


不要fflush(stdin)

包括string.h,stdlib.h

在某处定义MAXBUF

打开你的编译器警告...


-David


Lots of issues there, but your central problem is that buf includes
the newline. If you set (after appropriate checking) buf[len-1] to
''\0'' and use strcmp you will fix that. A few other points:

don''t fflush(stdin)
do include string.h, stdlib.h
do define MAXBUF somewhere
do turn on your compilers warnings...

-David



非常感谢 - 修好了。


再次感谢


kb。

many thanks - fixed it nicely.

thanks again

kb.


Krumble Bunk写道,On 17/06/08 16:01:


< snip>


除了其他评论你已经看过...


代码的随机更改(你提到随机调用

fflush)绝不是一个很好的开发方式。
Krumble Bunk wrote, On 17/06/08 16:01:

<snip>

In addition to the other comment you have seen...

Random changes to code (you mentioned throwing in random calls to
fflush) is NEVER a good way to develop.

#include< stdio.h>


int main()
#include <stdio.h>

int main()



更好会是

int main(无效)

Better would be
int main(void)


{


int int done = 0;

int len = 0;

char buf [MAXBUF];


printf("");
{

int done=0;
int len=0;
char buf[MAXBUF];

printf("");



在这个例子中,do-while循环更有意义,因为你总是希望

至少运行一次循环。

In this instance a do-while loop makes more sense since you always want
to run the loop at least once.


while(!done)

{
while(!done)
{



fflush(stdout);


使用代码,在等待

输入之前,可能不会显示第一个提示。

fflush(stdout);

With your code the first prompt might not be displayed before waiting
for input.


if (fgets(buf,MAXBUF,stdin)< 0)

{

perror(" fgets");

退出(1) ;
if(fgets(buf,MAXBUF,stdin) < 0)
{
perror("fgets");
exit(1);



这不是传递给退出的可移植值。只有0,EXIT_SUCCESS和

EXIT_FAILURE是可移植的。有时候有充分的理由使用

非便携式返回码,但不是这里。


此外,稍后您使用退货而不是退出退货。你应该用
来确定你使用哪种方法离开main。我个人通常

使用return。

This is not a portable value to pass to exit. Only 0, EXIT_SUCCESS and
EXIT_FAILURE are portable. Some times there are good reasons to use
non-portable return codes, but not here.

Also, later you use a return rather than calling exit. You should be
consistent in which method you use to leave main. Personally I normally
use return.


}


len = strlen(buf);

buf [len] =''\''';
}

len=strlen(buf);
buf[len]=''\0'';



如果输入的行太长,这将无法执行您想要的操作。检查

先看看它是否是换行符。

This would not do what you want if too long a line was input. Check to
see if it was a newline first.


fflush(stdin);
fflush(stdin);



fflush没有为C标准定义输入流。一些

实现*可能*定义它,但其他人肯定不会。

fflush is not defined for input streams by the C standard. Some
implementations *might* define it, but others definitely don''t.


if(* buf ==''\ n'')

printf("");
if(*buf == ''\n'')
printf("");



我认为更好地确定你是否在buf上使用数组

或指针运算符。请注意,在这里打印提示似乎

奇数并且有条件地这样做似乎更奇怪。

Better in my opinion to be consistent about whether you are using array
or pointer operators on buf. Mind you, printing the prompt here seems
odd and doing so conditionally seems even more odd.


printf(" debug:%s \ n",buf);

fflush(stdin);

fflush(stdout);

if(!strncmp(buf,") ;退出,len))

done = 1;
printf("debug: %s\n",buf);
fflush(stdin);
fflush(stdout);
if(!strncmp(buf,"quit",len))
done=1;



切换到do-while循环后,您可以将上述测试作为

条件并废弃完成标记!

After switching to a do-while loop you could put the above test as the
condition and scrap the done flag!


}

返回0;

}
}
return 0;
}



-

Flash Gordon

--
Flash Gordon


这篇关于使用strncmp进行fgets行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆