日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

文件操作的封装

發布時間:2025/4/5 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 文件操作的封装 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1 文件操作的封裝
      • 1.1 代碼實現

1 文件操作的封裝

為了更加方便對文件進行操作,我們這里對系統提供的API進行封裝。主要實現在file_op.h和file_op.cpp中。

1.1 代碼實現

file_op.h:

#ifndef QINIU_LARGE_FILE_OP_H_ #define QINIU_LARGE_FILE_OP_H_#include "common.h"namespace qiniu {namespace largefile{class FileOperation{public:FileOperation(const std::string &file_name, const int open_flags = O_RDWR | O_LARGEFILE);~FileOperation();int open_file();void close_file();int flush_file(); //把文件立即寫入到磁盤int unlink_file(); virtual int pread_file(char *buf, const int32_t nbytes, const int64_t offset);virtual int pwrite_file(const char *buf, const int32_t nbytes, const int64_t offset); int write_file(const char *buf, const int32_t nbytes);//seekint64_t get_file_size();int ftruncate_file(const int64_t length);int seek_file(const int64_t offset);int get_fd() const {return fd_;}protected:int fd_;int open_flags_;char *file_name_;protected:int check_file();protected:static const mode_t OPEN_MODE = 0644;static const int MAX_DISK_TIMES = 5;};} }#endif //QINIU_LARGE_FILE_OP_H_

file_op.cpp:

#include "file_op.h" #include "common.h"namespace qiniu {namespace largefile{FileOperation::FileOperation(const std::string &file_name, const int open_flags):fd_(-1), open_flags_(open_flags){file_name_ = strdup(file_name.c_str());}FileOperation::~FileOperation(){if(fd_ > 0){::close(fd_);}if(NULL != file_name_){free(file_name_);file_name_ = NULL;}}int FileOperation::open_file(){if(fd_ > 0){close(fd_);fd_=-1;}fd_ = ::open(file_name_, open_flags_, OPEN_MODE);if(fd_ < 0){return -errno;}return fd_;}void FileOperation::close_file(){if(fd_ < 0){return;}::close(fd_);fd_ = -1;}int FileOperation::pread_file(char *buf, const int32_t nbytes, const int64_t offset){int32_t left = nbytes;int64_t read_offset = offset;int32_t read_len = 0;char *p_tmp = buf;int i = 0;while( left > 0){++i;if(i >= MAX_DISK_TIMES){break;}if(check_file() < 0){return -errno;}read_len = ::pread64(fd_, p_tmp, left, read_offset);if(read_len < 0){read_len = -errno;if(-read_len == EINTR || EAGAIN == -read_len){continue;}else if(EBADF == -read_len){fd_ = -1;continue;}else {return read_len;}}else if( 0 == read_len){break;}left -= read_len;p_tmp += read_len;read_offset += read_len;}if(0 != left){return EXIT_DISK_OPER_INCOMPLETE;}return TFS_SUCCESS;}int FileOperation::pwrite_file(const char *buf, const int32_t nbytes, const int64_t offset){int32_t left = nbytes;int64_t write_offset = offset;int32_t written_len = 0;const char *p_tmp = buf;int i = 0;while( left > 0){++i;if(i >= MAX_DISK_TIMES){break;}if(check_file() < 0){return -errno;}written_len = ::pwrite64(fd_, p_tmp, left, write_offset);if(written_len < 0){written_len = -errno;if(-written_len == EINTR || EAGAIN == -written_len){continue;}else if(EBADF == -written_len){fd_ = -1;continue;}else {return written_len;}}left -= written_len;p_tmp += written_len;write_offset += written_len;}if(0 != left){return EXIT_DISK_OPER_INCOMPLETE;}return TFS_SUCCESS;}int FileOperation::write_file(const char *buf, const int32_t nbytes){int32_t left = nbytes;int32_t written_len = 0;const char *p_tmp = buf;int i = 0;while( left > 0){++i;if(i >= MAX_DISK_TIMES){break;}if(check_file() < 0){return -errno;}written_len = ::write(fd_, p_tmp, left);if(written_len < 0){written_len = -errno;if(-written_len == EINTR || EAGAIN == -written_len){continue;}else if(EBADF == -written_len){fd_ = -1;continue;}else {return written_len;}}left -= written_len;p_tmp += written_len;}if(0 != left){return EXIT_DISK_OPER_INCOMPLETE;}return TFS_SUCCESS;}int64_t FileOperation::get_file_size(){int fd = check_file();if(fd < 0){return -1;}struct stat statbuf;if( fstat(fd, &statbuf) !=0){return -1;}return statbuf.st_size;}int FileOperation::check_file(){if(fd_ < 0){fd_ = open_file();}return fd_;}int FileOperation::ftruncate_file(const int64_t length){int fd = check_file();if(fd < 0){return fd;}return ftruncate(fd, length);}int FileOperation::seek_file(const int64_t offset){int fd = check_file();if(fd < 0){return fd;}return lseek(fd, offset, SEEK_SET);}int FileOperation::flush_file(){if(open_flags_ & O_SYNC){return 0;}int fd=check_file();if(fd < 0){return fd;}return fsync(fd);}int FileOperation::unlink_file(){close_file();return ::unlink(file_name_);}} }

file_op_test.cpp:

#include "file_op.h" #include "common.h"using namespace std; using namespace qiniu;int main(void){const char * filename = "file_op.txt";largefile::FileOperation *fileOP = new largefile::FileOperation(filename, O_CREAT | O_RDWR | O_LARGEFILE);int fd = fileOP->open_file();if(fd < 0){fprintf(stderr, "open file %s failed. reason: %s\n", filename, strerror(-fd));exit(-1);}char buffer[65];memset(buffer, '8', 64);int ret = fileOP->pwrite_file(buffer, 64, 128);if(ret < 0){if(ret == largefile::EXIT_DISK_OPER_INCOMPLETE){fprintf(stderr, " pwrite_file: read length is less than required!");}else {fprintf(stderr, "pwrite file %s failed. reason: %s\n", filename, strerror(-ret));}}memset(buffer, 0, 64);ret = fileOP->pread_file(buffer, 64, 128);if(ret < 0){if(ret == largefile::EXIT_DISK_OPER_INCOMPLETE){fprintf(stderr, " pread_file: read length is less than required!");}else {fprintf(stderr, "pread file %s failed. reason: %s\n", filename, strerror(-ret));}}else {buffer[64] = '\0';printf("read: %s\n", buffer);}memset(buffer, '9', 64);ret = fileOP->write_file(buffer, 64);if(ret < 0){if(ret == largefile::EXIT_DISK_OPER_INCOMPLETE){fprintf(stderr, " write_file: read length is less than required!");}else{fprintf(stderr, " write_file %s failed. reason: %s\n", filename, strerror(-ret));}}fileOP->close_file();return 0; }

參考資料:

  • C/C++從入門到精通-高級程序員之路【奇牛學院】
  • 總結

    以上是生活随笔為你收集整理的文件操作的封装的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。