Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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
Strcpy()分段错误_C_String_Strcpy - Fatal编程技术网

Strcpy()分段错误

Strcpy()分段错误,c,string,strcpy,C,String,Strcpy,我创建了一个函数,它搜索文件的每一行,并尝试将参数par_string与文件中的字符串匹配。我有用于double和int的函数变体,但我似乎无法修改该函数以从文件中读取字符串 目前,我正在使用sscanf()读取一行,我目前的工作是使用strcpy()将文件中的字符串复制到函数参数中定义的字符串参数parameter。然而,一旦我到达strcpy(),我的函数就会出现seg错误,我不太明白为什么 我希望能够调用函数作为read\u字符串(“FILENAME”,FILENAME)可以将参数传递给函

我创建了一个函数,它搜索文件的每一行,并尝试将参数
par_string
与文件中的字符串匹配。我有用于double和int的函数变体,但我似乎无法修改该函数以从文件中读取字符串

目前,我正在使用
sscanf()
读取一行,我目前的工作是使用
strcpy()
将文件中的字符串复制到函数参数中定义的字符串参数
parameter
。然而,一旦我到达strcpy(),我的函数就会出现seg错误,我不太明白为什么

我希望能够调用函数作为
read\u字符串(“FILENAME”,FILENAME)可以将参数传递给函数,并将文件名返回到变量
filename
。我尝试使用

char filename[MAXLINE]

但两者都有同样的问题

下面是我编写的函数。有没有什么明显的遗漏,或者有没有更好的方法

谢谢

int 
read_string(char par_string[], char *parameter)
{
    char line[MAX_LINE], ini_par_name[MAX_LINE], par_separator[MAX_LINE];
    char par_value[MAX_LINE];

    FILE *par_file;

    if ((par_file = fopen(INI_FILE, "r")) == NULL)
    {
        printf("Cannot open parameter file 'plane.ini'.\n");
        exit(-1);
    }

    int linenum = 0;
    parameter = STRING_NO_PAR_CONST;

    while(fgets(line, MAX_LINE, par_file) != NULL)
    {        
        linenum++;

        /* 
        * If the line is a comment, skip that line. Note: There is a bug here
        * which will cause the script to crash if there is a blank line.
        */
        if (line[0] == '#' || line[0] == ' ')
        {
            continue;
        }

        /* 
        * Check a normal line is in the correct format
        */
        if (sscanf(line, "%s %s %s", ini_par_name, par_separator, par_value) \
            != 3)
        {
            printf("Syntax error, line %d for parameter %s\n", linenum,
                par_string);
            exit(-1);
        }

        /* 
        * Use strcmp to compare the difference between two strings. If it's
        * the same parameter, then strcmp will return 0. 
        */
        if (strcmp(par_string, ini_par_name) == 0)
        {
            strcpy(parameter, par_value);
        }
    }

    /* 
    * If parameter wasn't updated, the parameter was not found. Return an
    * error. 
    */
    if (parameter == STRING_NO_PAR_CONST)
    {
        printf("Parameter %s could not be found.\nExiting simulation.\n",
            par_string);
        exit(-1);
    }

    if (fclose(par_file) != 0)
    {
        printf("File could not be closed.\n");
        exit(-1);
    }

    return 0;
}

这条线在干什么

parameter = STRING_NO_PAR_CONST;
如果指定
参数
指向字符串文字,这可能是您的问题,您无法修改字符串文字的内容

如果
STRING\u NO\u PAR\u CONST
'\0'
,那只是
char
,而不是
STRING
。只要在调用
read_string()
之前正确分配了
参数
,您就可以

parameter[0] = STRING_NO_PAR_CONST
当你检查它是否改变时也是如此

if (parameter[0] == STRING_NO_PAR_CONST)
几件事

strcpy被称为->strcpy(目标,源)。您正在使用参数作为目标


也就是说,您分配的空间量应该是您想要的字符串的最大大小。我不认为它应该等于给定文件中的最大行数(尽管我可能错了)

啊,我明白了<代码>字符串\u NO\u PAR\u CONST='\0'
。我在另一个函数中使用了类似的东西来检查是否找到了变量。但它解决了我的问题,非常感谢!
if (parameter[0] == STRING_NO_PAR_CONST)