如何在C++中重命名文件
How to rename a file in C++
我重命名文件的代码部分无法工作。我试着在另一个项目中单独写,它很有效。请帮帮我。
#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 file(address);
if (!file.good()) {
cout << "The selected file does not exist! Try again. ";
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') {
result = rename(address, newname);
if (result == 0) {
cout << "renamed";
} else {
perror("not renamed");
}
} 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;
}
顺便说一句,整个代码还没有完成,所以只检查重命名部分。谢谢
好吧,这就是解决方案。
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
using namespace std;
int main() {
string address;
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;
getline(cin, address);
ifstream myfile(address.c_str());
我使用了带有c_str()
函数的ifstream,该函数将std::string
的内容传递到C风格的字符串中。
// try to open the file
if (myfile.is_open())
{
满足条件后,必须关闭打开的文件,以便以后能够操作/使用它。
myfile.close();
CREATE: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;
cin >> action;
switch (action)
{
case 1:
{
// do nothing.
}
break;
case 2:
{
// rename file.
cout << "Enter the new name" << endl << endl;
cin.ignore();
我在这里使用了ignore()函数来忽略我调用它时指定的字符数
getline(cin, newname);
cout << "Are you sure you want ot rename the selected file ? Y/N" << endl << endl;
cin >> confirm;
if (confirm == 'Y' || confirm == 'y')
{
与我前面解释的c_str()的情况相同。
rename(address.c_str(), newname.c_str());
}
}
break;
case 3:
{
// delete file.
remove(address.c_str());
}
break;
default:
{
cout << "You typed an invalid command!" << endl;
}
break;
}
}
else
{
cout << "The selected file does not exist! Would you like to create it? ";
cin >> input;
如果您输入的文件名不存在,系统会提示您使用指定的名称创建一个文件,然后使用goto重定向到操作菜单。
if (input == 'y' || input == 'Y')
{
// create the file.
ofstream output(address.c_str());
output.close();
cout << "File created";
goto CREATE;
}
}
return 0;
}
感谢您的尝试:)
相关文章:
- 如何在Visual Studio Code中重命名我的a.exe文件?
- 将内容从第一个文件("constituencies")移动到第二个文件("temp")并在之后重命名时,我的文件被删除
- 以编程方式防止重命名或删除文件,但仍使其可写
- 使用 stdio.h 重新定位和重命名文件C++
- 如何检测哪些进程更改,重命名或创建文件?
- 录制后无法重命名 rosbag 文件
- 将文件重命名为已存在的文件名
- 移动了 QT 项目,重命名文件夹和项目名称,Moc'ing 返回"'-I'之后缺少值"
- 将 .c 文件重命名为 .cpp,导入 Cython 库失败
- 为什么将可执行文件重命名为临时文件的此代码段不能按预期工作?
- 如何在 C++ 中将文件从 ext4 文件系统重命名为 ntfs 文件系统
- 用C 的名称重命名文件
- 如何在C++中重命名文件
- directory_iterator file_iter重命名文件夹中的文件
- 如何使用rename()函数(c++)用字符串重命名文件
- 重命名文件夹内的文件- Unicode等效函数
- 如何使用wstring重命名文件
- 在Windows 8上用C++重命名文件
- 如何在Windows上按句柄重命名文件
- 使用readdirectorychangesc++重命名文件夹后获取旧名称和新名称