gpt4 book ai didi

c++ - 通过给出一个词从c++文件中删除一行

转载 作者:太空宇宙 更新时间:2023-11-04 12:29:49 24 4
gpt4 key购买 nike

我制作了一个 Quizlet 代码,用于将单词及其翻译(俄语)保存在 csv 文件中。因此,“添加”和“读取”功能完美运行,但我一直在尝试让“删除”功能在我给出该行的子字符串时删除该行。

更新:我正在尝试将除我想删除的行之外的所有行复制到一个新文件中,然后重命名它。但是当创建新文件时,它是空的!

例如:在文件中,第 1 行:apple яблоко。

输入:apple,然后整个被删除。

这是我的代码:我只是在 void quizlet::DeleteWord() 中遇到问题

#include <string>
#include <iostream>
#include <vector>
#include<fstream>
#include <sstream>
#include<cstdlib>
using namespace std;

class quizlet {
private:
std::string filename;
std::vector<std::string> lines;

public:
quizlet(std::string filename) : filename(filename) {}

void AddWord(std::string, std::string);
vector<string> ReadAllWords();
void DeleteWord();
};

void quizlet::AddWord(std::string word, std::string translation) {
cout << "Write a word and its translation separated by a space:" << std::endl;
cin >> word >> translation;


// file pointer
fstream fout;

// opens an existing csv file or creates a new file.
fout.open("words.txt",ios::out | ios::app);
// Insert the data to file
fout <<word<<" "<<translation<<endl;

std::cout << "Saved new card: " << word << "/" << translation << std::endl;
}

vector<string> quizlet::ReadAllWords() {

// File pointer
fstream fin;

// Open an existing file
fin.open("words.txt", ios::in);




// Read the Data from the file
// as String Vector
vector <string> rows;
string line, word, temp;


while (getline(fin, line)) {

cout << line << std::endl;
rows.push_back(line);
stringstream s(line);

}

return rows;







}


void quizlet::DeleteWord() {

string line;
fstream fin;
fstream fout;
fin.open("words.txt", ios::in);
fout.open("new.txt",ios::out | ios::app);
string token;
cin>>token;

vector <string> lines;
while (getline(fin, line)) {
if (line.find(token) != string::npos) {
cout << line << endl;
fin << line << endl;
cout<<"the line has been deleted!";
//remove (line);
}
}

fin.close();
fout.close();
remove("words.txt");
rename("new.txt", "words.txt");
cout << "\nChanges has Successfully been made...... Data Saved\n" << endl;

}




int main() {
auto Quizlet = quizlet("words.txt");
string word, translation;
while (true) {
std::string command;
std::cin >> command;



if (command == "add") {

Quizlet.AddWord(word, translation);

} else if (command == "read") {
Quizlet.ReadAllWords();

}
else if (command == "delete") {
Quizlet.DeleteWord();

}



else {
return 0;
}
}
}

最佳答案

更新后编辑:

鉴于您正在尝试做的事情,我原来的答案现在更有意义了。您应该立即读取整个文件,在内存中进行任何您想要的添加和删除,然后用整个新列表覆盖原始文件。

原答案:

考虑通过 std::map<std::string,std::wstring> 将文件读入内存而不是 std::vector<std::string>文件中的行数。

使用这种方法,添加和删除单词及其翻译很简单。

添加:

//if-guard only needed if you want to protect against overwriting already-existing words.
auto found_iter = cards.find(word);
if(found_iter == cards.end()) {
cards.insert_or_assign(word, translation);
}

删除:

auto found_iter = cards.find(word);
if(found_iter != cards.end()) {
cards.erase(found_iter);
}

将其写回文件就像遍历集合一样简单:

for(const auto& [word,translation] : cards) {
fout << word << ' ' << translation << '\n';
}
fout.close();

关于c++ - 通过给出一个词从c++文件中删除一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59121333/

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