你如何允许输入空格使用scanf函数?
-
12-09-2019 - |
题
使用以下代码:
char *name = malloc(sizeof(char) + 256);
printf("What is your name? ");
scanf("%s", name);
printf("Hello %s. Nice to meet you.\n", name);
一个用户可以输入他们的名字,但是当他们进入一个名字与像Lucas Aardvark
空间,scanf()
只是Lucas
后切断了一切。如何使scanf()
允许空格
解决方案
人们(和尤其的初学者)不应该使用scanf("%s")
或gets()
或没有缓冲区溢出保护,除非你肯定知道输入将永远是一个特定格式的任何其他功能(也许甚至不那么)。
记住比scanf
代表“扫描格式化”,并有珍贵的小的少的格式不是用户输入的数据。这是理想的,如果你有输入数据格式的总控制,但通常不适合于用户的输入。
使用fgets()
(其中的的的缓冲区溢出保护),让您的输入字符串并sscanf()
对其进行评估。因为你只是想什么,用户无需输入解析,你并不真正需要sscanf()
在这种情况下无论如何:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Maximum name size + 1. */
#define MAX_NAME_SZ 256
int main(int argC, char *argV[]) {
/* Allocate memory and check if okay. */
char *name = malloc(MAX_NAME_SZ);
if (name == NULL) {
printf("No memory\n");
return 1;
}
/* Ask user for name. */
printf("What is your name? ");
/* Get the name, with size limit. */
fgets(name, MAX_NAME_SZ, stdin);
/* Remove trailing newline, if there. */
if ((strlen(name) > 0) && (name[strlen (name) - 1] == '\n'))
name[strlen (name) - 1] = '\0';
/* Say hello. */
printf("Hello %s. Nice to meet you.\n", name);
/* Free memory and exit. */
free (name);
return 0;
}
其他提示
尝试
char str[11];
scanf("%10[0-9a-zA-Z ]", str);
希望有所帮助。
这个例子使用倒置扫描集,所以scanf的不断取入的值,直到它遇到“\ N” - 换行,所以空间得到保存,以及
#include <stdio.h>
int main (int argc, char const *argv[])
{
char name[20];
scanf("%[^\n]s",name);
printf("%s\n", name);
return 0;
}
可以使用此
char name[20];
scanf("%20[^\n]", name);
或者这
void getText(char *message, char *variable, int size){
printf("\n %s: ", message);
fgets(variable, sizeof(char) * size, stdin);
sscanf(variable, "%[^\n]", variable);
}
char name[20];
getText("Your name", name, 20);
不要使用scanf()
而不指定字段宽度读取字符串。您也应该检查返回值是否正确:
#include <stdio.h>
#define NAME_MAX 80
#define NAME_MAX_S "80"
int main(void)
{
static char name[NAME_MAX + 1]; // + 1 because of null
if(scanf("%" NAME_MAX_S "[^\n]", name) != 1)
{
fputs("io error or premature end of line\n", stderr);
return 1;
}
printf("Hello %s. Nice to meet you.\n", name);
}
可替换地,使用fgets()
:
#include <stdio.h>
#define NAME_MAX 80
int main(void)
{
static char name[NAME_MAX + 2]; // + 2 because of newline and null
if(!fgets(name, sizeof(name), stdin))
{
fputs("io error\n", stderr);
return 1;
}
// don't print newline
printf("Hello %.*s. Nice to meet you.\n", strlen(name) - 1, name);
}
可以使用fgets()
函数来读取一个字符串或使用scanf("%[^\n]s",name);
所以串读数将在遇到一个新行字符终止。
如果有人还在寻找,这里是为我工作 - 读字符串包含空格的任意长度
由于网页共享这个简单优雅和溶液在许多海报。 如果成功要归功于他们,但任何错误都是我的。
char *name;
scanf ("%m[^\n]s",&name);
printf ("%s\n",name);
您可以使用scanf
为此与一个小窍门。其实,你应该允许用户输入,直到用户点击Enter键(\n
)。这将考虑中的每个字符,包括的空间的。下面是示例:
int main()
{
char string[100], c;
int i;
printf("Enter the string: ");
scanf("%s", string);
i = strlen(string); // length of user input till first space
do
{
scanf("%c", &c);
string[i++] = c; // reading characters after first space (including it)
} while (c != '\n'); // until user hits Enter
string[i - 1] = 0; // string terminating
return 0;
}
如何工作的吗?当用户输入的字符从标准输入,它们将被存储在的字符串的变量,直到第一空白空间。在此之后,进入的其余部分将留在输入流中,并等待下一个scanf函数。接下来,我们有一个for
循环,它从输入流(直到\n
)开炭由炭和apends他们结束的字符串的变量,从而形成一个完整的字符串与从键盘的用户输入。
希望这会帮助别人!
当你真的不应该使用scanf()
对于这样的事情,因为有更好的通话如gets()
或getline()
,是可以做到的:
#include <stdio.h>
char* scan_line(char* buffer, int buffer_size);
char* scan_line(char* buffer, int buffer_size) {
char* p = buffer;
int count = 0;
do {
char c;
scanf("%c", &c); // scan a single character
// break on end of line, string terminating NUL, or end of file
if (c == '\r' || c == '\n' || c == 0 || c == EOF) {
*p = 0;
break;
}
*p++ = c; // add the valid character into the buffer
} while (count < buffer_size - 1); // don't overrun the buffer
// ensure the string is null terminated
buffer[buffer_size - 1] = 0;
return buffer;
}
#define MAX_SCAN_LENGTH 1024
int main()
{
char s[MAX_SCAN_LENGTH];
printf("Enter a string: ");
scan_line(s, MAX_SCAN_LENGTH);
printf("got: \"%s\"\n\n", s);
return 0;
}
/*reading string which contains spaces*/
#include<stdio.h>
int main()
{
char *c,*p;
scanf("%[^\n]s",c);
p=c; /*since after reading then pointer points to another
location iam using a second pointer to store the base
address*/
printf("%s",p);
return 0;
}