gpt4 book ai didi

c++ - 在 C++ 中使用 ifstream/ofstream 删除文本文件中的特定行

转载 作者:行者123 更新时间:2023-11-30 01:53:19 26 4
gpt4 key购买 nike

这是我遇到问题的代码,我有一个 .txt 文件,其中包含使用以下格式的用户及其密码列表:user;password。我需要在文件中搜索用户,然后删除包含该用户的行。

    void deleteauser()
{
string user;
cout<<"Which user do you wish to delete?";
cin>>user;
string line;
string delimiter=";";
string token,token1;
ifstream infile;
infile.open("users.txt",ios::in);
while (getline(infile,line,'\n'))
{
token = line.substr(0, line.find(delimiter));
token1=line.substr(token.length(), line.find('\n'));
if(token==user)
{
//here i need to delete the line of the user that has been found
}
}
infile.close();
}

最佳答案

逐行读取输入文件,写入临时文件。当您找到不需要的行时,不要将它们写入临时文件。完成后将临时文件重命名为真实文件。

关于c++ - 在 C++ 中使用 ifstream/ofstream 删除文本文件中的特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23237774/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com