What am i doing wrong

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <cstdlib>
#include <iostream>
#include <string>
#include <windows.h> 
#include <dirent.h>
#include <WinBase.h>


using namespace std;


string Namecorrection(string &temps){
       replace(temps.begin(),temps.end(), '_', ' ');
       temps.replace(temps.find("(720p)"), 6, "720p");
       temps.replace(temps.find("(1080p)"),7, "1080p");

       }

int main(int argc, char *argv[])
{
    string path="c:/class/";
  DIR *dir = opendir("c:/class/"); 
    if(dir) 
    { 
        struct dirent *ent; 
    } 
    else 
    { 
        cout << "Error opening directory" << endl; 
    } 
    
    HANDLE hFind;
    WIN32_FIND_DATA FindData;
    hFind = FindFirstFile("c:/class/*.mp4", &FindData);
    if( hFind == INVALID_HANDLE_VALUE ) return false;
    string filepath = path + FindData.cFileName;
    
  int result;
  string temps=FindData.cFileName, finalpath;
  Namecorrection(temps);
  finalpath=path+temps;
  if (filepath!=finalpath){
    result= rename(filepath.c_str(), finalpath.c_str());
    if ( result == 0 ){
      puts ( "File successfully renamed to: " );
      cout<<temps<<endl;
      }
    else
    perror( "Error renaming file" );
    }
  

  FindNextFile(hFind, &FindData);
  if( hFind == INVALID_HANDLE_VALUE ) return false;
    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 ){
    puts ( "File successfully renamed to: " );
    cout<<temps<<endl;
    }
  else
    perror( "Error renaming file" );
    }
    FindClose(hFind);
    

    system("PAUSE");
    return EXIT_SUCCESS;
}

Last edited on
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/jEywvCM9/

2) At what point does it terminate?
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?
Last edited on
http://www.cplusplus.com/forum/general/112111/
In function: `string Namecorrection(string &temps)'
warning: no return statement in function returning non-void
It's almost as if compiler messages were actually useful, and worth paying attention to...

EDIT: although nothing in the OP's code ever uses the return value of NameCorrection, so that's unlikely to be the problem.
Last edited on
You've got an invalid string object whose internal buffer points to garbage. So when its destructor is invoked you've got a bad delete.
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.
given that you have no intention to return a valid string object from `Namecorrection()' declared it as
void Namecorrection(string &temps)
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
Well, if you'd show us the replace function, maybe we could help figure out the best way to fix it.
i got 3 calls of replace in the Namecorrection function, i'll add a lot more eventually.
here is what i came up with:
1
2
if(temps.find("_")<=temps.length()){
       temps.replace(temps.find("_"), 1, " ");


EDiT
1
2
3
4
do{
       if(temps.find("_")<=temps.length())
       temps.replace(temps.find("_"), 1, " ");
       } while(temps.find("_")<=temps.length());

in case i have more that 1 instances

EDiT2 in this part of code do i need the if( hFind == INVALID_HANDLE_VALUE ) return false; line?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
while (FindNextFile(hFind, &FindData) != 0){
  if( hFind == INVALID_HANDLE_VALUE ) return false;
    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" );
    }
}
    FindClose(hFind);
Last edited on
thank you guys for the info, i just finished my code, if u have a better way to do this or u can give me some notes i would really appreciate it!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <cstdlib>
#include <iostream>
#include <string>
#include <windows.h> 
#include <dirent.h>
#include <WinBase.h>


using namespace 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 ) return false;
    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;
}
Last edited on
Topic archived. No new replies allowed.