rename_spaces.cc

rename_spaces.cc



#include<iostream>
#include<fstream>
#include<string.h>

using namespace std;


int main(int argc, char * argv[])
{
        int a,l,f;
        char initial[140];
        char final[140];
        l = 0;
        f = 0;


if(argc!=2)

{

cout << "Usage: kprename filename" << endl ;

return 0;

}



//// CHECK IF THE FILE EXISTS
//    ifstream infile(argv[1]);
    fstream infile;
    infile.open(argv[1]);
    if (infile.is_open()){
        infile.close();
    }
    else {
cout << "File not found" << endl;
    return 0;
    }


   initial[0] = '\0';
    final[0] = '\0';
    strcat(initial, argv[1]);
      l = strlen(initial);
   initial[l+1] = '\0';


    for(a=0;a<=l;a++) {
            if ((initial[a] != '|')&& (initial[a] != ' ')&&(initial[a] != '[' )&&(initial[a] != ']')&&(initial[a]!= '(')&&(initial[a]!=')') && (initial[a] != '\\') && (initial[a] != '/')&& (initial[a] != '%') && (initial[a] != '!') && (initial[a] != '{')&& (initial[a] != '}')){
            final[f] = initial[a];
            f++;
            }
}  // for a loop


    l = strlen(final);
    final[l+1] = '\0';

    fstream outfile;
    outfile.open(final);
    if (outfile.is_open()){
        outfile.close();
        cout << "File " << final << " exists" << endl;
        return 1;
    }

    l = rename(initial,final);
    if (l == 0){
    //cout << "rename successful" << endl;
    }   
    else     {
    cout << "rename failed" << endl;
    }   
        

} // main