1

我正在开发一个创建 2000 个目录并在每个目录中放置一个文件(只有 10KB 左右的文件)的程序。我正在使用 mkdir 制作 dirs 和 ofstream (我也尝试过 fopen )将文件写入固态驱动器(我正在做速度测试以进行比较)。

当我运行代码时,目录创建良好,但文件在写入 1000 左右后停止写入。我尝试在每次写入之前延迟,以防它出现某种过载,并且还尝试使用 fopen 而不是 ofstream,但它总是在第 1000 个文件标记附近停止写入文件。

这是写入文件并退出的代码,告诉我它在哪个文件上失败了。

fsWriteFiles.open(path, ios::app); 
if(!fsWriteFiles.is_open()) 
{
   cout << "Fail at point: " << filecount  << endl; 
   return 1;
}
fsWriteFiles << filecontent;
fsWriteFiles.close();

有没有人有这方面的经验或有任何理论?

这是完整的代码:此代码从一个随机数创建一个 2 位十六进制目录,然后从一个随机数创建一个 4 位十六进制目录,然后在该目录中存储一个文件。在写入 1000 个文件后,它以“点失败”(我添加的一个 cout)退出。这表明它无法创建文件,但它应该已经检查过该文件不存在。有时它在 0 上失败,从底线开始第二个(文件已经存在的 else 子句)。任何帮助表示赞赏,我觉得这与我正在尝试创建的已经存在的文件有关,但是这些文件在我的文件存在检查中不知何故滑倒了。有没有办法为失败的文件创建尝试获取错误消息?

int main()
{
  char charpart1[3] = "";
  char charpart3[5] = "";

  char path[35] = "";
  int randomStore = 0;

  //Initialize random seed
  srand(time(NULL));
  struct stat buffer ;

  //Create output file streams
  ofstream fsWriteFiles;    
  ifstream checkforfile;

  //Loop X times
  int dircount = 0;
  while(dircount < 2000)
  {
    path[0] = '\0'; //reset the char array that holds the path

    randomStore = rand() % 255;
    sprintf(charpart1, "%.2x", randomStore);
    randomStore = rand() % 65535;
    sprintf(charpart3, "%.4x", randomStore);

    //Check if top level dir exists, create if not
    strcat(path, "fastdirs/");
    strcat(path, charpart1);
    DIR *pdir=opendir(path);

    //If the dir does not exist create it with read/write/search permissions for owner 
    // and group, and with read/search permissions for others
    if(!pdir)
      mkdir(path,  S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);

    //Check if 3rd level dir exists, create if not
    strcat(path, "/");
    strcat(path, charpart3);
    DIR *pdir3=opendir(path);

    if(!pdir3)
      mkdir(path,  S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);

    strcat(path, "/");
    strcat(path, charpart3);
    strcat(path, ".txt");
    //Write the file if it does not already exist
    checkforfile.open(path, fstream::in);

    if (checkforfile.is_open() != true)
    {
      fsWriteFiles.open(path, ios::app); 
      if(!fsWriteFiles.is_open()) 
      {
        cout << "Fail at point: " << dircount << "\n" << endl;
        return 1;
      }
      fsWriteFiles << "test";
      fsWriteFiles.flush();
      fsWriteFiles.close();

      dircount ++; //increment the file counter
    }
    else
    {
      cout << "ex";
      checkforfile.close();
    }
  } 
}
4

5 回答 5

6

您使用 opendir() 打开目录但从不使用 closedir() 关闭它们 - 我怀疑那里也存在资源限制。

于 2009-08-18T13:50:00.080 回答
1

你关闭文件吗?听起来您达到了通常为 1024 的文件描述符限制。要进行简单检查,请尝试“ulimit -n 4096”并再次执行您的程序。

于 2009-08-18T10:34:54.037 回答
1

我看不出发布的代码有任何明显错误,但是您没有显示足够的上下文来让任何人提供太多帮助。

但是,如果您使用 RAII 来确保文件已关闭,通过使用 ofstream 构造函数/析构函数来执行打开/关闭,我会更高兴:-

for (int i=0; i != MAX_FILES; ++i)
{     
  ofstream sWriteFiles(getFilePath(i), ios::app); 

  if (!fsWriteFiles.is_open()) 
  {
    cout << "Fail at point: " << filecount  << endl; 
    return 1;
  }

  fsWriteFiles << filecontent;
}

是的,您每次循环时都在“构造”ofstream 对象,但我发现这段代码更易于维护。

于 2009-08-18T11:01:04.870 回答
1

只是想确保您了解boost::filesystem。它可能会大大简化您的代码。

#include "boost/filesystem.hpp"   // includes all needed Boost.Filesystem declarations
#include <iostream>               // for std::cout
using boost::filesystem;          // for ease of tutorial presentation;
                                  //  a namespace alias is preferred practice in real code

path my_path( "some_dir/file.txt" );

remove_all( "foobar" );
create_directory( "foobar" );
ofstream file( "foobar/cheeze" );
file << "tastes good!\n";
file.close();
if ( !exists( "foobar/cheeze" ) )
  std::cout << "Something is rotten in foobar\n";
于 2009-08-18T13:54:41.637 回答
0

您有以下内容:

char charpart1[3] = "";
randomStore = rand() % 255;
sprintf(charpart1, "%.2x", randomStore);

您的意思是使用“%.2f”格式而不是 x 吗?%.2x 仍然打印溢出缓冲区的“3ff3c083”。此外,如果您切换到 %0.2f,您可能希望 charpart1 的大小为 5,而不是 3,因为它会写入 [0][.][2][3][\0] (例如)。

于 2009-08-18T14:00:49.023 回答