代码之家  ›  专栏  ›  技术社区  ›  Haya Raed

文件的C++输出流不工作

  •  0
  • Haya Raed  · 技术社区  · 7 年前

    我的代码如下。缓冲区有数据,但有fout2。写什么都不做。文件已创建,并且为空。

    ofstream fout2(fname, ios::binary);
    fout2.open(fname, ios::binary | ios::in | ios::out);
    if (fout2.is_open()) {
        //problem is here   //write the buffer contents
        fout2.write(rmsg.buffer, rmsg.length);
        fout2.flush();
        memset(rmsg.buffer, 0, sizeof(rmsg.buffer)); //clear the buffer
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Barmar    7 年前

    如果您计划同时进行输入和输出,则使用 ios::in ,您应该使用 fstream ofstream open() .

    fstream fout2(fname, ios::binary | ios::in | ios::out);
    
        2
  •  1
  •   Michael K    7 年前

    fout2.close()
    

    或者简单地关闭fout2的范围:

    {
        ofstream fout2(fname, ios::binary);
        fout2.open(fname, ios::binary | ios::in | ios::out);
        if (fout2.is_open()) {
             fout2.write(rmsg.buffer, rmsg.length);
             //out2.flush(); // no need for this
             memset(rmsg.buffer, 0, sizeof(rmsg.buffer)); //clear the buffer
        }
    }