简体   繁体   English

C ++ ofstream指针无法写入磁盘

[英]c++ ofstream pointer fails to write to disk

I am having trouble writing to ofstream pointer and this is quite perplexing as I really don't see anything that is missing anymore. 我在写入ofstream指针时遇到麻烦,这非常令人困惑,因为我真的看不到任何丢失的东西。 Note, this is a follow up from this question: C++ vector of ofstream, how to write to one particular element 请注意,这是该问题的后续内容: ofstream的C ++向量,如何写入一个特定元素

My code is as follows: 我的代码如下:

std::vector<shared_ptr<ofstream>> filelist;

void main()
{
  for(int ii=0;ii<10;ii++)
  {
     string filename = "/dev/shm/table_"+int2string(ii)+".csv";
     filelist.push_back(make_shared<ofstream>(filename.c_str()));
  }

  *filelist[5]<<"some string"<<endl;
  filelist[5]->flush();
  exit(1);

}

This does doesn't write anything to the output file but it does create 10 empty files. 这不会向输出文件写入任何内容,但会创建10个空文件。 Does anybody know what might possibly be wrong here? 有人知道这里可能有什么问题吗?

EDIT: I ran some further tests. 编辑:我进行了一些进一步的测试。 I let the code run without exit(1) until completion, over all files until all callbacks are finished. 我让代码在没有退出(1)的情况下运行,直到完成为止,在所有文件上直到所有回调完成为止。 It turns out that some files are not empty, while others that should have data are empty. 事实证明,有些文件不为空,而其他应包含数据的文件为空。

There is plenty of disk space, and I know I have more file descriptors than are necessary for this. 有足够的磁盘空间,而且我知道我的文件描述符比为此所需的要多。 Any explanation for why some of the files would be written properly while others are not? 为什么某些文件可以正确写入而另一些文件却不能正确写入任何解释?

I'd try: (*filelist[5])<<"some string\\n"; 我会尝试: (*filelist[5])<<"some string\\n"; .

I'd guess, however, that you probably meant to write to the files inside a loop -- as-is, you're writing to only one file. 但是,我猜想您可能打算在循环中写入文件-照原样,您只在写入一个文件。

Oh, and in C++, you don't want to use exit . 哦,在C ++中,您不想使用exit

Edit: Here's a quick (tested) standalone demo: 编辑:这是一个快速(经过测试)的独立演示:

#include <fstream>
#include <string>
#include <vector>

std::vector<std::ofstream *> filelist;

int main() {
  for(int ii=0;ii<3;ii++)
  {
    char *names[] = {"one", "two", "three"};
     std::string filename = "c:\\trash_";
     filename += names[ii];
     filename += ".txt";
     filelist.push_back(new std::ofstream(filename.c_str()));
  }

  for (int i=0; i<filelist.size(); i++) {
    (*filelist[i])<<"some string\n";
    filelist[i]->close();
  }
}

Note, however, that the file name this generates is for Windows, whereas the original was (apparently) intended for something Unix-like. 但是请注意,此生成的文件名是Windows的,而原始文件(显然)是供Unix之类的。 For a Unix-like OS, you'll need/want a different file name string. 对于类似Unix的OS,您将需要/想要一个不同的文件名字符串。

Try closing the file before you call exit with filelist[5]->close(); 尝试在使用filelist[5]->close();调用exit之前关闭文件filelist[5]->close(); . You've aborted a process with an open file which means your write may not have made it to the OS buffer or was discarded upon process exit. 您已中止了带有打开文件的进程,这意味着您的写操作可能尚未写入OS缓冲区,或者在进程退出时被丢弃。 You could also remove the exit call it would probably fix the problem. 您也可以删除退出调用,这可能会解决问题。 The results of IO on a process that is aborted are tricky to nail down, so it's best to try avoiding aborts with active IO or to assume any active IO will fail upon abort. 要确定失败的进程上的IO的结果很困难,因此最好尝试避免使用活动IO进行中止,或者假定任何活动IO在中止时都会失败。

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

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