代码拉取完成,页面将自动刷新
//
// Created by lester on 2023/12/21.
//
#ifndef TEST_FILE_H
#define TEST_FILE_H
#include <cstring>
#include <functional>
#include <map>
#include <fcntl.h>
#include <unistd.h>
class AppendFile{
public:
explicit AppendFile(const std::string& filePath) : file_path_(filePath){
fd_ = open(filePath.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0644);
if(fd_ < 0){
perror("open file failed\n");
open_succ_ = false;
}
}
bool OpenSuccess() const{
return open_succ_;
}
void Append(const char* data,size_t size){
if(OpenSuccess()){
size_t has_write {0};
size_t remain {0};
while(has_write != size){
remain = size - has_write;
size_t write_len = write(fd_,data + has_write,remain);
has_write += write_len;
write_bytes += write_len;
}
}
}
uint64_t GetWriteBytes() const{
return write_bytes;
}
~AppendFile(){
if(fd_){
::close(fd_);
}
}
private:
std::string file_path_;
int fd_{};
bool open_succ_{true};
uint64_t write_bytes{0};
};
#endif //TEST_FILE_H
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。