I'm working on a program that creates 2000 directories and puts a file in each (just a 10KB or so file). I am using mkdir to make the dirs and ofstream (i tried fopen as well) to write the files to a solid state drive (i'm doing speed tests for comparison).
When I run the code the directories are created fine but the files stop writing after 1000 or so have been written. I've tried putting a delay before each write in case it was some kind of overload and also tried using fopen instead of ofstream but it always stops writing the files around the 1000th file mark.
this is the code that writes the files and exits telling me which file it failed on.
fsWriteFiles.open(path, ios::app);
if(!fsWriteFiles.is_open())
{
cout << "Fail at point: " << filecount << endl;
return 1;
}
fsWriteFiles << filecontent;
fsWriteFiles.close();
Has anyone had any experience of this or has any theories?
Here's the full code: This code creates a 2 digit hex directory from a random number then a 4 digit hex directory from a random number then stores a file in that directory. It exits with a 'fail at point' (a cout I've added) after writing 1000ish files. This indicates that it cannot create a file, but it should have already checked that the file does not exist. Sometimes it fails on 0 having hit the second from bottom line (else clause for file already existing). any help appreciated, I feel that it is to do with the files i'm trying to create already existing but that have somehow slipped by my file existence check. Is there a way to get an error message for a failed file creation attempt?
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();
}
}
}