renaming files doesn't work

Here is the source code of the program... I'm not sure what else to mention... uhm.. here is the program's output ... every time:
File not found! ... so it can't open the files.
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
#include <iostream>
#include <sstream>
#include <fstream>

using namespace std;

string convertInt(int number)
{
    stringstream ss;//create a stringstream
   ss << number;//add number to the stream
   return ss.str();//return a string with the contents of the stream
}

string getInitialName(int i) {
    string initName = "1(";
    string final = ").txt";
    string temp;
    temp = convertInt(i);
    initName += temp;
    initName += final;

    return initName;
}

string getFinalName(int i){
    string finalName;
    string temp = ".txt";
    finalName=convertInt(i);
    finalName+=temp;

    return finalName;
}

int main () {
    int i,rez;
    string oldName;
    string newName;
    for(i=1; i<=11; i++) {

        oldName=getInitialName(i);
        newName=getFinalName(i);

        //open the current file
        ofstream currentFile;
        currentFile.open(oldName.c_str(), ios::in);

        //check if exists
        if(!currentFile) {
            cout << "File not found" << endl;
            currentFile.close();
        }
        else {
 		// First close file so it's not in use!
 		currentFile.close();

 		// Everything is ok, so rename the file
 		int result;
 		result = rename(oldName.c_str(), newName.c_str() );

 		// Error check
 		if (result != 0 ) {
 			cout << "Error on rename." << endl;
 			return -1;
 		}
        }



        /*rez=rename(oldName.c_str(), newName.c_str());
        if(rez==0) cout <<"A mers" << endl;
        else cout << "Nu a mers" << endl;.
        */
    }
cin.get();
    return 0;
}
It may sound stupid, but... there ARE files named 1(1).txt, 1(2).txt and so on in the directory where you start your program from, right? ;-)

Ciao, Imi.
Last edited on
yes... tested both with and without the files. Same thing
Later edit: Fixed.... imi was right... Tiny mistake there... the filenames where actually 1 (1).txt thanks
Last edited on
Topic archived. No new replies allowed.