这个file.h库哪里可以性能优化,一定采纳(如果好使)
#pragma once
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <filesystem>
class File
{
public:
explicit File(const std::string& filename);
File& operator<<(const std::string& content);
File& operator<<(const File& content);
void operator=(const std::string& content);
std::string text() const;
void exists() const;
operator std::string() const {
return text();
}
private:
std::string filename;
};
File::File(const std::string& filename) : filename(filename) {}
File& File::operator<<(const std::string& content) {
std::ofstream file(filename, std::ios::app);
if (file) file << content;
return *this;
}
File& File::operator<<(const File& file) {
std::ofstream file2(filename, std::ios::app);
if (file2) file2 << file.text();
return *this;
}
void File::operator=(const std::string& content) {
std::ofstream file(filename);
if (file) file << content;
}
std::string File::text() const {
std::ifstream file(filename);
if (file) {
std::ostringstream ss;
ss << file.rdbuf();
return ss.str();
}
}
void File::exists() const {
std::ofstream file(filename, std::ios::app);
if (file) file << "";
}