So i'm trying to write a program that reads all the files of a directory and renames their filenames but i get a long error: "This application has requested the Runtime to terminate it in an unusial way. Please contact the application's support team for more information." and the exe stops working.
here is the code:
thnx i knew something was off...i think it has something to do with the hFind handling, that 3 lines i configure hFind, it has to be it, debug doesnt give me anything...does profile analysis give me any useful info?
thnx for the responses guys...i'll change my code a bit and post back.
MikeyBoy: i didn't know about the -Wall -Wextra -std=c++98 -pedantic -g parameters, and now that i know of them debugger still doesnt give me enough clues to pipoint the error..i guess i have to learn how to backtrace.
ne555: done :)
so the problem was in the replace function, every time it did find the specified char(s) in the string everything went fine, but when it didnt it returned a char position in the string that was way bigger than the lenght of the string.
so i'm gonna try a for loop for every replace call
#include <cstdlib>
#include <iostream>
#include <string>
#include <windows.h>
#include <dirent.h>
#include <WinBase.h>
usingnamespace std;
void Namecorrection(string &temps){
do{
if(temps.find(" - ")<=temps.length())
temps.replace(temps.find(" - "),4, " - ");
} while(temps.find(" - ")<=temps.length());
do{
if(temps.find(" .")<=temps.length())
temps.replace(temps.find(" ."), 2, ".");
} while(temps.find(" .")<=temps.length());
//a long list of parametres written in the same pattern
};
int main(int argc, char *argv[])
{
string path, path1, errorlist;
cout<<"Give me directory: "<<endl;
cin>>path;
path1=path;
int filenum=0;
DIR *dir = opendir(path.c_str());
if(dir)
{
struct dirent *ent;
}
else
{
cout << "Error opening directory" << endl;
}
HANDLE hFind;
WIN32_FIND_DATA FindData;
path1+="*";//cant use + operator in FindFirstFileA so i created a new string plus the "*"
hFind = FindFirstFileA(path1.c_str(), &FindData);
if( hFind == INVALID_HANDLE_VALUE ) returnfalse;
string filepath = path + FindData.cFileName;
int result;
string temps=FindData.cFileName, finalpath;
Namecorrection(temps);
finalpath=path+temps; //the file path with its name changed
if (filepath!=finalpath){
result= rename(filepath.c_str(), finalpath.c_str());
if ( result == 0 ){
filenum++;//enumeration to se how many file names' got changed
cout<<"File #"<<filenum<< " successfully renamed to:\n"<<temps<<"\n\n";
}
else
perror( "Error renaming file" );
errorlist+=FindData.cFileName;
errorlist+="\n\n";
}
while (FindNextFileA(hFind, &FindData) != 0){
filepath = path + FindData.cFileName;
temps=FindData.cFileName;
Namecorrection(temps);
finalpath=path+temps;
if (filepath!=finalpath){
result= rename(filepath.c_str(), finalpath.c_str());
if ( result == 0 ){
filenum++;
cout<<"File #"<<filenum<< " successfully renamed to:\n"<<temps<<"\n\n";
}
else
perror( "Error renaming file" ); //in case of an error renaming the file, i put the file name
errorlist+=FindData.cFileName;//in a string to manually edit (can't edit special characters)
errorlist+="\n\n";
}
}
FindClose(hFind);
cout<<"A total of "<<filenum<<" files have been renamed\n";
if (errorlist!="")
cout<<"The File(s) that gave error message(s):"<<"\n\n"<<errorlist;
system("PAUSE");
return EXIT_SUCCESS;
}