简体   繁体   English

如何在C中读取包含空格的字符串?

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

scanf("%s",str) won't do it. scanf(“%s”,str)不会这样做。 It will stop reading at the first space. 它会在第一个空间停止阅读。 gets(str) doesn't work either when the string is large. 当字符串很大时,gets(str)也不起作用。 Any ideas? 有任何想法吗?

use fgets with STDIN as the file stream. 使用带有STDIN的fgets作为文件流。 Then you can specify the amount of data you want to read and where to put it. 然后,您可以指定要读取的数据量以及放置位置。

char str[100];

Try this 试试这个

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

or this 或这个

fgets(str, sizeof str, stdin))

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?). 您需要确定什么是错误(字符串长度为10Gbytes,没有'\\ n'错误?)。 You need to decide on how to grow the array. 您需要决定如何增长数组。


Edit 编辑

Actually it may be better to fgetc rather than fgets 实际上fgetc而不是fgets可能更好

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

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);

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); c匹配一系列宽度计数字符(默认值为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). 下一个指针必须是指向char的指针,并且必须有足够的空间容纳所有字符(不添加终止NUL)。 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. 下一个指针必须是指向char的指针,并且字符串中的所有字符必须有足够的空间,以及终止NUL字符。 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''. 例如,`[^] 0-9-]'表示设置``除了紧括号,零到九和连字符'之外的所有东西。 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 字符串以字符的外观结束,而不是(或者,带有cir-cumflex,in)设置或字段宽度用完时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM