ofstream 是從輸出文件流,ifstream 是從輸入文件流 ofstream 是從內存到硬盤,ifstream 是從硬盤到內存
不多囉嗦,明白了這兩點之後,直接簡單的代碼帶入理解。
ifstream類的使用:將指定路徑文件輸入到指定字符串中。
//將指定路徑文件輸入到指定字符串中。
bool Read(const std::string& file_path,std::string* html){
std::ifstream file(file_path.c_str());
if(!file.is_open())
{
return false;
}
std::string line;
while(std::getline(file,line)){
*html+=line+"\n";
}
file.close();
return true;
}
ofstream類的使用:將字符串內容輸出到指定文件
//將字符串內容輸出到指定文件
bool Write(const std::string& file_path,std::string& content){
std::ofstream file(file_path.c_str());
if(!file.is_open())
{
return false;
}
file.write(content.c_str(),content.size());
file.close();
return true;