You are renaming files to the same folder in which the original files were, resulting in an infinite loop. You renamed 04.png
to 4.png
but since you are iterating over all files in the folder, at some point you're going to iterate to the "new" 4.png
file (in your smaple, on the 40th iteration) and rename that file to 40.png
and so on...
The easiest way to resolve this with minimal changes to the existing code is to "rename" (move) the files to a temporary folder with their new names. Something like:
new_s = temp_path;
out << *start;
new_s += out.str();
new_s.append(file_format);
// Switch name on the file from old string to new string
rename(old_s.c_str(), new_s.c_str());
and when you are done renaming all the files in path
(outside the while
loop), delete the folder and "rename" (move) temp_path
to `path:
closedir (dir);
deletedir(path);
rename(temp_path, path);
`