码迷,mamicode.com
首页 > 编程语言 > 详细

关于c++对文件读写的封装

时间:2018-05-22 19:49:05      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:name   end   size   auto_ptr   turn   return   sub   dwr   stat   

  1 namespace 
  2 {
  3     UINT_T GetWriteSizeForNoBuf(UINT_T fsize)
  4     {
  5         UINT_T write_buf_size = 0;
  6         if (fsize % 512 == 0)
  7         {
  8             write_buf_size = fsize;
  9         }
 10         else
 11         {
 12             write_buf_size = (fsize / 512 + 1)* 512;
 13         }
 14         return write_buf_size;
 15     }
 16     
 17 }
 18 
 19 //将文件解析成路径,名称,类型
 20 bool CFileBak::ParseFilePath(const string& file_path_name, string& file_path, string& file_name, string& file_type)
 21 {
 22     size_t path_pos = file_path_name.find_last_of("/\\"); // 考虑/,\两种表示路径的方法
 23     if (path_pos == string::npos)
 24     {
 25         assert(0);
 26         return false;
 27     }
 28     file_path = file_path_name.substr(0, path_pos);
 29     file_name = file_path_name.substr(path_pos + 1);
 30     size_t type_pos = file_name.find_last_of(".");
 31     if (type_pos != string::npos)
 32     {
 33         file_type = file_name.substr(type_pos + 1);
 34         file_name = file_name.substr(0, type_pos);
 35     }
 36     return true;
 37 }
 38 
 39 bool CFileBak::ReadFile(std::string& buf, const std::string& file_path_name, const std::string& bak_name_tmp)
 40 {
 41     bool need_bak = true;
 42     FILE* ptr_file = NULL;
 43     size_t fsize = 0;
 44     string bak_name = bak_name_tmp;
 45     bool tmp_false = false;
 46     do
 47     {
 48         fopen_s(&ptr_file, file_path_name.c_str(), "rb");
 49         if (NULL == ptr_file)
 50         {
 51             break;
 52         }
 53 
 54         //get file size
 55         fseek(ptr_file, 0, SEEK_END);
 56         fsize = (ftell(ptr_file));
 57         fseek(ptr_file, 0, SEEK_SET);
 58         if (fsize == 0)
 59         {
 60             fclose(ptr_file);
 61             ptr_file = NULL;
 62             break;
 63         }
 64         need_bak = false;
 65     } while (tmp_false);
 66 
 67     if (need_bak)
 68     {
 69         //assert(0); // 已经属于异常范畴了
 70         if (bak_name.empty())
 71         {
 72             string file_path;
 73             string file_name;
 74             string file_type;
 75             if (!ParseFilePath(file_path_name, file_path, file_name, file_type))
 76             {
 77                 assert(0);
 78                 return false;
 79             }
 80             bak_name = file_path + "/" + file_name + ".data";
 81         }
 82         
 83         fopen_s(&ptr_file, bak_name.c_str(), "rb");
 84         if (NULL == ptr_file)
 85         {
 86             //assert(0);
 87             return false;
 88         }
 89 
 90         //get file size
 91         fseek(ptr_file, 0, SEEK_END);
 92         fsize = (ftell(ptr_file));
 93         fseek(ptr_file, 0, SEEK_SET);
 94     }
 95     if (nullptr == ptr_file)
 96     {
 97         assert(0);
 98         return false;
 99     }
100 
101     if (0 == fsize)
102     {
103         assert(0);
104         fclose(ptr_file);
105         return false;
106     }
107 
108     auto_ptr<char> pBuf(new char[fsize]);
109     memset(pBuf.get(), 0, fsize);
110     size_t read_size = fread(pBuf.get(), 1, fsize, ptr_file);
111     fclose(ptr_file);
112     buf.assign(pBuf.get(), read_size);
113     //buf = string(pBuf.get(), read_size);
114 
115     size_t n_pos = buf.find_last_not_of(\n);
116     if (string::npos != n_pos)
117     {
118         buf = buf.substr(0, n_pos + 1);
119     }
120     return true;
121 }
122 
123 bool CFileBak::WriteFile(const std::string& file_name, const std::string& buf)
124 {
125     HANDLE hfile = CreateFileA(file_name.c_str(), GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_ALWAYS, FILE_FLAG_NO_BUFFERING, 0);
126     if (INVALID_HANDLE_VALUE == hfile)
127     {
128         assert(0);
129         CloseHandle(hfile);
130         return false;
131     }
132 
133     LARGE_INTEGER large_file_size;
134     if (!GetFileSizeEx(hfile, &large_file_size))
135     {
136         assert(0);
137         CloseHandle(hfile);
138         return false;
139     }
140     size_t n_pos = buf.find_last_not_of(\n);
141 
142     string write_buf;
143     if (string::npos != n_pos)
144     {
145         write_buf = buf.substr(0, n_pos + 1);
146     }
147     else
148     {
149         write_buf = buf;
150     }
151     UINT_T write_size = GetWriteSizeForNoBuf(max(large_file_size.LowPart, static_cast<UINT_T>(write_buf.length())));
152     if (write_size == 0)
153     {
154         assert(0);
155         CloseHandle(hfile);
156         return false;
157     }
158 
159     auto_ptr<char> ptr_write_buf(new char[write_size]);
160     memset(ptr_write_buf.get(), \n, write_size); // 将换行写在文件中不影响使用,写0都会影响xml的功能
161     memcpy(ptr_write_buf.get(), write_buf.c_str(), write_buf.length());
162 
163     DWORD dwret = 0;
164     ::WriteFile(hfile, ptr_write_buf.get(), write_size, &dwret, NULL);
165     CloseHandle(hfile);
166     return true;
167 }

 

关于c++对文件读写的封装

标签:name   end   size   auto_ptr   turn   return   sub   dwr   stat   

原文地址:https://www.cnblogs.com/wangshaowei/p/9073590.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!