_=Gigant=_
Известный
- 144
- 221
Functions to add names to the file, could be useful for friends list etc... not just names other text in general:
void WriteNamesToFile(const std::string& filename, const std::set<std::string>& names) {
std::ofstream outfile(filename);
for (const auto& name : names) {
if (!name.empty()) {
outfile << name << std::endl;
}
}
}
void AddNameToFile(const std::string& filename, const std::string& new_name) {
std::set<std::string> names = ReadNamesFromFile(filename);
if (new_name.empty() || names.find(new_name) != names.end())
return;
names.insert(new_name);
WriteNamesToFile(filename, names);
}
//name_to_remove or in general any text
void RemoveNameFromFile(const std::string& filename, const std::string& name_to_remove) {
std::set<std::string> names = ReadNamesFromFile(filename);
if (names.erase(name_to_remove) > 0)
WriteNamesToFile(filename, names);
}
bool NameExistsInFile(const std::string& filename, const std::string& name_to_check) {
std::set<std::string> names = ReadNamesFromFile(filename);
return names.find(name_to_check) != names.end();
}
std::set<std::string> ReadNamesFromFile(const std::string& filename) {
std::set<std::string> names;
std::ifstream infile(filename);
std::string name;
while (std::getline(infile, name)) {
if (!name.empty())
names.insert(name);
}
return names;
}