c++ - 是什么导致了这个段错误?

标签 c++ segmentation-fault

    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!='\0') && (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分配内存。使用stringstream ,并忘记笨拙的缓冲区。

关于c++ - 是什么导致了这个段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19622699/

相关文章:

c++ - 使用 CLANG 内联汇编创建 C++ 预自增操作

C++静态常量模板成员初始化

c++ - 显示文本文件

c++ - 如何嵌入 Webkit/Gecko 布局引擎并开始将其用于 UI?

c++ - 获取 C++ 段错误

c - SIGSEGV 的 sig 处理问题

c++ - 删除临时指针时出现段错误

c++ - unique_ptr 删除的 vector ?

c - 为什么 MAP_GROWSDOWN 映射不增长?

c++ - 将换行符添加到文本字段 (Win32)