在字符串中遇到空格后程序未写入文件
Program is not writing to the file after a whitespace is encountered in the string
例如,如果我
输入:'stephen 8108' 它输出
'stephen'
而不是输出'stephen 8108'。
谁能帮帮我!
我希望完整的字符串出现在输出中。
它只读取字符串直到第一个白色 space。
即使我删除了 for 循环条件它似乎不起作用它仍然只读到第一个白色 space.
#include<fcntl.h>
#include<stdio.h>
#include <unistd.h>
void main()
{
char a[100];
int i,f2,f3,f4;
f2 = creat("a.txt",0666);
f3 = open("a.txt",O_RDWR);
printf("Enter your name & Roll-no\n");
scanf("%s",a);
for(i=0;a[i] != '[=10=]';i++);
write(f3,a,i);
close(f3);
}
在您的代码的当前状态下,for 循环没有循环体 { ... } ,因此写入和关闭操作只会执行一次。
此外,如果您希望 scanf 读取带空格的字符串,您可以使用 %[0-9a-zA-Z ] 而不是 %s
关于输入,至少有两个问题:
scanf()
的 s
转换一直解析到找到空格为止,它是这样记录的。
- 没有字段宽度,
scanf()
将在找不到空格时继续解析,溢出缓冲区 -> 未定义的行为。
快速修复是将 scanf("%s",a);
替换为 scanf("%99[^\n]",a);
。但是 scanf()
绝对不是 读取 输入的最佳工具,它是用于解析的。您似乎只想读取整行输入,并且已经有一个函数:fgets()
。在您的示例中使用它(如果您想使用这种去除换行符的方法,请包括 string.h
):
fgets(a, 100, stdin);
a[strcspn(a, "\n")] = 0; // remove the newline character if it was read by fgets
这是 sprintf
功能。
引用:http://www.cplusplus.com/reference/cstdio/scanf/
Any number of non-whitespace characters, stopping at the first whitespace character found.
一种选择是使用取反字符匹配(引用自上文link):
[^characters]
Negated scanset
Any number of characters none of them specified as characters between the brackets.
例如,要匹配除换行符之外的所有内容:
scanf("%[^\n]", a);
(下面是完整的工作示例 - 尽管请不要将其视为在 C++ 中读取用户输入的完整和完美的示例...)
#include<fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main()
{
char a[100];
int fp;
fp = open("a.txt", O_CREAT|O_WRONLY|O_TRUNC);
printf("Enter your name & Roll-no\n");
scanf("%[^\n]", a);
write(fp, a, strlen(a));
close(fp);
}
但是:我真的鼓励您阅读有关缓冲区溢出的广泛警告:
简而言之,确保您不允许用户写入超出您(当前 100 个字符长)内存分配的内容。
例如,如果我 输入:'stephen 8108' 它输出 'stephen' 而不是输出'stephen 8108'。 谁能帮帮我!
我希望完整的字符串出现在输出中。 它只读取字符串直到第一个白色 space。 即使我删除了 for 循环条件它似乎不起作用它仍然只读到第一个白色 space.
#include<fcntl.h>
#include<stdio.h>
#include <unistd.h>
void main()
{
char a[100];
int i,f2,f3,f4;
f2 = creat("a.txt",0666);
f3 = open("a.txt",O_RDWR);
printf("Enter your name & Roll-no\n");
scanf("%s",a);
for(i=0;a[i] != '[=10=]';i++);
write(f3,a,i);
close(f3);
}
在您的代码的当前状态下,for 循环没有循环体 { ... } ,因此写入和关闭操作只会执行一次。 此外,如果您希望 scanf 读取带空格的字符串,您可以使用 %[0-9a-zA-Z ] 而不是 %s
关于输入,至少有两个问题:
scanf()
的s
转换一直解析到找到空格为止,它是这样记录的。- 没有字段宽度,
scanf()
将在找不到空格时继续解析,溢出缓冲区 -> 未定义的行为。
快速修复是将 scanf("%s",a);
替换为 scanf("%99[^\n]",a);
。但是 scanf()
绝对不是 读取 输入的最佳工具,它是用于解析的。您似乎只想读取整行输入,并且已经有一个函数:fgets()
。在您的示例中使用它(如果您想使用这种去除换行符的方法,请包括 string.h
):
fgets(a, 100, stdin);
a[strcspn(a, "\n")] = 0; // remove the newline character if it was read by fgets
这是 sprintf
功能。
引用:http://www.cplusplus.com/reference/cstdio/scanf/
Any number of non-whitespace characters, stopping at the first whitespace character found.
一种选择是使用取反字符匹配(引用自上文link):
[^characters]
Negated scansetAny number of characters none of them specified as characters between the brackets.
例如,要匹配除换行符之外的所有内容:
scanf("%[^\n]", a);
(下面是完整的工作示例 - 尽管请不要将其视为在 C++ 中读取用户输入的完整和完美的示例...)
#include<fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main()
{
char a[100];
int fp;
fp = open("a.txt", O_CREAT|O_WRONLY|O_TRUNC);
printf("Enter your name & Roll-no\n");
scanf("%[^\n]", a);
write(fp, a, strlen(a));
close(fp);
}
但是:我真的鼓励您阅读有关缓冲区溢出的广泛警告:
简而言之,确保您不允许用户写入超出您(当前 100 个字符长)内存分配的内容。