C++ fstream.write 分段错误

C++ fstream.write segmentation fault

我正在尝试将二进制数据写入文件,但出现分段错误。我从 sqlite blob 获取二进制数据。 我需要一些关于这段代码的帮助。 写入命令时出错。 sqlite 回调:

    static int callback(void* object, int, char** data, char**)
{
    if (fromdb* const art= static_cast<fromdb*>(object))
    {
        art->title = *data[1];
        art->creator = *data[2];
        art->bin = data[3];
        art->year = *data[4];
    }
    return 0;
}

写函数:

    void write() {
    // << bin;
    ofstream towrite;
    string name;
    cout << "Podaj nazwe pliku do zapisu";
    cin >> name;
    towrite.open(name, ios::out | ios::binary);
    towrite.write(bin, sizeof(bin));
    towrite.close();
}

sqlite3_exec() 只是 preapre/step/finalize 循环的包装器。

sqlite3_exec() documentation 说:

The 3rd argument to the sqlite3_exec() callback is an array of pointers to strings obtained as if from sqlite3_column_text(), one for each column.

该文档说:

These routines may only be called when the most recent call to sqlite3_step() has returned SQLITE_ROW and neither sqlite3_reset() nor sqlite3_finalize() have been called subsequently. If any of these routines are called after sqlite3_reset() or sqlite3_finalize() or after sqlite3_step() has returned something other than SQLITE_ROW, the results are undefined.

换句话说:字符串在回调后消失returns;您必须复制字符串的内容。