I have 50 image files which I want to rename.
from: img (1) - Copy.jpg
to: picture_1.jpg
Do you know more elegant way to write it? I came up with this:
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
string oldname = "";
string newname = "";
char oldfilename[20];
char newfilename[30];
for (int i = 1; i <= 50; i++)
{
oldname = "img ("+ to_string(i) +") - copy.jpg"; // old file name
newname = "picture_" + to_string(i) + ".jpg"; // new file name
strcpy(oldfilename, oldname.c_str());
strcpy(newfilename, newname.c_str());
if (rename(oldfilename, newfilename) != 0)
perror("Error renaming file");
else
{
cout << oldfilename << " renamed successfully to " << newfilename << endl;
}
}
char c;
cin >> c;
return 0;
}