1 Star 0 Fork 0

lugang/cpp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
File.h 1.19 KB
一键复制 编辑 原始数据 按行查看 历史
lugang51 提交于 2023-12-22 11:17 +08:00 . Add WriteBytes function in AppendFile
//
// 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
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lugang0218/cpp.git
git@gitee.com:lugang0218/cpp.git
lugang0218
cpp
cpp
master

搜索帮助