如何在C中扫描同一行中的字符串(带空格)和整数
我得到了一个“与1相同”的输入。这里,“SAME AS”是字符串,“1”是整数。如果它们在同一行中,如何将它们作为输入 使用[^\n]没有帮助,因为它还将“1”作为字符串的一部分。使用[^0-9]也无济于事,因为同一个scanf命令必须将输入作为“左”或“右”,而不是以数字结尾 有没有办法将[^\n]和[^0-9]组合起来?还有别的办法吗 注:无法更改输入。如何在C中扫描同一行中的字符串(带空格)和整数,c,scanf,C,Scanf,我得到了一个“与1相同”的输入。这里,“SAME AS”是字符串,“1”是整数。如果它们在同一行中,如何将它们作为输入 使用[^\n]没有帮助,因为它还将“1”作为字符串的一部分。使用[^0-9]也无济于事,因为同一个scanf命令必须将输入作为“左”或“右”,而不是以数字结尾 有没有办法将[^\n]和[^0-9]组合起来?还有别的办法吗 注:无法更改输入。scanf()不关心换行符 在某种程度上,你可以这样做: #include <stdio.h> int main(void)
scanf()
不关心换行符
在某种程度上,你可以这样做:
#include <stdio.h>
int main(void)
{
char buffer[64];
int rc;
int number;
while ((rc = scanf("%63[^\n0-9]%d", buffer, &number)) != EOF)
{
if (rc == 0)
{
printf("Nothing read!\n");
int c;
while ((c = getchar()) != EOF && c != '\n')
;
}
else if (rc == 1)
printf("Simple word - no number [[%s]]\n", buffer);
else
printf("Word and number [[%s]] %d\n", buffer, number);
}
return 0;
}
SAME AS 1
LEFT
RIGHT
123 ANGELS
程序内容如下:
Word and number [[SAME AS ]] 1
Nothing read!
Simple word - no number [[LEFT]]
Simple word - no number [[RIGHT]]
也可以在“单词和数字”报告之后添加“字符吞噬”循环。最好创建一个微函数来实现这一点:
static inline void gobble(void)
{
int c;
while ((c = getchar()) != EOF && c != '\n')
;
}
首先在数据文件中添加一行数字,如下所示:
#include <stdio.h>
int main(void)
{
char buffer[64];
int rc;
int number;
while ((rc = scanf("%63[^\n0-9]%d", buffer, &number)) != EOF)
{
if (rc == 0)
{
printf("Nothing read!\n");
int c;
while ((c = getchar()) != EOF && c != '\n')
;
}
else if (rc == 1)
printf("Simple word - no number [[%s]]\n", buffer);
else
printf("Word and number [[%s]] %d\n", buffer, number);
}
return 0;
}
SAME AS 1
LEFT
RIGHT
123 ANGELS
你会得到:
Word and number [[SAME AS ]] 1
Nothing read!
Simple word - no number [[LEFT]]
Word and number [[RIGHT]] 123
Simple word - no number [[ ANGELS]]
scanf()
函数族几乎不注意换行符或其他空白,除非您强制它们这样做。整个文件可以
也可以是在一行上,或者在自己的行上有每组非空白,并且将产生几乎相同的输出
你真的在乎新线吗
如果需要基于行的输入,最好先读取行,然后解析结果。这还有一个附带的好处,即您可以更连贯地报告错误。例如:
#include <stdio.h>
int main(void)
{
char line[4096];
while (fgets(line, sizeof(line), stdin) != NULL)
{
char buffer[64];
int number;
int rc = sscanf(line, "%63[^\n0-9]%d", buffer, &number);
if (rc == 0)
printf("Nothing read! (line = [[%s]])\n", line);
else if (rc == 1)
printf("Simple word - no number [[%s]]\n", buffer);
else if (rc == 2)
printf("Word and number [[%s]] %d\n", buffer, number);
else
printf("sscanf() returned %d - can't happen (but did)?\n", rc);
}
return 0;
}
第二个文件:
Word and number [[SAME AS ]] 1
Simple word - no number [[LEFT]]
Simple word - no number [[RIGHT]]
Nothing read! (line = [[123 ANGELS
]])
很难使用任何scanf()
函数系列,也很难避免上的尾随空格与数据相同-除非您利用“两个字加数字”或“一个字加数字”或“一个字”,这需要“读取行”示例(第二个程序)和多次尝试sscanf()
scanf()
函数系列非常、非常、非常难以准确使用。通常,阅读一行代码并使用sscanf()
有助于降低复杂性,但即使这样也不是完美的。有关此主题的更多想法,请参阅。scanf()
不关心换行符
在某种程度上,你可以这样做:
#include <stdio.h>
int main(void)
{
char buffer[64];
int rc;
int number;
while ((rc = scanf("%63[^\n0-9]%d", buffer, &number)) != EOF)
{
if (rc == 0)
{
printf("Nothing read!\n");
int c;
while ((c = getchar()) != EOF && c != '\n')
;
}
else if (rc == 1)
printf("Simple word - no number [[%s]]\n", buffer);
else
printf("Word and number [[%s]] %d\n", buffer, number);
}
return 0;
}
SAME AS 1
LEFT
RIGHT
123 ANGELS
程序内容如下:
Word and number [[SAME AS ]] 1
Nothing read!
Simple word - no number [[LEFT]]
Simple word - no number [[RIGHT]]
也可以在“单词和数字”报告之后添加“字符吞噬”循环。最好创建一个微函数来实现这一点:
static inline void gobble(void)
{
int c;
while ((c = getchar()) != EOF && c != '\n')
;
}
首先在数据文件中添加一行数字,如下所示:
#include <stdio.h>
int main(void)
{
char buffer[64];
int rc;
int number;
while ((rc = scanf("%63[^\n0-9]%d", buffer, &number)) != EOF)
{
if (rc == 0)
{
printf("Nothing read!\n");
int c;
while ((c = getchar()) != EOF && c != '\n')
;
}
else if (rc == 1)
printf("Simple word - no number [[%s]]\n", buffer);
else
printf("Word and number [[%s]] %d\n", buffer, number);
}
return 0;
}
SAME AS 1
LEFT
RIGHT
123 ANGELS
你会得到:
Word and number [[SAME AS ]] 1
Nothing read!
Simple word - no number [[LEFT]]
Word and number [[RIGHT]] 123
Simple word - no number [[ ANGELS]]
scanf()
函数族几乎不注意换行符或其他空白,除非您强制它们这样做。整个文件可以
也可以是在一行上,或者在自己的行上有每组非空白,并且将产生几乎相同的输出
你真的在乎新线吗
如果需要基于行的输入,最好先读取行,然后解析结果。这还有一个附带的好处,即您可以更连贯地报告错误。例如:
#include <stdio.h>
int main(void)
{
char line[4096];
while (fgets(line, sizeof(line), stdin) != NULL)
{
char buffer[64];
int number;
int rc = sscanf(line, "%63[^\n0-9]%d", buffer, &number);
if (rc == 0)
printf("Nothing read! (line = [[%s]])\n", line);
else if (rc == 1)
printf("Simple word - no number [[%s]]\n", buffer);
else if (rc == 2)
printf("Word and number [[%s]] %d\n", buffer, number);
else
printf("sscanf() returned %d - can't happen (but did)?\n", rc);
}
return 0;
}
第二个文件:
Word and number [[SAME AS ]] 1
Simple word - no number [[LEFT]]
Simple word - no number [[RIGHT]]
Nothing read! (line = [[123 ANGELS
]])
很难使用任何scanf()
函数系列,也很难避免上的尾随空格与数据相同-除非您利用“两个字加数字”或“一个字加数字”或“一个字”,这需要“读取行”示例(第二个程序)和多次尝试sscanf()
scanf()
函数系列非常、非常、非常难以准确使用。通常,阅读一行代码并使用sscanf()
有助于降低复杂性,但即使这样也不是完美的。有关此主题的更多想法,请参阅。发布未能澄清您遇到的问题的代码。您可以执行哪些限制?您最好的选择可能是“读取一行”,并使用sscanf()
,在必要时多次扫描。这就是“阅读第一行”的好处之一;稍后解析”-您可以多次尝试解析它。“%[^0-9]%d”
有什么问题?这将读取所有不是0-9
的数据,然后读取转换为int
的数字。然后,您只需从字符串末尾向后迭代,以覆盖在最后一个字母之后和遇到数字之前包含的任何意外空白。另一个简单的方法是简单地阅读整行代码,然后沿着缓冲区走一走,在你走的时候(你可以用一对指针解析任何东西)找出你所需要的东西,然后发布代码,这些代码无法让你所遇到的问题变得清晰。对你可以做的事情有哪些限制?您最好的选择可能是“读取一行”,并使用sscanf()
,在必要时多次扫描。这就是“阅读第一行”的好处之一;稍后解析”-您可以多次尝试解析它。“%[^0-9]%d”
有什么问题?这将读取所有不是0-9
的数据,然后读取转换为int
的数字。然后,您只需从字符串末尾向后迭代,以覆盖在最后一个字母之后和遇到数字之前包含的任何意外空白。另一种简单的方法是读取整行数据,然后沿着缓冲区遍历指针,在运行时找出所需内容(您可以用一对指针解析任何内容)