Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.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 换行符导致的fgets()分段错误_C_Segmentation Fault_Fgets - Fatal编程技术网

C 换行符导致的fgets()分段错误

C 换行符导致的fgets()分段错误,c,segmentation-fault,fgets,C,Segmentation Fault,Fgets,我正在使用fgets()从stdin获取输入。我希望fgets()继续从用户获取输入,直到执行CTRL+D(EOF)命令。到达文件末尾后,应打印用户输入的数组 当用户在两行输入之间输入换行符(\n)时,在我尝试打印数组时会出现分段错误。我看过类似的StackOverflow文章,除了教程中的点描述,但是我仍然需要一些帮助来解决这个问题 int main(int argc, char **argv) { // Maximum input size int input_size=10

我正在使用fgets()从stdin获取输入。我希望fgets()继续从用户获取输入,直到执行CTRL+D(EOF)命令。到达文件末尾后,应打印用户输入的数组

当用户在两行输入之间输入换行符(\n)时,在我尝试打印数组时会出现分段错误。我看过类似的StackOverflow文章,除了教程中的点描述,但是我仍然需要一些帮助来解决这个问题

int main(int argc, char **argv) {
    // Maximum input size
    int input_size=1024;
    // Standard Input array
    char standard_input[input_size];
    // Input array
    char **input=malloc(input_size*sizeof(char*));
    // Integer to track input size
    int size=0;

    // Handle the input
    while(fgets(standard_input, input_size, stdin)){
        // Delimiter
        char delimit[]=" \n";
        // Node string
        char *my_node;
        my_node=strtok(standard_input, delimit);
        // Process the input from stdin
        for(int i=0; my_node!=NULL; i++){
            input[i]=malloc(strlen(my_node)+1);
            strcpy(input[i], my_node);
            // Special input processing
            // Ignore non-alpha entries
            // Convert uppercase to lowercase
            bool valid=true;
            for(int j=0; j<strlen(input[i]); j++){
                // Ignore non-alpha entries
                if(!isalpha(input[i][j]))
                    valid=false;
                // Convert uppercase to lowercase
                else
                    input[i][j]=tolower(input[i][j]);
            }
            my_node=strtok(NULL, delimit);
            // If the element was found to be invalid due to the presence
            // of non-alpha characters, remove it
            if(!valid){
                free(input[i]);
                i--;
            }
            else if(valid)
                size++;
        }
    }
    for(int i=0; i<size; i++)
        printf("%s\n", input[i]);
    // Memory cleanup
    // Free input to avoid memory leaks
    for(int i=0; i<size; i++){
        free(input[i]);
    }
    free(input);
    return 0; // return 0 to match the return type of the main function
}
int main(int argc,char**argv){
//最大输入大小
int input_size=1024;
//标准输入阵列
字符标准输入[输入大小];
//输入阵列
字符**输入=malloc(输入大小*大小(字符*);
//跟踪输入大小的整数
int size=0;
//处理输入
而(fgets(标准输入、输入大小、标准输入)){
//分隔符
字符分隔符[]=“\n”;
//节点字符串
char*my_节点;
my_node=strtok(标准_输入,定界);
//处理来自stdin的输入
for(int i=0;my_节点!=NULL;i++){
输入[i]=malloc(strlen(my_节点)+1);
strcpy(输入[i],我的_节点);
//特殊输入处理
//忽略非alpha项
//将大写字母转换为小写字母
bool valid=true;

对于(int j=0;j如果
strtok
返回
NULL
,会发生什么情况?另外,使用调试器捕获运行中的崩溃,并在代码中找到它发生的位置,并检查所有相关变量的值。如果仍然无法解决问题,请编辑问题以提供这些详细信息(崩溃位置和变量值)
for(inti=0;my_节点!=NULL;i++){input[i]=malloc(strlen(my_节点)+1);strcpy(input[i],my_节点);
:当输入多行时,
i
逐行重置。因此
大小
不匹配。@BLUEPIXY,这很有效。非常感谢。