简体   繁体   English

如何使用 remove() 重命名和删除 c++ 中的 txt 文件以更新内容?

[英]How to rename and remove txt file in c++ using remove() to update the contents?

I have these 2 files and I need to update the file by updating into the temp file.我有这 2 个文件,我需要通过更新到临时文件来更新文件。 I did it but how do I rename the temp file to the old file and then delete the old file?我做到了,但是如何将临时文件重命名为旧文件,然后删除旧文件?

ifstream in_file;
ofstream temp_file;

char in_file_name[] = "input practical 12 part d.txt";
char temp_file_name[] = "temp.txt";

I tried to do like this and the file name don't change.我试着这样做,文件名没有改变。 I need some explanation on this and how it works.我需要对此进行一些解释以及它是如何工作的。 This is the site I referred to and I don't understand it.这是我提到的网站,我不明白。 https://www.programiz.com/cpp-programming/library-function/cstdio/rename https://www.programiz.com/cpp-programming/library-function/cstdio/rename

in_file.close();
temp_file.close();

int rename(char *temp_file_name, char *in_file_name);

Also, can I update the file by renaming the temp file to the old file and then rename the old file to temp file so that the user don't need to recreate a temp file to update the contents in the file?另外,我可以通过将临时文件重命名为旧文件然后将旧文件重命名为临时文件来更新文件,这样用户就不需要重新创建临时文件来更新文件中的内容?

Here's the whole code just in case这是整个代码以防万一

  1. Write and test a program that reads product details from a file and stores the information in an array of structures.编写和测试从文件中读取产品详细信息并将信息存储在结构数组中的程序。 The program then prints a menu to allow the user to do any of the following:然后程序会打印一个菜单以允许用户执行以下任何操作:
  2. List all products列出所有产品
  3. Search the price of a product搜索产品的价格
  4. Update the price of a product更新产品价格
  5. Exit出口

If the user chooses to exit, the program will write the updated data in the array of structures to the file.如果用户选择退出,程序会将结构数组中的更新数据写入文件。 The product information includes product number, description and price.产品信息包括产品编号、描述和价格。

Note: You may assume that the total number of product details read from the file will not exceed 30, the product number consist of 6 numbers, and the product description will not exceed 30 characters.注意:您可以假设从文件中读取的产品详细信息总数不超过 30 个,产品编号由 6 个数字组成,产品描述不超过 30 个字符。

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std;

typedef struct
{
    char product_num[5];
    char product_des[29];
    float price;
    
}PRODUCT_TYPE;

int main()
{
    ifstream in_file;
    ofstream temp_file;

    char in_file_name[] = "input practical 12 part d.txt";
    char temp_file_name[] = "temp.txt";
    in_file.open(in_file_name);
    temp_file.open(temp_file_name);

    if (!in_file || !temp_file)
    {
        if (!in_file)
        {
            cout << "Error opening input file" << endl;
        }
        else if (!temp_file)
        {
            cout << "Error opening output file" << endl;
        }
    }
    else
    {
        PRODUCT_TYPE product[50];
        int index = -1;
        int choice;
        string line;
        in_file >> product[++index].product_num;
        cout << "Succesful run" << endl;
        cout << "Menu (Type number for the function)" << endl;
        cout << "1. List all products\n2.Search the price of a product\n3.Update the price of a product\n4.Exit\n";
        cin >> choice;
        if (choice == 1)
        {
            cout << setw(18) << left << "Product Index" << setw(35) << left << "Product Description" << "Price" << endl;
        }
        
        else if (choice == 4)
        {
            cout << setw(18) << left << "Product Index" << setw(35) << left << "Product Description" << "Price" << endl;
        }

        while (in_file)
        {
            in_file >> product[index].product_des >> product[index].price;
            if (choice == 1)
            {
                cout << setw(18) << left << product[index].product_num << setw(35) << left << product[index].product_des << product[index].price << endl;

            }
            else if (choice == 2)
            {
                cout << "Type the product number to get the price" << endl;
                cout << "Product Number: ";
                cin >> product[index].product_num;
                cout << "Price: ";
                cout << product[index].price << endl;
            }
            
            else if (choice == 3)
            {
                float new_price;
                cout << "Enter the product number to change its price: ";
                cin >> product[index].product_num;
                cout << "Enter the updated price list: ";
                cin >> new_price;
                if (new_price != product[index].price)
                {
                    temp_file << " " << product[index].product_num << " "<< product[index].product_des << " "<< new_price << endl;
                }

            }

            else if (choice == 4)
            {
                cout << setw(18) << left << product[index].product_num << setw(35) << left << product[index].product_des << product[index].price << endl;
            }
            in_file >> product[++index].product_num;
        }
        
        in_file.close();
        temp_file.close();

        int rename(char *temp_file_name, char *in_file_name);
    }
    return 0;

}

Thanks.谢谢。

What this is telling you:这告诉你什么:

int rename(char *temp_file_name, char *in_file_name);

is that there's a method called rename that takes two arguments.是有一个名为 rename 的方法需要两个 arguments。 You've defined your two file names like this:您已经像这样定义了两个文件名:

char in_file_name[] = "input practical 12 part d.txt";
char temp_file_name[] = "temp.txt";

So I believe you've written temp.txt, and now you want to move it into place over the other one with the really obnoxious name.所以我相信你已经编写了 temp.txt,现在你想把它移到另一个名字非常讨厌的地方。

rename(temp_file_name, in_file_name);

Beware: this destroys the original input file, so if you want to save it:注意:这会破坏原始输入文件,所以如果你想保存它:

rename(in_file_name, "copy_of_input.txt");

Does that make sense?那有意义吗?

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

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