第二个字符串未被扫描
Second string not getting scanned
#include<stdio.h>
void main()
{
char str1[100];
char str2[100];
printf("\n Enter the first String\n");
scanf("%[^\n]s",str1);
printf("Enter the second String");
scanf("%[^\n]s",str2);
printf("\n The strings are %s %d \n",str1,i);
}
嗯,问题是你按下 Enter,'\n'
保留在 stdin
中,它在第二个 scanf()
中作为字符串使用。
您可以在两个 scanf()
之间放置一个虚拟 getchar()
。这将解决问题,因为它会消耗之前 scanf
.
未消耗的 '\n'
你说你正在阅读 word
的方式 - 你基本上是在阅读由 '\n'
.
分隔的行
更好的方法是使用 fgets()
。它有两种方式解决 \n
消耗问题,另一件事是 fgets()
将读取一行并提供比 scanf()
.
更好的控制
"second string not getting scanned"
没有。事实并非如此,它正在读取您输入的上一行中的 \n
。
关于 fgets
,您还应该了解一些事情。它也会消耗 \n
。所以你的字符串将包含 \n
作为字符。如果你不想要那个,那么你可以这样做
str[strcspn(str,"\n")]='[=10=]'
char *fgets(char * restrict s, int n, FILE * restrict stream);
The fgets
function reads at most one less than the number of
characters specified by n
from the stream pointed to by stream
into the array pointed to by s
. No additional characters are read
after a new-line character (which is retained) or after end-of-file. A
null character is written immediately after the last character read
into the array.
还要检查fgets()
的return值,判断是否成功。
如果遇到 (EOF) 文件结尾且未读取任何字符,则 fgets
returns NULL
。
所以代码将是
if( fgets(str1, 100, stdin) ){
// successfully read the string.
str1[strcspn(str1,"\n")]='[=12=]'; ///removing `'\n'`
}
所以在这里您可以输入字符串,但同时输入 \n
。如果调用成功,我们正在覆盖它。
#include<stdio.h>
#include<string.h>
#define MAXLEN 100
int main()
{
char str1[MAXLEN];
char str2[MAXLEN];
printf("\n Enter the first line\n");
if( fgets(str1,MAXLEN,stdin) ){
str1[strcspn(str1,"\n")]='[=13=]';
}
else {
fprintf(stderr,"Line not read");
exit(EXIT_FAILURE);
}
printf("\n Enter the second line\n");
if( fgets(str2,MAXLEN,stdin) ){
str2[strcspn(str2,"\n")]='[=13=]';
}
else {
fprintf(stderr,"Line not read");
exit(EXIT_FAILURE);
}
printf("\n The strings are \n(%s) \n%s \n",str1,str2);
return EXIT_SUCCESS;
}
既然要读整行,就用fgets
。至少你对字符串长度输入有一些控制,不需要处理 scanf
特性
printf("\n Enter the first String\n");
fgets(str1, 100, stdin);
printf("Enter the second String");
fgets(str2, 100, stdin);
printf("\n The strings are %s %s \n",str1,str2);
请注意,尾随 \n
仍在字符串中(如果它们的长度最大为 98 个字符)。
Worth reading - scanf vs fgets
#include<stdio.h>
void main()
{
char str1[100];
char str2[100];
printf("\n Enter the first String\n");
scanf("%[^\n]s",str1);
printf("Enter the second String");
scanf("%[^\n]s",str2);
printf("\n The strings are %s %d \n",str1,i);
}
嗯,问题是你按下 Enter,'\n'
保留在 stdin
中,它在第二个 scanf()
中作为字符串使用。
您可以在两个 scanf()
之间放置一个虚拟 getchar()
。这将解决问题,因为它会消耗之前 scanf
.
'\n'
你说你正在阅读 word
的方式 - 你基本上是在阅读由 '\n'
.
更好的方法是使用 fgets()
。它有两种方式解决 \n
消耗问题,另一件事是 fgets()
将读取一行并提供比 scanf()
.
"second string not getting scanned"
没有。事实并非如此,它正在读取您输入的上一行中的 \n
。
关于 fgets
,您还应该了解一些事情。它也会消耗 \n
。所以你的字符串将包含 \n
作为字符。如果你不想要那个,那么你可以这样做
str[strcspn(str,"\n")]='[=10=]'
char *fgets(char * restrict s, int n, FILE * restrict stream);
The
fgets
function reads at most one less than the number of characters specified byn
from the stream pointed to bystream
into the array pointed to bys
. No additional characters are read after a new-line character (which is retained) or after end-of-file. A null character is written immediately after the last character read into the array.
还要检查fgets()
的return值,判断是否成功。
如果遇到 (EOF) 文件结尾且未读取任何字符,则 fgets
returns NULL
。
所以代码将是
if( fgets(str1, 100, stdin) ){
// successfully read the string.
str1[strcspn(str1,"\n")]='[=12=]'; ///removing `'\n'`
}
所以在这里您可以输入字符串,但同时输入 \n
。如果调用成功,我们正在覆盖它。
#include<stdio.h>
#include<string.h>
#define MAXLEN 100
int main()
{
char str1[MAXLEN];
char str2[MAXLEN];
printf("\n Enter the first line\n");
if( fgets(str1,MAXLEN,stdin) ){
str1[strcspn(str1,"\n")]='[=13=]';
}
else {
fprintf(stderr,"Line not read");
exit(EXIT_FAILURE);
}
printf("\n Enter the second line\n");
if( fgets(str2,MAXLEN,stdin) ){
str2[strcspn(str2,"\n")]='[=13=]';
}
else {
fprintf(stderr,"Line not read");
exit(EXIT_FAILURE);
}
printf("\n The strings are \n(%s) \n%s \n",str1,str2);
return EXIT_SUCCESS;
}
既然要读整行,就用fgets
。至少你对字符串长度输入有一些控制,不需要处理 scanf
特性
printf("\n Enter the first String\n");
fgets(str1, 100, stdin);
printf("Enter the second String");
fgets(str2, 100, stdin);
printf("\n The strings are %s %s \n",str1,str2);
请注意,尾随 \n
仍在字符串中(如果它们的长度最大为 98 个字符)。
Worth reading - scanf vs fgets