qscopedpointer qfile不会动态写入文件

时间:2017-10-16 14:15:49

标签: c++ smart-pointers qfile

写入文件时遇到问题。 在我的代码中,我有一个指向文件(私有类成员)的智能指针,我想写一个smth。 问题是写入函数写入缓冲区,缓冲区内容仅在我关闭应用程序时写入文件。为什么会这样?

这是我的代码:

void MyClass::setFile(const QString &name)
{
    m_file.reset(new QFile(name + "temp.txt"));
    m_file->open(QFile::WriteOnly);
}

void writeToFile()
{
    if(m_file != nullptr)
    {
        m_file->write((char *)(&m_str), sizeof(m_str));    // m_str is a struct
        m_file->flush();
    }
}

0 个答案:

没有答案