I try to handle the cases when I try to write a file that already exists by adding plus ID to the filename. In short it's something like what Windows does when I copy a file.
Assuming I have a file test.bmp. I want to apply a filter on it and save the filtered image as testout.bmp. But the testout.bmp already exists in that folder, so the program catches this and save it as testout(1).bmp for example. In my code I try to use exeptions. My idea is something like this: (pseudo code)
bool IMAGE_DATA::openFile(const char* filename)
{
int i = 0;
try
{
if(file_exists(filename)) throw whatever;
}
catch(type whatever)
{
changefilename(filename,i)
i++;
if(file_exists(filename)) /* throw whatever/do something */;
}
};
Currently if the file already exists, my program only exists (file_exists just returns true when there's a file with that name in the folder).
I started to redesign my base function with exception handling instead of simply returning false if any error occurred. I wrote whatever here because I will have more throws (if if the file cannot be opened or exists but the same as my file ect.).
How can I try and catch file names until there's a file name that's correct. Or is there any easier method for this and I should not use exceptions? What is the best design to handle this problem?