简体   繁体   English

fstream不会打印到文件

[英]fstream won't print to file

The following code will print something to a file 以下代码将内容打印到文件中

    std::fstream fout ("D_addr.txt", std::fstream::app);
    fout <<  pkt->Addr() << std::endl;
    flush(fout);
    fout.close();

While debugging, I watched pkt->Addr() and it has some values. 调试时,我看到了pkt->Addr() ,它具有一些值。 The fout line is passed without problem. fout顺利通过。 Also the file D_addr.txt is created. D_addr.txt创建文件D_addr.txt However after closing the file, the file size is zero! 但是,关闭文件后,文件大小为零! nothing has been written to it. 没有写任何东西。

Where is the problem? 问题出在哪儿?

This is not your actual code I guess and if it is I would start with that Addr() function of yours. 我猜这不是您的实际代码,如果是的话,我将从您的Addr()函数开始。

Note that fstream::close "closes the file currently associated with the object, disassociating it from the stream. Any pending output sequence is written to the physical file." 请注意, fstream :: close “关闭当前与对象关联的文件,将其从流中取消关联。任何未决的输出序列都将写入到物理文件中。” flush(fout); can be omitted. 可以省略。

You should also specify std::fstream::out flag. 您还应该指定std::fstream::out标志。 "If the function is called with any value in that parameter the default mode is overridden, not combined." “如果使用该参数中的任何值调用该函数,则默认模式将被覆盖,而不是合并。” So instead of std::fstream::app you should pass std::fstream::app | std::fstream::out 因此,而不是std::fstream::app你应该通过std::fstream::app | std::fstream::out std::fstream::app | std::fstream::out . std::fstream::app | std::fstream::out

I'm wondering if you're not using the wrong class. 我想知道您是否使用了错误的类。 If you want to write to a file, use std::ofstream , and not std::fstream . 如果要写入文件,请使用std::ofstream ,而不要使用std::fstream In particular, the constructor of std::ofstream forces the ios_base::out bit when calling rdbuf()->open ; 特别是,在调用rdbuf()->open时, std::ofstream的构造函数强制ios_base::out位; the constructor of std::fstream doesn't (so you're opening the file with neither read nor write access). std::fstream的构造函数没有(因此您要打开的文件既没有读取权限也没有写入权限)。

And you probably want to check the error status: did the open succeed, and after the close (or the flush), did all of the writes succeed. 您可能要检查错误状态:打开是否成功,关闭(或刷新)之后,所有写入是否成功。 The usual way of doing this is just: 通常的方法是:

if ( fout ) {
    //  All OK...
}

if ( !fout ) {
    //  Something went wrong.
}

After the open (the constructor), you can use fout.is_open() , which has the advantage of being a little bit more explicit with regards to what you are checking for. 打开(构造函数)之后,可以使用fout.is_open() ,其优点是要检查的内容更加明确。

First of all, flush() and fout.close() do not make any harm, but are not needed here, because when fout gets destroyed the file will be closed (and flushed) as part of fstream destructor. 首先, flush()fout.close()不会造成任何损害,但此处并不需要,因为当fout被销毁时,文件将作为fstream析构函数的一部分被关闭(并刷新)。

Second, you should use an ofstream or alternatively add the flag std::ios::out to the openmode parameter. 其次,您应该使用ofstream或将标志std::ios::outopenmode参数。

Try something along the lines of: 尝试以下方法:

{
  uint64_t x = 42;
  std::fstream of("test.txt", std::ios::app);
  of << x << std::endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM