导致此分段错误的原因

What causes this segmentation fault?

本文关键字:错误 分段      更新时间:2023-10-16
    string replace_strings (FILE *in, FILE *out, char *what, char *repl)
{
    int x = strlen(what);
    int z = strlen(repl);
    string newWhat(what, x);
    string newRepl(repl, z);
    char c;
    char *str; //наш буффер
    int i = 0;
    size_t found;

我知道这样做是一个糟糕的决定

    while(!feof(in))
    {
        while((c!='') && (i<=255))
        {
            str[i] = fscanf(in, "%c", c);
            i++;
        }
        string newStr (str, i);
        while(found != string::npos)
        {
            found = newStr.find(newWhat);
            newStr.replace(found, newWhat.length(), newRepl);
        }
        fprintf(out, "%s", newStr.c_str());

它返回分段错误,怎么了?我该怎么办?帮帮我伙计们

您必须为str分配内存。使用字符串流,忘记笨拙的缓冲区。