Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
QFile:同一物理文件的多个句柄不会写入所有数据 我想知道当多个句柄被打开到同一个文件时, qfile < /> >如何运行(在Windows 7上使用Visual Studio 2013中的C++),所以我编写了以下小程序: QFile file("tmp.txt"); file.open(QIODevice::WriteOnly | QIODevice::Truncate); QTextStream ts(&file); ts << "Hallo\n"; QFile file2("tmp.txt"); file2.open(QIODevice::WriteOnly | QIODevice::Append); QTextStream ts2(&file2); ts2 << "Hallo 2\n"; file.close(); ts2 << "Hello again\n"; file2.close();._C++_Qt_File_Qfile - Fatal编程技术网

QFile:同一物理文件的多个句柄不会写入所有数据 我想知道当多个句柄被打开到同一个文件时, qfile < /> >如何运行(在Windows 7上使用Visual Studio 2013中的C++),所以我编写了以下小程序: QFile file("tmp.txt"); file.open(QIODevice::WriteOnly | QIODevice::Truncate); QTextStream ts(&file); ts << "Hallo\n"; QFile file2("tmp.txt"); file2.open(QIODevice::WriteOnly | QIODevice::Append); QTextStream ts2(&file2); ts2 << "Hallo 2\n"; file.close(); ts2 << "Hello again\n"; file2.close();.

QFile:同一物理文件的多个句柄不会写入所有数据 我想知道当多个句柄被打开到同一个文件时, qfile < /> >如何运行(在Windows 7上使用Visual Studio 2013中的C++),所以我编写了以下小程序: QFile file("tmp.txt"); file.open(QIODevice::WriteOnly | QIODevice::Truncate); QTextStream ts(&file); ts << "Hallo\n"; QFile file2("tmp.txt"); file2.open(QIODevice::WriteOnly | QIODevice::Append); QTextStream ts2(&file2); ts2 << "Hallo 2\n"; file.close(); ts2 << "Hello again\n"; file2.close();.,c++,qt,file,qfile,C++,Qt,File,Qfile,因此,第一个Hallo语句丢失了。如果我在ts.flush()之后执行ts.flush() 阅读资料来源:有几件可疑的事情正在发生。首先,您将引入两个级别的缓冲 首先也是最重要的是,QTextStream有一个内部缓冲区,您可以通过调用flush对其进行刷新 其次,QFile也在进行缓冲(或者更好的是,它使用库中的缓冲API--fopen,fwrite,等等)。传递到open,使其使用未缓冲的API(open,write,…) 现在,由于这非常容易出错,QTextStream::flush 此外

因此,第一个
Hallo
语句丢失了。如果我在
ts.flush()之后执行
ts.flush()

阅读资料来源:

有几件可疑的事情正在发生。首先,您将引入两个级别的缓冲

  • 首先也是最重要的是,QTextStream有一个内部缓冲区,您可以通过调用
    flush
    对其进行刷新

  • 其次,QFile也在进行缓冲(或者更好的是,它使用库中的缓冲API--
    fopen
    fwrite
    ,等等)。传递到
    open
    ,使其使用未缓冲的API(
    open
    write
    ,…)

  • 现在,由于这非常容易出错,QTextStream::flush

    此外,您正在传递
    WriteOnly | Append
    ,这没有意义。这只是其中的一个


    但是,请注意,您的写入仍可能交错

    如果在一个操作中写入的全部数据没有与来自任何其他进程的数据交错,则写入是原子的。当有多个写入程序向单个读卡器发送数据时,这非常有用。应用程序需要知道以原子方式执行的写入请求的大小。这个最大值称为{PIPE_BUF}


    (在Windows上,我不知道)。

    我似乎你想要两种方式

    如果您想要多个写入缓冲区,并且不想刷新它们,则很难确保文件中的所有写入都是正确的顺序。
    您使用
    std::basic_ostream
    进行的小测试并不是一个证明:它是否适用于较大的写入?它会在其他操作系统上工作吗?您是否想冒险让您的流程获得(尚未证实的)速度增益?

    std::endl;刚刚测试它时没有使用
    std::endl
    -相同的行为(后期编辑)相同的行为-为什么二进制文件的参数顺序或相关性?抱歉,请参阅我的更新。你能试试吗?ReadWrite invoke Write调用Truncate。在这里找到它:就这样。好的,我把它改成了
    文件。打开(QIODevice::WriteOnly)
    文件2。打开(QIODevice::Append)
    ,但在两个缓冲级别上还是一样的行为:你说的有意义,但是使用
    QTextStreams
    实际上也是在Qt文档中实现的。FWIW,如果我不使用流,但直接使用
    file.write()
    ,我也会经历同样的行为。关于标志:你是对的,我将其更改为仅使用
    QIODevice::Append
    并添加了QIODevice::Unbuffered,但它不会更改结果。因此,如果我使用
    QIODevice::Unbuffered
    并直接使用
    file.write()
    对文件进行操作,那么它将按照你的建议工作。然而,正如我在最初的帖子中指出的,我想找出不使用刷新(或者在本例中,使用缓冲区)来提高性能的方法。那么STL是否更优越呢?
    Hallo 2
    Hello again
    
      std::basic_ofstream<char> file;
      file.open("tmp.txt", std::ios_base::out | std::ios_base::ate | std::ios_base::app);
      file << "Hallo\n";
    
      std::basic_ofstream<char> file2;
      file2.open("tmp.txt", std::ios_base::out | std::ios_base::ate | std::ios_base::app);
      file2 << "Hallo 2\n";
    
      file.close();
    
      file2 << "Hello again\n";
    
      file2.close();
    
    Hallo
    Hallo 2
    Hello again
    
     QFile file("tmp.txt");
      file.open(QIODevice::WriteOnly | QIODevice::Truncate);
      QTextStream ts(&file);
      ts << "Hallo\n";
    
      QFile file2("tmp.txt");
      file2.open(QIODevice::Append);
      QTextStream ts2(&file2);
      ts2 << "Hallo 2\n";
    
      file.close();
    
      ts2 << "Hello again\n";
    
      file2.close();
    
    QIODevice::WriteOnly    0x0002  The device is open for writing. Note that this mode implies Truncate.
    QIODevice::Truncate 0x0008  If possible, the device is truncated before it is opened. All earlier contents of the device are lost.