Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 从控制台读取长度未知的字符串_C_Arrays_Malloc - Fatal编程技术网

C 从控制台读取长度未知的字符串

C 从控制台读取长度未知的字符串,c,arrays,malloc,C,Arrays,Malloc,如果我想从命令行读取任意长度的字符串,最好的方法是什么 目前我正在这样做: char name_buffer [ 80 ]; int chars_read = 0; while ( ( chars_read < 80 ) && ( !feof( stdin ) ) ) { name_buffer [ chars_read ] = fgetc ( stdin ); chars_read++; } char name_缓冲区[80]; int chars_read

如果我想从命令行读取任意长度的字符串,最好的方法是什么

目前我正在这样做:

char name_buffer [ 80 ];
int chars_read = 0;
while ( ( chars_read < 80 ) && ( !feof( stdin ) ) ) {
   name_buffer [ chars_read ] = fgetc ( stdin );
   chars_read++;
}
char name_缓冲区[80];
int chars_read=0;
而((字符数<80)和(!feof(stdin))){
名称缓冲区[字符读取]=fgetc(标准输入法);
chars_read++;
}
但是,如果字符串长度超过80个字符,我该怎么办?显然,我可以将数组初始化为一个更大的数字,但我确信一定有更好的方法使用malloc或其他方法来为数组提供更多空间


任何提示都很好。

使用
realloc()
分配缓冲区,并在缓冲区已满时进行扩展。

很久以前在网上的某个地方发现了这一点,它非常有用:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    unsigned int len_max = 128;
    unsigned int current_size = 0;

    char *pStr = malloc(len_max);
    current_size = len_max;

    printf("\nEnter a very very very long String value:");

    if(pStr != NULL)
    {
    int c = EOF;
    unsigned int i =0;
        //accept user input until hit enter or end of file
    while (( c = getchar() ) != '\n' && c != EOF)
    {
        pStr[i++]=(char)c;

        //if i reached maximize size then realloc size
        if(i == current_size)
        {
                        current_size = i+len_max;
            pStr = realloc(pStr, current_size);
        }
    }

    pStr[i] = '\0';

        printf("\nLong String value:%s \n\n",pStr);
        //free it 
    free(pStr);
    pStr = NULL;


    }
    return 0;
}
#包括
#包括
int main()
{
无符号整数len_max=128;
无符号整数当前大小=0;
char*pStr=malloc(len_max);
当前尺寸=长度最大值;
printf(“\n输入一个非常长的字符串值:”);
如果(pStr!=NULL)
{
int c=EOF;
无符号整数i=0;
//接受用户输入,直到按enter键或文件结束
而((c=getchar())!='\n'&&c!=EOF)
{
pStr[i++]=(char)c;
//如果我达到最大化大小,那么realloc大小
如果(i==当前_大小)
{
当前尺寸=i+长度最大值;
pStr=realloc(pStr,当前大小);
}
}
pStr[i]='\0';
printf(“\n长字符串值:%s\n\n”,pStr);
//释放它
免费(pStr);
pStr=NULL;
}
返回0;
}

可能重复感谢,太棒了!为什么c在开始时设置为EOF?为什么它是int而不是char?它被设置为EOF以最初标记输入字符串的最后一点,并且它是int,因为输入首先采用ASCII值。如果您发现答案有用,请将其标记为正确并进行放弃投票。提前谢谢。啊,好的。谢谢我已经接受了,但不能投票支持,抱歉,为什么兄弟?你能告诉我原因吗?