C语言 如何在C中读取带有空格的字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4025673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:48:39  来源:igfitidea点击:

How can I read a string with spaces in it in C?

cstringspaces

提问by Johny

scanf("%s",str) won't do it. It will stop reading at the first space. gets(str) doesn't work either when the string is large. Any ideas?

scanf("%s",str) 不会这样做。它将在第一个空格处停止阅读。当字符串很大时,gets(str) 也不起作用。有任何想法吗?

回答by NG.

use fgetswith STDIN as the file stream. Then you can specify the amount of data you want to read and where to put it.

使用带有 STDIN 的fgets作为文件流。然后,您可以指定要读取的数据量以及放置位置。

回答by sujeesh

char str[100];

Try this

尝试这个

 scanf("%[^\n]s",str);

or this

或这个

fgets(str, sizeof str, stdin))

回答by pmg

Create your own function to read a line. Here's what you basically have to do:

创建您自己的函数来读取一行。以下是您基本上必须做的事情:

1. fgets into allocated (growable) memory
2. if it was a full line you're done
3. grow the array
4. fgets more characters into the newly allocated memory
5. goto 2.

The implementation may be a bit tricky :-)

实现可能有点棘手:-)

You need to think about what you need to pass to your function (at the very least the address of the array and its size); and what the function returns when everything "works" or when there is an error. You need to decide what is an error (is a string 10Gbytes long with no '\n' an error?). You need to decide on how to grow the array.

您需要考虑需要传递给函数的内容(至少是数组的地址及其大小);以及当一切“正常”或出现错误时函数返回的内容。您需要确定什么是错误(是一个 10GB 长且没有 '\n' 错误的字符串吗?)。您需要决定如何扩展阵列。



Edit

编辑

Actually it may be better to fgetcrather than fgets

实际上它可能fgetcfgets

get a character
it it EOF? DONE
add to array (update length), possible growing it (update size)
is it '\n'? DONE
repeat

回答by Raakhee Bora

To read string with space you can do as follows:

要读取带空格的字符串,您可以执行以下操作:

char name[30],ch;

i=1;
while((ch=getchar())!='\n')
{
name[i]=ch;
i++;
}
i++;
name[i]='\n';
printf("String is %s",name);

回答by The Archetypal Paul

When do you want to stop reading? At EOF, at a specific character, or what?

你想什么时候停止阅读?在EOF,在特定字符,还是什么?

You can read a specific number of characters with %c

您可以使用 %c 读取特定数量的字符

c Matches a sequence of width count characters (default 1); the next pointer must be a pointer to char, and there must be enough room for all the characters (no terminating NUL is added). The usual skip of leading white space is suppressed. To skip white space first, use an explicit space in the format.

c 匹配宽度计数字符序列(默认为 1);next 指针必须是指向 char 的指针,并且必须有足够的空间容纳所有字符(不添加终止 NUL)。前导空白的通常跳过被抑制。要先跳过空格,请在格式中使用显式空格。

You can read specific characters (or up to excluded ones) with %[

您可以使用 %[ 读取特定字符(或最多排除的字符)

[ Matches a nonempty sequence of characters from the specified set of accepted characters; the next pointer must be a pointer to char, and there must be enough room for all the characters in the string, plus a terminating NUL character. The usual skip of leading white space is suppressed. The string is to be made up of characters in (or not in) a particular set; the set is defined by the characters between the open bracket [ character and a close bracket ] charac- ter. The set excludes those characters if the first character after the open bracket is a circumflex ^. To include a close bracket in the set, make it the first character after the open bracket or the circumflex; any other position will end the set. The hyphen character - is also special; when placed between two other characters, it adds all intervening characters to the set. To include a hyphen, make it the last character before the final close bracket. For instance, `[^]0-9-]' means the set ``everything except close bracket, zero through nine, and hyphen''. The string ends with the appearance of a character not in the (or, with a cir- cumflex, in) set or when the field width runs out

[ 匹配指定的一组接受字符中的非空字符序列;next 指针必须是指向 char 的指针,并且必须有足够的空间容纳字符串中的所有字符,加上一个终止 NUL 字符。前导空白的通常跳过被抑制。字符串将由特定集合中(或不在)中的字符组成;该集合由左括号 [ 字符和右括号 ] 字符之间的字符定义。如果左括号后的第一个字符是抑扬符 ^,则该集合将排除这些字符。要在集合中包含右括号,请将其放在左括号或抑扬符之后的第一个字符;任何其他位置都将结束该组。连字符 - 也是特殊的;当放置在其他两个字符之间时,它将所有中间字符添加到集合中。要包含连字符,请将其作为最后一个右括号之前的最后一个字符。例如,“[^]0-9-]”表示集合“除了右方括号、零到九和连字符之外的所有内容”。字符串以不在集合中(或,带有圆括号,在)集合中的字符出现或字段宽度用完时结束 使其成为最后一个右括号之前的最后一个字符。例如,“[^]0-9-]”表示集合“除了右方括号、零到九和连字符之外的所有内容”。字符串以不在集合中(或,带有圆括号,在)集合中的字符出现或字段宽度用完时结束 使其成为最后一个右括号之前的最后一个字符。例如,“[^]0-9-]”表示集合“除了右方括号、零到九和连字符之外的所有内容”。字符串以出现不在(或,带圆括号,in)集合中的字符或当字段宽度用完时结束