从空间字符输入读取字符串? [重复]

问题描述 投票:88回答:14

这个问题在这里已有答案:

我正在使用Ubuntu,我也使用Geany和CodeBlock作为我的IDE。我正在尝试做的是读取一个字符串(如"Barack Obama")并将其放在一个变量中:

#include <stdio.h>

int main(void)
{
    char name[100];

    printf("Enter your name: ");
    scanf("%s", name);
    printf("Your Name is: %s", name);

    return 0;
}

输出:

Enter your name: Barack Obama
Your Name is: Barack

如何让程序读取整个名称?

c string input scanf whitespace
14个回答
162
投票

使用:

fgets (name, 100, stdin);

100是缓冲区的最大长度。您应该根据需要进行调整。

使用:

scanf ("%[^\n]%*c", name);

[]是扫描集角色。 [^\n]告诉我,虽然输入不是新行('\n')接受输入。然后使用%*c从输入缓冲区读取换行符(未读取),*表示此读取输入被丢弃(赋值抑制),因为您不需要它,并且缓冲区中的这个换行符确实如此不会为您可能采取的下一个输入创建任何问题。

在这里阅读有关scansetassignment suppression运营商的信息。

注意你也可以使用gets但....

切勿使用gets()。因为如果事先不知道数据就不可能告诉你读取多少个字符,并且因为gets()将继续存储超过缓冲区末尾的字符,所以使用它是非常危险的。它已被用来打破计算机安全。请改用fgets()


0
投票

“巴拉克奥巴马”在“巴拉克”和“奥巴马”之间占有一席之地。为了适应这种情况,请使用此代码;

#include <stdio.h>
int main()
{
    printf("Enter your name\n");
   char a[80];
   gets(a);
   printf("Your name is %s\n", a);
   return 0;
}

0
投票

正确答案是这样的:

#include <stdio.h>

int main(void)
{
    char name[100];

    printf("Enter your name: ");
    // pay attention to the space in front of the %
    //that do all the trick
    scanf(" %[^\n]s", name);
    printf("Your Name is: %s", name);

    return 0;
}

在%之前的那个空间是非常重要的,因为如果你在程序中有另外几个scanf,假设你有一个整数值的scanf和另一个具有double值的scanf ...当你到达scanf的char时(字符串) name)该命令将被跳过,你不能为它输入值......但是如果你把那个空间放在%之前就行了,一切都没问题。


0
投票

虽然上面提到的方法确实有效,但每个方法都有它自己的问题。

如果您使用的是posix支持的平台,则可以使用getline()getdelim()。如果您使用Windows和minigw作为编译器,那么它应该可用。

getline()定义为:

ssize_t getline(char **lineptr, size_t *n, FILE *stream);

为了获取输入,首先需要创建一个指向char类型的指针。

#include <stdio.h>
#include<stdlib.h>

// s is a pointer to char type.
char *s;
// size is of size_t type, this number varies based on your guess of 
// how long the input is, even if the number is small, it isn't going 
// to be a problem
size_t size = 10;

int main(){
// allocate s with the necessary memory needed, +1 is added 
// as its input also contains, /n character at the end.
    s = (char *)malloc(size+1);
    getline(&s,&size,stdin);
    printf("%s",s);
    return 0;
}

样本输入:Hello world to the world!

输出:Hello world to the world!\n

这里需要注意的一点是,即使为s分配的内存是11个字节,其中输入大小为26个字节,getline使用s重新分配realloc()

所以输入的时间并不重要。

使用no.of字节读取更新size,如上面的示例输入size将是27

getline()也认为\n是输入。所以你的''将在最后持有'\ n'。

还有更为通用的getline()版本,它是getdelim(),它还需要一个额外的参数,即delimiter

getdelim()定义为:

ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);

Linux man page


-1
投票

"%s"将读取输入,直到到达空格。

如果你想读一行(即所有字符包括空格直到到达换行符),gets可能是一个好的起点。


-3
投票
scanf("%s",name);

使用&scanf输入


21
投票

试试这个:

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

\n只是为扫描的字符串设置分隔符。


5
投票

下面是一个如何使用fgets函数获取包含空格的输入的示例。

#include <stdio.h>

int main()
{
    char name[100];
    printf("Enter your name: ");
    fgets(name, 100, stdin); 
    printf("Your Name is: %s", name);
    return 0;
}

4
投票
scanf(" %[^\t\n]s",&str);

str是从中获取字符串的变量。


2
投票

注意:使用fgets()时,当您在CLI(命令行解释器)中对小输入使用fgets()时,数组中的最后一个字符将为'\ n',因为您使用'Enter'结束字符串。因此,当您打印字符串时,编译器将始终在打印字符串时转到下一行。如果您希望输入字符串具有类似行为的空终止字符串,请使用此简单的hack。

#include<stdio.h>
int main()
{
 int i,size;
 char a[100];
 fgets(a,100,stdin);;
 size = strlen(a);
 a[size-1]='\0';

return 0;
}

更新:在其他用户的帮助下更新。


1
投票
#include<stdio.h>
int main()
{
   char name[100];
   printf("Enter your name: ");
   scanf("%[^\n]s",name);
   printf("Your Name is: %s",name);
   return 0;
}

1
投票

使用此代码,您可以输入输入直到按下键盘输入。

char ch[100];
int i;
for (i = 0; ch[i] != '\n'; i++)
{
    scanf("%c ", &ch[i]);
}

1
投票
#include <stdio.h>
// read a line into str, return length
int read_line(char str[]) {
int c, i=0;
c = getchar();
while (c != '\n' && c != EOF) { 
   str[i] = c;
   c = getchar();
   i++;
}
str[i] = '\0';
return i;
}

1
投票

如果需要读取多行,需要清除缓冲区。例:

int n;
scanf("%d", &n);
char str[1001];
char temp;
scanf("%c",&temp); // temp statement to clear buffer
scanf("%[^\n]",str);
© www.soinside.com 2019 - 2024. All rights reserved.