如何在特定时间向文件追加内容,而在其他时间覆盖它?

huangapple go评论77阅读模式
英文:

How to append to a file at certain times, and overwrite it at others?

问题

在我的程序中,我有一个存储名称列表的文件。我也有一种删除这些名称的方法 - 将文件内容写入一个向量,然后从向量中删除名称,然后将向量的内容重新写入文件。当用户想要添加一个名称时,文件会将其追加。我的问题是,当我尝试删除一个条目并用剩下的内容替换文件时,它会将其附加到文件末尾。

例如,如果一个名称是Admin,另一个名称是User,如果用户尝试删除Admin,那么结果如下:


-- Admin --

-- User --

-- User --

它保留了Admin和User,并将删除User后的结果追加到文件中。

这是我的代码:

void create_password()
{
    string holder;
    string keeper;
    vector<string> delete_entry;
    vector<string>::iterator new_end;
    
    ofstream New_PasswordFile(new_password_name + ".txt"); // 创建一个名为*name*.txt的文件(用于存储给定名称的密码)
    ofstream Display_File; // 将名称保存到另一个文件,当用户尝试检索密码时,该文件会显示给用户
    
    // Display_File 是包含所有名称的文件
    Display_File.open(stored_username + "names.txt", ios_base::app); // 追加到文件而不是重新创建
    
    ifstream Display_File_Checker(stored_username + "names.txt"); // 以不同的名称打开相同的文件
    
    if (is_same_entry != true) // 如果输入了新条目
    {
        Display_File << "\n-- " << new_password_name << " --\n"; // 将新名称添加到文件中,格式为 -- *name* --
    }
    
    cout << "WHAT IS THE PASSWORD?"; // 提示输入要保存到*name*的密码
    cin >> new_password;
    
    if (new_password == "DELETE") // 如果为给定条目输入了删除密码
    {
        new_password = "";
        
        while (getline(Display_File_Checker, holder))
        {
            delete_entry.push_back(holder); // 将显示内容添加到向量delete_entry
        }

        for (int i = 0; i < delete_entry.size(); i++)
        {
            if (delete_entry[i] == "-- " + new_password_name + " --")
            {
                cout << "working" << endl;
                new_end = remove(delete_entry.begin(), delete_entry.end(), "-- " + new_password_name + " --");
                delete_entry.erase(new_end, delete_entry.end()); // 删除该条目
            }
        }
        
        // 将new_end的内容写入条目文件
        for (int i = 0; i < delete_entry.size(); i++)
        {
            keeper = delete_entry[i];
            cout << "keeper: " << keeper << endl;
            Display_File << keeper << endl; // 追加而不是覆盖
        }
    
        Display_File.close();
    }
}

我尝试不追加到文件,但用户无法添加条目。我不确定是否有一种编码方法或者是否有一种合乎逻辑的方法来解决这个问题。

英文:

In my program, I have a file which stores a list of names. I also have a way of deleting those names - writing the file contents to an vector, then erasing the name from the vector, then writing the contents of the vector back to the file. When a user wants to add a name, then the file appends it. My issue is that when I try to take out an entry, and replace the file with whatever is left, it appends that to the file.
So, for example, if one name is Admin, and another name is User, if a user tries to remove Admin, then the result looks like this:


-- Admin --

-- User --

-- User --

It retains Admin and User, and appends the result of erasing user.

This is my code:

	void create_password()
	{
		string holder;
		string keeper;
		vector&lt;string&gt; delete_entry;
		vector&lt;string&gt;::iterator new_end;
		
		ofstream New_PasswordFile(new_password_name + &quot;.txt&quot;);//Creates a file called *name*.txt(used to store password for given name)
	ofstream Display_File;  		//Saves name to another file, which is diplayed to the user, when they attempt to retrieve a passwords

	//Display file is the file with all of the names
	
	Display_File.open(stored_username + &quot;names.txt&quot;, ios_base::app);
	//Appends to the file instead of recreating it.
	ifstream Display_File_Checker(stored_username + &quot;names.txt&quot;);
	//Opens same file under different name
	if(is_same_entry != true){//if a new entry is inputed
		Display_File &lt;&lt; &quot;\n-- &quot; &lt;&lt; new_password_name &lt;&lt; &quot; --\n&quot;; //Inputs the new name into the file with -- *name* --
	}
	
	cout&lt;&lt;&quot;WHAT IS THE PASSWORD?&quot;; //Prompts to input password to save to the *name*
	cin &gt;&gt; new_password;
	
	if (new_password == &quot;DELETE&quot;){//if inputed password for given entry is delete
		new_password = &quot;&quot;;

		while (getline (Display_File_Checker, holder))
		delete_entry.push_back(holder);//Adds display to vector delete_entry

for (int i = 0; i &lt; delete_entry.size(); i++) { 
	if(delete_entry[i] == &quot;-- &quot; + new_password_name + &quot; --&quot;){
		cout &lt;&lt; &quot;working&quot; &lt;&lt; endl;
new_end  = remove(delete_entry.begin(), delete_entry.end(), &quot;-- &quot; + new_password_name + &quot; --&quot;);

delete_entry.erase(new_end,delete_entry.end()); //erase the entry
	}
	}
	
	//write contents of new_end to entry file
	for (int i = 0; i &lt; delete_entry.size(); i++){
		keeper = delete_entry[i];
cout &lt;&lt;  &quot;keeper :&quot; &lt;&lt; keeper &lt;&lt; endl; //&lt;&lt; endl;
		Display_File &lt;&lt; keeper &lt;&lt; endl; //appends instead of overwriting.
	}

	Display_File.close(); 
	}

I tried to not append to the file, but then users are unable to add entries. I'm not sure if there is a way to code this, or if there is a logical way to go about doing this.

答案1

得分: 1

是的,您混合了几个应该分开的操作。

如果您想要就地更新文件 - 这正是数据库的用途。如果您正在考虑这一点,sqlite3 将是一个非常好的解决方案。

然而,这里的典型解决方案是:

  1. 将整个文件加载到内存中,可能加载到(哈希)映射中。

  2. 执行您需要的操作 - 添加/删除/修改。

  3. 将整个映射重新写入文件。

英文:

Yes you are mixing several operations that should be apart.

If you want to update a file in place - that's what database are meant to do. IF you are considering that, sqlite3 would be a very good solution

However the typical solution here would be:

  1. Load the entire file in memory, perhaps into a (hash) map

  2. Make the operations you need - add/delete/modify

  3. Write the entire map back to the file.

huangapple
  • 本文由 发表于 2023年2月27日 05:00:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定