如何在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数组。

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());

我将ifstream与c_str()将a的内容传递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;
}

感谢您仍然尝试:)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章