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

发布于 2024-09-29 08:08:31 字数 76 浏览 9 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

嗳卜坏 2024-10-06 08:08:31

使用 fgets 和 STDIN 作为文件流。然后您可以指定要读取的数据量以及将其放置在何处。

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

流云如水 2024-10-06 08:08:31
char str[100];

试试这个

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

或这个

fgets(str, sizeof str, stdin))
char str[100];

Try this

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

or this

fgets(str, sizeof str, stdin))
海风掠过北极光 2024-10-06 08:08:31

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

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.

实现可能有点棘手:-)

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


编辑

实际上,fgetc 可能比 fgets 更好

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

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.


Edit

Actually it may be better to fgetc rather than fgets

get a character
it it EOF? DONE
add to array (update length), possible growing it (update size)
is it '\n'? DONE
repeat
百思不得你姐 2024-10-06 08:08:31

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

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

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

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

[ 匹配非空序列
指定集合中的字符
接受的字符;下一个指针必须是指向
炭,
并且必须有足够的空间容纳所有角色
细绳,
加上一个 NUL 终止字符。通常跳过领先
白色的
空间被压制。该字符串由字符组成

(或不在)特定集合中;该集合定义为
人物
位于左括号 [ 字符和右括号 ] 之间
特征-
之三。如果第一个字符集排除这些字符
特点
左括号后面是抑扬符 ^。包括关闭
集合中的括号,使其成为开号后的第一个字符
方括号或扬抑符;任何其他位置都将结束该组。
连字符 - 也很特殊;当放置在两个之间时
其他字符,它将所有中间字符添加到集合中。
要包含连字符,请将其作为最后一个字符之前的最后一个字符
关闭括号。例如,“[^]0-9-]”表示集合
``一切
除了右括号、零到九和连字符''。这
细绳
以不在 中的角色出现而结束(或者,以
循环
cumflex, in) 设置或当字段宽度用完时

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

You can read a specific number of characters with %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.

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

不美如何 2024-10-06 08:08:31

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

char name[30],ch;

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文