我正在开发一个创建 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();
}
}
}