scanf有更好的方法吗? [英] Is there a better way for scanf?

查看:68
本文介绍了scanf有更好的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在运行此代码

int main(无效)

{

char A [3],B [3];

printf(请输入A:\ n);

scanf("%2s",A);

printf (请输入B:\ n);

scanf("%2s",B);

printf(" A is%s,B是%s \ nn,A,B);

}


如果输入A超过2个字符,我会得到输出。


../a.out

请输入A:

asdgc

请输入B:

A是,B是dg


我知道避免这种情况的使用方法

scanf("%s",A );

然而,人们通常不会以这种方式使用

所以这是避免它的更好方法吗?


非常感谢!

Hello I am running this code
int main(void)
{
char A[3],B[3];
printf("Please input A: \n");
scanf("%2s",A);
printf("Please input B: \n");
scanf("%2s",B);
printf("A is %s,B is %s\n",A,B);
}

I get output if I type A more than 2 chars.

../a.out
Please input A:
asdgc
Please input B:
A is as,B is dg

I know the way to avoid this to use
scanf("%s",A);
However, people ususally don''t use in this way
so is it a better way to avoid it?

Thanks a lot!

推荐答案



" QQ" <菊**** @ yahoo.com>在消息中写道

news:11 ********************* @ g44g2000cwa.googlegro ups.com ...

"QQ" <ju****@yahoo.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
您好我正在运行此代码
int main(无效)
{A A [3],B [3];
printf("请输入A: \ n");
scanf("%2s",A);
printf(" Please input B:\ n");
scanf("%2s" ,B);
printf(A是%s,B是%s \ n,A,B);


如果输入我输出一个超过2个字符。

./a.out
请输入A:
asdgc
请输入B:
A是as,B是dg

我知道如何避免使用
scanf("%s",A);
然而,人们通常不会以这种方式使用


原因是没有任何保护,因为

溢出阵列。

所以它是一种更好的避免方法它?
Hello I am running this code
int main(void)
{
char A[3],B[3];
printf("Please input A: \n");
scanf("%2s",A);
printf("Please input B: \n");
scanf("%2s",B);
printf("A is %s,B is %s\n",A,B);
}

I get output if I type A more than 2 chars.

./a.out
Please input A:
asdgc
Please input B:
A is as,B is dg

I know the way to avoid this to use
scanf("%s",A);
However, people ususally don''t use in this way
The reason is that there''s no protection from
overflowing the array.
so is it a better way to avoid it?




如上所述限制输入,只需
扔掉任何不需要的字符:


#include< stdio.h>


void discard(void)

{

int c = 0;

while((c = getchar())!= EOF&& c!=''\ n'')

;

}


int main(无效)

{

char A [3] = {0};

char B [3] = {0};


printf(请输入A:\ n);

scanf("%2s",A);

discard();


printf("请输入B:\ n");

scanf("%2s",B);

discard();


printf(A是%s,B是%s \ n,A,B);

返回0 ;

}


-Mike



Limit the input as you''re doing above, and simply
throw away any unwanted characters:

#include <stdio.h>

void discard(void)
{
int c = 0;
while((c = getchar()) != EOF && c != ''\n'')
;
}

int main(void)
{
char A[3] = {0};
char B[3] = {0};

printf("Please input A: \n");
scanf("%2s", A);
discard();

printf("Please input B: \n");
scanf("%2s", B);
discard();

printf("A is %s, B is %s\n",A,B);
return 0;
}

-Mike


QQ写道:
您好我正在运行此代码
int main(void)
{A A [3],B [3];
printf(请输入A:\ n ;);
scanf("%2s",A);
printf(" Please input B:\ n");
scanf("%2s",B) ;
printf(A是%s,B是%s \ n,A,B);


如果输入A,我会得到输出请输入A:
asdgc
请输入B:
A是,B是dg

我知道如何避免使用
scanf("%s",A);


不,这不是避免它的方法。除了将

程序暴露给缓冲区溢出之外,这可能不会做你想要的。如果

用户输入三个单词,第一个单词将被读入A,

秒将由上述语句处理,但最后一个单词

仍然会在输入流上等待下一次

调用scanf来接收。

然而,人们通常不会使用通过这种方式
这是避免它的更好方法吗?
Hello I am running this code
int main(void)
{
char A[3],B[3];
printf("Please input A: \n");
scanf("%2s",A);
printf("Please input B: \n");
scanf("%2s",B);
printf("A is %s,B is %s\n",A,B);
}

I get output if I type A more than 2 chars.

./a.out
Please input A:
asdgc
Please input B:
A is as,B is dg

I know the way to avoid this to use
scanf("%s",A);
No, that''s not the way to avoid it. In addition to exposing the
program to buffer overflows, this probably won''t do what you want. If
the user entered three words, the first one would be read into A, the
second would then be handled by the above statement, but the last word
would still be on the input stream waiting to be picked up by the next
call to scanf.
However, people ususally don''t use in this way
so is it a better way to avoid it?




你还没有完全清楚你想要避免的是什么。如果

你只想在你的scanf之后丢弃其余部分,请尝试这样的



scanf( "%* [^ \ n]%* 1 [\ n]");


您也可以考虑使用fgets和sscanf。


Robert Gamble



You weren''t completely clear on what it is you are trying to avoid. If
you just want to discard the rest of the line after your scanf, try
something like this:

scanf("%*[^\n]%*1[\n]");

You might also consider using fgets and sscanf instead.

Robert Gamble


Robert Gamble写道:
Robert Gamble wrote:
...如果你只想丢弃其余的在你的扫描结束之后的行,尝试这样的事情:

scanf("%* [^ \ n]%* 1 [\ n]");


请注意%[必须匹配_at至少一个字符,然后scanf将

吞下后续换行符。因此,如果

行上的剩余文本是_just_换行符,则此scanf调用会将其保留在那里。

更好的是......


if(scanf("%* [^ \ n]")!= EOF)getchar();

您也可以考虑使用fgets和sscanf。
... If you just want to discard the rest of the line after your
scanf, try something like this:

scanf("%*[^\n]%*1[\n]");
Note that %[ must match _at least_ one character before scanf will
swallow a subsequent newline. Hence, if the remaining text on the
line is _just_ the newline, this scanf call will leave it there.
Better is something like...

if (scanf("%*[^\n]") != EOF) getchar();
You might also consider using fgets and sscanf instead.




-

彼得



--
Peter


这篇关于scanf有更好的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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