How to rename a file?

The part of code where I rename the file just won't work. I tried writing it separately in another project, it works. And yes, I've tried closing "myfile" before renaming it Help me please.

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
  #include <iostream>
#include <stdio.h>
#include <fstream>

using namespace std;

int main () {
	char address[] = "";
	char newname[] = "";
	int action;
	char confirm;
	int result;
	
	cout << "File Manipulator 1.0" << endl;
	cout << "--------------------" << endl << endl;
	cout << "Type the full address of a file you wish to manipulate." << endl << endl;
	ADDRESS:cin >> address;
	fstream myfile;
	myfile.open(address);
	if (!myfile.good()) {
		cout << "The selected file does not exist! Would you like to create it? ";
		goto ADDRESS;
	} else {
		cout << endl << "-----------------------------------" << endl;
		cout << "Type 1 to move the selected file." << endl;
		cout << "Type 2 to rename the selected file." << endl;
		cout << "Type 3 to delete the selected file." << endl;
		cout << "-----------------------------------" << endl << endl;
		ACTION:cin >> action;
		
		if (action == 1) {
			cout << 1;
		} else if (action == 2) {
			cout << "Enter the new name: ";
			cin >> newname;
			cout << "Are you sure you want to rename the selected file? Y/N ";
			CONFIRM:cin >> confirm;
			if (confirm == 'Y' || 'y') {
				myfile.close();
				rename(address, newname);
				
			} else if (confirm == 'N' || 'n') {
				cout << "No";
			} else {
				cout << "You typed an invalid command! Try again. ";
				goto CONFIRM;
			}
		} else if (action == 3) {
			cout << 3;
		} else {
			cout << "You typed an invalid command! Try again." << endl;
			goto ACTION;
		}
	}
	return 0;
}


BTW the whole code is not finished, so check just the renaming part. Thanks.
You didn't specify a buffer size for your array/strings, add a size your code will work.

However there are other things that should be mentioned:

1. You can't use anything with a space in the file name in your code. (eg. c:\\documents and settings\\blahblahblah.txt)

2. You may want to consider using std::string instead of char arrays, but your code=your call

3. You'll start a flame war using goto :)
Can you please be more specific about how to fix the problem.

1. Not using the space, I'm aware of that, it's a char ofc.
2. Tried with strings also, same issue.
3. I have it under control :P
the code is not complete but it works in renaming the file.

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
97
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>

using namespace std;

int main() 
{
	std::string address;
	std::string newname;
	char input;
	int action;
	char confirm;
	int result;

	cout << "File Manipulator 1.0" << endl;
	cout << "--------------------" << endl << endl;
	cout << "Type the full address of a file you wish to manipulate." << endl << endl;
	
	std::getline(std::cin, address);
		
	ifstream myfile(address);

	// try to open the file
	if (myfile.is_open())
	{		
		cout << "File Good" << std::endl;
		myfile.close();
	}
	else
	{
		cout << "The selected file does not exist! Would you like to create it? ";
		std::cin >> input;	

		if (input == 'y' || input == 'Y')
		{
			// create the file.
			ofstream output(address.c_str());
			output.close();

			cout << endl << "-----------------------------------" << endl;
			cout << "Type 1 to move the selected file." << endl;
			cout << "Type 2 to rename the selected file." << endl;
			cout << "Type 3 to delete the selected file." << endl;
			cout << "-----------------------------------" << endl << endl;

			std::cin >> action;

			switch (action)
			{
				case 1:
				{	
					// do nothing.
				}
				break;

				case 2:
				{
					// rename file.
					std::cout << "Enter the new name" << endl << endl;	
					cin.ignore();
					std::getline(std::cin, newname);
					std::cout << "Are you sure you want ot rename the selected file ? Y/N" << endl << endl;
					std::cin >> confirm;

					if (confirm == 'Y' || confirm == 'y')
					{
						rename(address.c_str(), newname.c_str());
					}
					
					
				}
				break;
				
				case 3:
				{
					// delete file.
					std::remove(address.c_str());
				}
				break;

				default:
				{
					std::cout << "You typed an invalid command!" << std::endl;
				}
				break;
			}
		}
		


	}
	
	system("pause");
	return 0;
}
Last edited on
To be more specific, change
1
2
     char address[] = "";
     char newname[] = "";

To both have an array size instead of a blank size.
Last edited on
@rafae11 the code u provided works perfect on an online c++ shell, but still doesn't work for me. Line 23 gives me an error:
[Error] no matching function for call to 'std::basic_ifstream<char>::basic_ifstream(std::string&)'
http://stackoverflow.com/questions/6323619/c-ifstream-error-using-string-as-opening-file-path
 
ifstream myfile(address);

to
 
ifstream myfile(address.c_str());
Topic archived. No new replies allowed.