为什么 printf 只打印字符串中的第一个单词?
Why does printf only print the first word in a string?
我对 C 编程还很陌生,所以我一直在尝试使用 fgets() 和 sscanf() 从文件中读取数据。我在这里设置了一个真正简单的实验:
#include <stdio.h>
#include <stdlib.h>
main()
{
char szInputBuffer[100];
FILE *pFile;
char szInput[100];
int i;
pFile = fopen("MyFile.txt", "r");
printf("This is the input: \n\n");
for (i = 0; i <= 2; ++i)
{
fgets(szInputBuffer, 100, pFile);
sscanf(szInputBuffer, "%s", szInput);
printf("%s", szInput);
}
}
我正在阅读 MyFile.txt,其中仅包含:
This is more input.
The next line of Input.
More input here.
我的输出是:
This is the input:
ThisTheMore
这是每行的第一个词。我发现当我像这样向 printf 语句添加第二个 %s 时:
printf("%s%s", szInput);
我达到了预期的输出:
This is the input:
This is more input.
The next line of Input.
More input here.
有人可以给我解释一下吗?我从未听说过使用第二个占位符来获取整个字符串。我在这里找不到任何有助于解决我的问题的东西。我在 class 中看到示例程序在 print 语句中只使用一个 %s 并打印整个字符串。感谢您帮助一位好奇的程序员!
P.S 我是 运行 Ubuntu 14.04 LTS,使用 Geany IDE。我知道你们中有些人会问这个问题。
sscanf(szInputBuffer, "%s", szInput);
格式说明符 %s
获取字符串,直到遇到 space 或换行符,因此您只需获取一个单词即可获取整行,只需在 [ 之后终止字符串即可=14=] 因为 fgets()
带有换行符并打印出字符串。
scanf() 人说 %s
Matches a sequence of non-white-space characters; the next pointer
must be a pointer to character array that is long enough to hold the
input sequence and the terminating null byte ('[=18=]'), which is added
automatically. The input string stops at white space or at the maximum
field width, whichever occurs first.
做
size_t n;
fgets(szInputBuffer, 100, pFile);
n = strlen(szInputBuffer);
if(n>0 && szInputBuffer[n-1] == '\n')
szInputBuffer[n-1] = '[=11=]';
printf("%s\n",szInputBuffer);
如果您希望将行分成字符串并打印出一行中的每个单词,请使用 space 作为分隔符的 strtok()
。
如果你看到
printf("%s%s", szInput);
工作然后你有什么是未定义的 behavior.printf() 说格式说明符的数量应该匹配需要打印的值的数量。请注意,即使类型不匹配,行为也是未定义的。
我对 C 编程还很陌生,所以我一直在尝试使用 fgets() 和 sscanf() 从文件中读取数据。我在这里设置了一个真正简单的实验:
#include <stdio.h>
#include <stdlib.h>
main()
{
char szInputBuffer[100];
FILE *pFile;
char szInput[100];
int i;
pFile = fopen("MyFile.txt", "r");
printf("This is the input: \n\n");
for (i = 0; i <= 2; ++i)
{
fgets(szInputBuffer, 100, pFile);
sscanf(szInputBuffer, "%s", szInput);
printf("%s", szInput);
}
}
我正在阅读 MyFile.txt,其中仅包含:
This is more input.
The next line of Input.
More input here.
我的输出是:
This is the input:
ThisTheMore
这是每行的第一个词。我发现当我像这样向 printf 语句添加第二个 %s 时:
printf("%s%s", szInput);
我达到了预期的输出:
This is the input:
This is more input.
The next line of Input.
More input here.
有人可以给我解释一下吗?我从未听说过使用第二个占位符来获取整个字符串。我在这里找不到任何有助于解决我的问题的东西。我在 class 中看到示例程序在 print 语句中只使用一个 %s 并打印整个字符串。感谢您帮助一位好奇的程序员!
P.S 我是 运行 Ubuntu 14.04 LTS,使用 Geany IDE。我知道你们中有些人会问这个问题。
sscanf(szInputBuffer, "%s", szInput);
格式说明符 %s
获取字符串,直到遇到 space 或换行符,因此您只需获取一个单词即可获取整行,只需在 [ 之后终止字符串即可=14=] 因为 fgets()
带有换行符并打印出字符串。
scanf() 人说 %s
Matches a sequence of non-white-space characters; the next pointer must be a pointer to character array that is long enough to hold the input sequence and the terminating null byte ('[=18=]'), which is added automatically. The input string stops at white space or at the maximum field width, whichever occurs first.
做
size_t n;
fgets(szInputBuffer, 100, pFile);
n = strlen(szInputBuffer);
if(n>0 && szInputBuffer[n-1] == '\n')
szInputBuffer[n-1] = '[=11=]';
printf("%s\n",szInputBuffer);
如果您希望将行分成字符串并打印出一行中的每个单词,请使用 space 作为分隔符的 strtok()
。
如果你看到
printf("%s%s", szInput);
工作然后你有什么是未定义的 behavior.printf() 说格式说明符的数量应该匹配需要打印的值的数量。请注意,即使类型不匹配,行为也是未定义的。