您之前的代码:
char* get_input()
{
char c = 'a';
int counter = 1;
while(c != '\0')
{
c = getc(stdin);
char* d = (char)malloc(sizeof(char)*counter);
*(d+counter) = c;
counter++;
return d;
}
return d;
}
哦,不,不,不!每次调用malloc 时创建一个指向counter 大小的内存块的单独指针地址,将其分配给d,然后尝试将c 分配给*(d+counter)当前块,它完全无法知道 d 在上一次迭代中是什么 -- sigh...
首先,动态输入的方法与彩虹中的颜色一样多...它可以像使用 scanf 和 %m 转换说明符一样简单而无知(旧版本和 Windows 使用 @ 987654330@)。它带有scanf 带来的所有陷阱。但是,它可以工作:
char *get_input_scanf()
{
char *ln = NULL;
scanf ("%m[^\n]%*c", &ln);
return ln;
}
但是对数据输入没有任何限制,控制也很少。 (注意: 除非您将附加信息传递给任何输入函数或使用全局 #defines 或变量,否则任何输入法都会受到此限制。请考虑将最大长度传递给输入函数,或使确保您验证退货)
您也可以使用getchar() 或getc(fp) 逐个字符 阅读,或者您可以使用fgets 或@987654337 等line-input 方法@。使用这些方法中的任何一种(getline 除外),您都需要分配一个临时行缓冲区来保存输入,然后为了将分配限制为所需的数量,根据您的 strlen + 1 分配最终缓冲区输入(或仅使用strdup)。与所有动态方法一样,您负责跟踪它,将起始地址保存到内存块,并在不再需要时释放内存。除此之外,天空是您使用该功能的极限。
带有getline 的动态输入例程示例如下所示:
char *get_input()
{
char *ln = NULL; /* line buffer, NULL - getline allocates */
size_t n = 0; /* initial buff size, 0 - getline decides */
ssize_t nchr = 0; /* getline return - actual no. of chars read*/
if ((nchr = getline (&ln, &n, stdin)) != -1)
{
/* strip newline or carriage rtn */
while (nchr > 0 && (ln[nchr-1] == '\n' || ln[nchr-1] == '\r'))
ln[--nchr] = 0;
/* if (!nchr) { // do not accept blank lines
free (ln);
return NULL;
} */
char *input = strdup (ln); /* duplicate ln in input */
free (ln); /* free getline allocated mem */
return input;
}
return NULL;
}
注意:如上所述,getline 对输入字符串的长度没有限制,因此由您来验证。
您可以进行动态输入的不同方式的数量没有限制。 (SO 上可能至少有 1000 个示例)但是,这一切都归结为 character 或 line 输入,因此请选择所需的方法使用然后编写您的代码。这是一个具有上述两个不同功能的小型工作示例。查看帖子,如果您有任何问题,请告诉我。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *get_input();
char *get_input_scanf();
int main (void) {
char *line = NULL;
printf ("\nEnter input below, [ctrl+d] to quit\n");
for (;;)
{
printf ("\n input: ");
line = get_input();
if (line)
printf (" value: '%s'\n", line);
else {
printf ("\n value: [ctrl+d] received\n");
break;
}
free (line);
line = NULL;
}
if (line) free (line);
printf ("\n");
line = NULL;
printf ("\nEnter input below to read with get_input_scanf\n\n input: ");
line = get_input_scanf();
if (line) {
printf (" value: '%s'\n", line);
free (line);
}
return 0;
}
char *get_input()
{
char *ln = NULL; /* line buffer, NULL - getline allocates */
size_t n = 0; /* initial , 0 - getline decides*/
ssize_t nchr = 0;
if ((nchr = getline (&ln, &n, stdin)) != -1)
{
/* strip newline or carriage rtn */
while (nchr > 0 && (ln[nchr-1] == '\n' || ln[nchr-1] == '\r'))
ln[--nchr] = 0;
/* if (!nchr) { // do not accept blank lines
free (ln);
return NULL;
} */
char *input = strdup (ln); /* duplicate ln in input */
free (ln); /* free getline allocated mem */
return input;
}
return NULL;
}
char *get_input_scanf()
{
char *ln = NULL;
scanf ("%m[^\n]%*c", &ln);
return ln;
}
使用/输出示例
$ ./bin/getline_getinput
Enter input below, [ctrl+d] to quit
input: some string of input.
value: 'some string of input.'
input: another string that can be any length ........ ........
value: 'another string that can be any length ........ ........'
input:
value: ''
input: a
value: 'a'
input:
value: [ctrl+d] received
Enter input below to read with get_input_scanf
input: some input to show scanf will allocate dynamically as well!
value: 'some input to show scanf will allocate dynamically as well!'