简体   繁体   English

如何在C++中重命名文件

[英]How to rename a file in C++

The part of code where I rename the file just won't work.我重命名文件的代码部分将不起作用。 I tried writing it separately in another project, it works.我尝试在另一个项目中单独编写它,它有效。 Help me please.请帮帮我。

#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;
}

BTW the whole code is not finished, so check just the renaming part.顺便说一句,整个代码还没有完成,所以只检查重命名部分。 Thanks.谢谢。

Well, this is the solution.嗯,这就是解决方案。

#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>

using namespace std;

int main() {
string address;
string newname;

Here you can see I used strings instead of char arrays.在这里你可以看到我使用了字符串而不是字符数组。

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

I used ifstream with c_str() function which passes contents of a std::string into a C style string.我将 ifstream 与c_str()函数一起使用,该函数将std::string内容传递到 C 样式字符串中。

// try to open the file
if (myfile.is_open())
{       

When the condition is met, you must close the opened file in order to be able to manipulate/work with it later.当条件满足时,您必须关闭打开的文件,以便以后能够操作/使用它。

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

I used here the ignore() function to ignores the amount of characters I specify when I call it.我在这里使用了 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')
        {

Same case with c_str() that i explained earlier.与我之前解释的 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;   

If the file name you input doesn't exist, you are prompted to create a file with the specified name, then you are redirected with goto to the manipulation menu.如果您输入的文件名不存在,系统会提示您创建一个指定名称的文件,然后使用 goto 将您重定向到操作菜单。

    if (input == 'y' || input == 'Y')
    {
        // create the file.
        ofstream output(address.c_str());
        output.close();
        cout << "File created";
        goto CREATE;
    }
}
return 0;
}

Thanks for trying anyway :)无论如何,感谢您的尝试:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM