我想读取用户输入的字符串.我不知道字符串的长度.由于CI中没有字符串声明指针:
char * word;
Run Code Online (Sandbox Code Playgroud)
并用于scanf
从键盘读取输入:
scanf("%s" , word) ;
Run Code Online (Sandbox Code Playgroud)
但是我遇到了分段错误.
当长度未知时,如何从C中读取键盘输入?
Pau*_*l R 54
你没有分配存储空间word
- 它只是一个悬空指针.
更改:
char * word;
Run Code Online (Sandbox Code Playgroud)
至:
char word[256];
Run Code Online (Sandbox Code Playgroud)
请注意,256是一个任意选择 - 此缓冲区的大小需要大于您可能遇到的最大可能字符串.
另请注意,fgets是一个更好(更安全)的选项,然后scanf用于读取任意长度的字符串,因为它接受一个size
参数,这反过来有助于防止缓冲区溢出:
fgets(word, sizeof(word), stdin);
Run Code Online (Sandbox Code Playgroud)
glg*_*lgl 19
我不明白为什么有建议在scanf()
这里使用.scanf()
只有在格式字符串中添加限制参数时才是安全的 - 例如%64s
左右.
更好的方法是使用char * fgets ( char * str, int num, FILE * stream );
.
int main()
{
char data[64];
if (fgets(data, sizeof data, stdin)) {
// input has worked, do something with data
}
}
Run Code Online (Sandbox Code Playgroud)
(另)
Dav*_*ica 15
当从任何不知道长度的文件(包括stdin)读取输入时,通常最好使用getline
而不是scanf
或fgets
因为getline
将自动处理字符串的内存分配,只要您提供空指针来接收输入的字符串.这个例子将说明:
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[]) {
char *line = NULL; /* forces getline to allocate with malloc */
size_t len = 0; /* ignored when line = NULL */
ssize_t read;
printf ("\nEnter string below [ctrl + d] to quit\n");
while ((read = getline(&line, &len, stdin)) != -1) {
if (read > 0)
printf ("\n read %zd chars from stdin, allocated %zd bytes for line : %s\n", read, len, line);
printf ("Enter string below [ctrl + d] to quit\n");
}
free (line); /* free memory allocated by getline */
return 0;
}
Run Code Online (Sandbox Code Playgroud)
相关部分是:
char *line = NULL; /* forces getline to allocate with malloc */
size_t len = 0; /* ignored when line = NULL */
/* snip */
read = getline (&line, &len, stdin);
Run Code Online (Sandbox Code Playgroud)
设置line
为NULL
使getline自动分配内存.示例输出:
$ ./getline_example
Enter string below [ctrl + d] to quit
A short string to test getline!
read 32 chars from stdin, allocated 120 bytes for line : A short string to test getline!
Enter string below [ctrl + d] to quit
A little bit longer string to show that getline will allocated again without resetting line = NULL
read 99 chars from stdin, allocated 120 bytes for line : A little bit longer string to show that getline will allocated again without resetting line = NULL
Enter string below [ctrl + d] to quit
Run Code Online (Sandbox Code Playgroud)
因此,getline
您无需猜测用户字符串的长度.
归档时间: |
|
查看次数: |
251929 次 |
最近记录: |