scanf问题与摄入字符串* PLS。救命* [英] scanf problem with intaking a string *PLS. HELP*
问题描述
HI all;
我创建了一个程序只是为了理解strcpy。 (还是一个
业余开发者)。
我使用scanf(%s,& first)来获取变量的输入第一个
(首先是char [])。
然后我去strcpy(第二个,第一个)。这只是为了展示我自己的strcpy
函数,并且它(据我所知)复制首先的内容为
秒。
问题是:*当用户在他/她的输入中使用空格时(例如Hello
World)
- >只有你好将显示。
现在我确保阵列有足够的尺寸;我只是不知道'b $ b错误是什么?
我很遗憾错误地删除了源代码(因为我是
沮丧);所以我不能在这里供应;如果你们真的需要它我不会再次写出来但是我想你们都可以从我的描述中得到一个非常好的
理解。
请帮助。非常感谢它提前。
HI all;
I have created a program just to get an understanding of strcpy. (still an
amateur developer).
I use scanf("%s", &first) in order to get the input to the variable first
(which is char first[]).
Then I go strcpy(second, first). This is just to demonstrate the strcpy
function for myself and it (as far as I know) copies what''s in first to
second.
The problem is: * When a user uses spaces in his/her input (e.g. "Hello
World")
--> Only "Hello" will be displayed.
Now I have made sure the array has ample size; I just dont know what''s
wrong?
I unfortunately have deleted the source code by mistake (as I was
frustrated); so I can''t supply it here; If y''all really need it I wouldn''t
ming writing it out again but I think y''all can have a pretty good
understanding from my descriptions.
Please Help. Thanks a lot for it in advance.
推荐答案
" Radith" < RA **** @ xtra.co.nz>写道:
"Radith" <ra****@xtra.co.nz> writes:
我使用scanf("%s"& first)来获取变量的输入
(这是char first []) 。
然后我去strcpy(第二,第一)。这只是为了展示我自己的strcpy
功能,并且(据我所知)将第一个内容复制到第二个。
问题是:*当用户在他/她的输入中使用空格时(例如Hello
World)
- >只有你好将显示。
I use scanf("%s", &first) in order to get the input to the variable first
(which is char first[]).
Then I go strcpy(second, first). This is just to demonstrate the strcpy
function for myself and it (as far as I know) copies what''s in first to
second.
The problem is: * When a user uses spaces in his/her input (e.g. "Hello
World")
--> Only "Hello" will be displayed.
这是因为%s只读取一个单词。它将跳过
领先的空白区域,但之后空白区域会导致它停止
读取。
-
当我不得不依靠不足时,我更喜欢它是我自己的。
- 理查德希思菲尔德
This is because %s only reads a single word. It will skip
leading white space, but after that white space causes it to stop
reading.
--
"When I have to rely on inadequacy, I prefer it to be my own."
--Richard Heathfield
Radith < RA **** @ xtra.co.nz>写了
"Radith" <ra****@xtra.co.nz> wrote
我使用scanf("%s"& first)来获取变量的输入
(这是char first [])。
I use scanf("%s", &first) in order to get the input to the variable first
(which is char first[]).
scanf("%s",ptr);
只读取第一个单词(由空格分隔的字符串)
进入ptr指向的缓冲区。
你可以用gets()读取整行。不幸的是,这个功能是有缺陷的,因为如果用户在太多字符中键入
,缓冲区将会溢出,可能会导致崩溃。如果使用不正确,fgets()会更糟糕 - 输入是
静默截断。为什么会更糟?因为错误的结果通常比没有结果更糟糕。但是你可以在fgets()的顶部构建一个可靠的输入函数,但是它不必要地复杂。
很遗憾没有简单的方法从用户在ISO
C.
scanf("%s", ptr);
will only read the first word (string of characters separated by whitespace)
into the bufer pointed to by ptr.
You can read a whole line with gets(). Unfortunately the function is flawed,
because the buffer will overrun, probably causing a crash, if the user types
in too many characters. fgets() is even worse if used incorrectly - input is
silently truncated. Why is that worse? Because wrong results are usually
worse than no results. However you can build a solid input function on top
of fgets(), but it is needlessly complicated.
So regrettably there is no easy way of getting a line from the user in ISO
C.
" Malcolm" <再******* @ btinternet.com>写道:
"Malcolm" <re*******@btinternet.com> writes:
" Radith" < RA **** @ xtra.co.nz>写了
"Radith" <ra****@xtra.co.nz> wrote
我使用scanf("%s"& first)来获取变量的输入
(这是char first [])。
I use scanf("%s", &first) in order to get the input to the variable first
(which is char first[]).
scanf("%s",ptr);
只读取第一个单词(由空格分隔的字符串)
进入指向的缓冲区通过ptr。
您可以使用gets()读取整行。不幸的是,该功能存在缺陷,因为如果用户输入太多字符,缓冲区将会溢出,可能会导致崩溃。如果使用不正确,fgets()会更糟糕 - 输入被静默截断。为什么会更糟?因为错误的结果通常比没有结果更糟糕。但是你可以在fgets()的顶部构建一个可靠的输入函数,但是它已经不必要地复杂了。
很遗憾,在ISO中没有简单的方法可以从用户那里获得一行。
C 。
scanf("%s", ptr);
will only read the first word (string of characters separated by whitespace)
into the bufer pointed to by ptr.
You can read a whole line with gets(). Unfortunately the function is flawed,
because the buffer will overrun, probably causing a crash, if the user types
in too many characters. fgets() is even worse if used incorrectly - input is
silently truncated. Why is that worse? Because wrong results are usually
worse than no results. However you can build a solid input function on top
of fgets(), but it is needlessly complicated.
So regrettably there is no easy way of getting a line from the user in ISO
C.
fgets()比得到的更差()?
废话。
-
Keith Thompson(The_Other_Keith) ks***@mib.org < http ://www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
fgets() is worse than gets()?
Nonsense.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
这篇关于scanf问题与摄入字符串* PLS。救命*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!