C++ FStream 抛出异常错误

标签 c++ fstream

我正在使用 WinSock 从 Internet 下载我的文件。

这里的代码演示了我从 header 中获取内容大小,而不是删除 header 并将其余部分写入应用程序。 FileDownload1.write(...) 它会引发访问冲突读取错误。

这段代码有什么问题吗? (我习惯了 C 风格的字符串,所以我还不是 100% 熟悉 C++ 标准字符串。

这是我的一些代码:

    DWORD WINAPI DownloadFile(LPVOID VoidAtt){

    ...

    postion = TextBuffer.find("Content-Length: ", 0);
    std::string LengthOfFile = TextBuffer.substr(postion + strlen("Content-Length: "), 7);
    int FileSize = std::stoi(LengthOfFile, nullptr, 10);
    postion = TextBuffer.find("\r\n\r\n", 0);
    std::string memoryblock = TextBuffer.substr(postion + 4, -1);
    std::ofstream FileDownload1;

    FileDownload1.open("64bit1.m4a", std::ios::out | std::ios::binary);
    FileDownload1.write(memoryblock.c_str(), FileSize);
    FileDownload1.close();

    SetWindowTextA(hTextBox, &TextBuffer[0]);
}

如果你需要它,请告诉我,(但是完整的源代码有点乱,因为我只是想搞清楚如何下载文件并将其成功写入计算机。

最佳答案

第二个写入参数必须是内存大小:

 FileDownload1.write(memoryblock.c_str(), memoryblock.size());

参见:fstream::write

关于C++ FStream 抛出异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35478664/

相关文章:

c++ - 使用 popen 代替 "write to file"然后 "use ifstream to read from it"c++

c++ - Qt designer如何抓取信息

c++ - R 和 C++ 迭代

c++ - 将指针传递给结构在两个程序中的行为不同

c++ - 在 Android 上使用 OpenCV 将 cv::Mat 传递给 JNI 时出错

c++ - 如何在C++中读取然后编辑文件txt

c++ - 当我将它放在 while 语句之外时,为什么我的 ofstream 不起作用?

c++ - 使用 GLM 和 GTK 制作简单的 3D 绘图

c++ - Ifstream读取无用数据

c++ - 以相反的顺序从文本文件中读取行 C++