vc文件操作汇总—支持wince
程序员文章站
2022-07-04 22:21:19
一、判断文件及文件夹是否存在 二、创建及删除目录 三、查看指定根目录下目录个数 四、文件记录操作 五、CCeFileFind — wince 由于wince中不支持CFileFind,在此提供网上查找的CCeFileFind类,经过实践证明可用。 ......
一、判断文件及文件夹是否存在
// 判断文件是否存在 bool isfileexist(const cstring& csfile) { dword dwattrib = getfileattributes(csfile); return invalid_file_attributes != dwattrib && 0 == (dwattrib & file_attribute_directory); } // 判断文件夹是否存在 bool isdirexist(const cstring & csdir) { dword dwattrib = getfileattributes(csdir); return invalid_file_attributes != dwattrib && 0 != (dwattrib & file_attribute_directory); } // 判断文件或文件夹是否存在 bool ispathexist(const cstring & cspath) { dword dwattrib = getfileattributes(cspath); return invalid_file_attributes != dwattrib; } // 判断文件或文件夹是否存在 bool ispathexistex(const cstring & cspath) { win32_file_attribute_data attrs = { 0 }; return 0 != getfileattributesex(cspath, getfileexinfostandard, &attrs); }
二、创建及删除目录
// 创建目录
bool createfolder(cstring strpath) { security_attributes attrib; attrib.binherithandle = false; attrib.lpsecuritydescriptor = null; attrib.nlength = sizeof(security_attributes); return ::createdirectory(strpath, &attrib); //return ::createdirectory(strpath,null); }
// 删除目录及子目录 bool deletedirectory(cstring dirname) { cstring pubpath; pubpath = dirname; cfilefind tempfind; dirname += "\\*.*"; bool isfinded = (bool)tempfind.findfile(dirname); while(isfinded) { isfinded = (bool)tempfind.findnextfile(); if(!tempfind.isdots()) { cstring strdirname; strdirname += pubpath; strdirname += "\\"; strdirname += tempfind.getfilename(); if(tempfind.isdirectory()) { deletedirectory(strdirname); } else { setfileattributes(strdirname, file_attribute_normal); //去掉文件的系统和隐藏属性 deletefile(strdirname); } } } tempfind.close(); if(!removedirectory(pubpath)) { return false ; } return true; }
三、查看指定根目录下目录个数
// 获取目录数
int getdirnum(cstring strpath) { int ndirnum = 0; cfilefind tempfind; bool bfound; bfound = tempfind.findfile(strpath + _t("\\*.*")); while (bfound) { bfound = tempfind.findnextfile(); if(!tempfind.isdots()) { if(tempfind.isdirectory()) // 判断是否为文件夹 ndirnum++; } } tempfind.close(); return ndirnum; }
四、文件记录操作
// 文件末尾写入记录 (以时间为例)
void writerecord(time_t tm, cstring strrecordfilepath) { ctime ctm(tm); cstring strtime = ctm.format(_t("%y-%m-%d %h_%m_%s")); cstdiofile stdiofile; if (stdiofile.open(strrecordfilepath, cfile::modewrite)) { stdiofile.seektoend(); stdiofile.writestring(strtime); stdiofile.close(); } }
// 读取第一条记录 cstring readrecord(cstring strrecordfilepath) { cstring strfirstrecord = _t(""); cstdiofile stdiofile; if (stdiofile.open(strrecordfilepath, cfile::modereadwrite)) { stdiofile.seektobegin(); stdiofile.readstring(strfirstrecord); stdiofile.close(); } return strfirstrecord; }
// 删除第一条记录 void deleterecord(cstring strrecordfilepath) { vector<cstring> vecstr; cstring strtemp; cstdiofile stdiofile; if (stdiofile.open(strrecordfilepath, cfile::modenotruncate|cfile::moderead)) { stdiofile.readstring(strtemp); while (stdiofile.readstring(strtemp)) { vecstr.push_back(strtemp); } stdiofile.close(); stdiofile.open(strrecordfilepath, cfile::modecreate|cfile::modewrite); for (int i = 0; i < (int)vecstr.size(); i++) { stdiofile.writestring(vecstr[i]); } stdiofile.close(); } }
五、ccefilefind — wince
由于wince中不支持cfilefind,在此提供网上查找的ccefilefind类,经过实践证明可用。
//================================头文件=================================== #if !defined _cefilefind_h_ #define _cefilefind_h_ // cefilefind.h : header file #include <afxwin.h> ///////////////////////////////////////////////////////////////////////////// // ccefilefind window class ccefilefind : public cwnd { // construction public: ccefilefind( ); public: // operations void close(); virtual bool findnextfile( ); virtual bool findfile( lpctstr pstrname = null); public: // attributes //gets the length of the found file, in bytes. dword getlength() const; //gets the name, including the extension, of the found file cstring getfilename() const; //gets the whole path of the found file. cstring getfilepath() const; //gets the whole path of the found file. cstring getroot() const; // to get the time the specified file was created virtual bool getcreationtime( filetime* pfiletime ) const; virtual bool getcreationtime( ctime& reftime ) const; //gets the time that the file was last accessed. virtual bool getlastaccesstime( ctime& reftime ) const; virtual bool getlastaccesstime( filetime* pfiletime ) const; //gets the time the file was last changed and saved. virtual bool getlastwritetime( filetime* pfiletime ) const; virtual bool getlastwritetime( ctime& reftime ) const; //indicates the desired file attributes of the file to be found. virtual bool matchesmask( dword dwmask ) const; //determines if the name of the found file has the name "." or "..", //indicating that is actually a directory. virtual bool isdots( ) const; //determines if the found file is read-only. bool isreadonly( ) const; //determines if the found file is a directory. bool isdirectory( ) const; //determines if the found file is compressed. bool iscompressed( ) const; //determines if the found file is a system file. bool issystem( ) const; //determines if the found file is hidden. bool ishidden( ) const; //determines if the found file is temporary. bool istemporary( ) const; //determines if the found file is normal (in other words, has no other attributes). bool isnormal( ) const; //determines if the found file is archived. bool isarchived( ) const; // implementation public: virtual ~ccefilefind(); private: lpwin32_find_data m_pfiledata; lpwin32_find_data m_pnextdata; cstring m_csroot; handle m_hfilehandle; char m_chdirseparator; void assertdonenext() const; }; #endif // !defined _cefilefind_h_
//====================================源文件=============================== // cefilefind.cpp : implementation file // #include "stdafx.h" #include "cefilefind.h" #ifdef _debug #define new debug_new #undef this_file static char this_file[] = __file__; #endif #define delete_pointer(ptr) if( ptr != null ) \ { \ delete ptr; \ ptr = null; \ } #define dir_seperator '\\' ///////////////////////////////////////////////////////////////////////////// // ccefilefind ccefilefind::ccefilefind() :m_hfilehandle(null), // initialize to null m_pfiledata(null) { } ccefilefind::~ccefilefind() { close(); } // operations bool ccefilefind::findfile(lpctstr pstrname) { //close(); // if null , wild card search if( null == pstrname ) { m_csroot = dir_seperator; pstrname = _t("\\*.*"); } else { m_csroot = pstrname; int npos = m_csroot.reversefind( '\\' ); if( npos == 0 ) m_csroot = '\\'; else m_csroot = m_csroot.left( npos ); } m_pnextdata = new win32_find_data; // search for file m_hfilehandle = findfirstfile( pstrname, m_pnextdata ); if ( m_hfilehandle == invalid_handle_value ) { close(); return false; } // file was found return true; } bool ccefilefind::findnextfile() { assert(m_hfilehandle != null); if (m_hfilehandle == null) return false; if (m_pfiledata == null) m_pfiledata = new win32_find_data; assertdonenext(); lpwin32_find_data ptemp = m_pfiledata; m_pfiledata = m_pnextdata; m_pnextdata = ptemp; return ::findnextfile(m_hfilehandle, m_pnextdata); } void ccefilefind::close() { delete_pointer( m_pfiledata ); delete_pointer( m_pnextdata ); if( m_hfilehandle!= null && m_hfilehandle != invalid_handle_value ) { ::findclose( m_hfilehandle ); m_hfilehandle = null; } } bool ccefilefind::matchesmask(dword dwmask) const { assert(m_hfilehandle != null); assertdonenext(); if ( m_pfiledata != null) return (!!(m_pfiledata->dwfileattributes & dwmask) ); else return false; } cstring ccefilefind::getroot() const { assert(m_hfilehandle != null); assertdonenext(); if (m_pfiledata != null) return m_csroot; else return cstring(""); } bool ccefilefind::getlastaccesstime(filetime* ptimestamp) const { assert(m_hfilehandle != null); assert(ptimestamp != null); assertdonenext(); if (m_pfiledata != null && ptimestamp != null) { *ptimestamp = m_pfiledata -> ftlastaccesstime; return true; } else return false; } bool ccefilefind::getlastwritetime(filetime* ptimestamp) const { assert(m_hfilehandle != null); assert(ptimestamp != null); assertdonenext(); if (m_pfiledata != null && ptimestamp != null) { *ptimestamp = m_pfiledata -> ftlastwritetime; return true; } else return false; } bool ccefilefind::getcreationtime(filetime* ptimestamp) const { assert(m_hfilehandle != null); assertdonenext(); if (m_pfiledata != null && ptimestamp != null) { *ptimestamp = m_pfiledata -> ftcreationtime; return true; } else return false; } bool ccefilefind::getlastaccesstime(ctime& reftime) const { assert(m_hfilehandle != null); assertdonenext(); if (m_pfiledata != null) { reftime = ctime( m_pfiledata -> ftlastaccesstime ); return true; } else return false; } bool ccefilefind::getlastwritetime(ctime& reftime) const { assert(m_hfilehandle != null); assertdonenext(); if (m_pfiledata != null) { reftime = ctime( m_pfiledata -> ftlastwritetime ); return true; } else return false; } bool ccefilefind::getcreationtime(ctime& reftime) const { assert(m_hfilehandle != null); assertdonenext(); if (m_pfiledata != null) { reftime = ctime( m_pfiledata -> ftcreationtime ); return true; } else return false; } bool ccefilefind::isdots() const { assert(m_hfilehandle != null); assertdonenext(); // return true if the file name is "." or ".." and // the file is a directory bool bresult = false; if (m_pfiledata != null && isdirectory()) { lpwin32_find_data pfinddata = m_pfiledata; if (pfinddata->cfilename[0] == '.') { if (pfinddata->cfilename[1] == '\0' || (pfinddata->cfilename[1] == '.' && pfinddata->cfilename[2] == '\0')) { bresult = true; } } } return bresult; } bool ccefilefind::isarchived( ) const { assert(m_hfilehandle != null); assertdonenext(); if (m_pfiledata != null) { if( m_pfiledata -> dwfileattributes == file_attribute_archive ) return true; else return false; } return false; } bool ccefilefind::iscompressed() const { assert(m_hfilehandle != null); assertdonenext(); if (m_pfiledata != null) { if( m_pfiledata -> dwfileattributes == file_attribute_compressed ) return true; else return false; } return false; } bool ccefilefind::isdirectory() const { assert(m_hfilehandle != null); assertdonenext(); if (m_pfiledata != null) { if( m_pfiledata -> dwfileattributes & file_attribute_directory ) return true; else return false; } return false; } bool ccefilefind::ishidden() const { assert(m_hfilehandle != null); assertdonenext(); if (m_pfiledata != null) { if( m_pfiledata -> dwfileattributes & file_attribute_hidden ) return true; else return false; } return false; } bool ccefilefind::isnormal() const { assert(m_hfilehandle != null); assertdonenext(); if (m_pfiledata != null) { if( m_pfiledata -> dwfileattributes & file_attribute_normal ) return true; else return false; } return false; } bool ccefilefind::isreadonly() const { assert(m_hfilehandle != null); assertdonenext(); if (m_pfiledata != null) { if( m_pfiledata -> dwfileattributes & file_attribute_readonly ) return true; else return false; } return false; } bool ccefilefind::issystem() const { assert(m_hfilehandle != null); assertdonenext(); if (m_pfiledata != null) { if( m_pfiledata -> dwfileattributes & file_attribute_system ) return true; else return false; } return false; } bool ccefilefind::istemporary() const { assert(m_hfilehandle != null); assertdonenext(); if (m_pfiledata != null) { if( m_pfiledata -> dwfileattributes & file_attribute_temporary ) return true; else return false; } return false; } cstring ccefilefind::getfilepath() const { assert(m_hfilehandle != null); assertdonenext(); cstring csresult = m_csroot; if (csresult[csresult.getlength()-1] != dir_seperator ) csresult += dir_seperator; csresult += getfilename(); return csresult; } cstring ccefilefind::getfilename() const { assert(m_hfilehandle != null); assertdonenext(); cstring ret; if (m_pfiledata != null) ret = m_pfiledata->cfilename; return ret; } dword ccefilefind::getlength() const { assert(m_hfilehandle != null); assertdonenext(); if (m_pfiledata != null) return m_pfiledata -> nfilesizelow; else return 0; } void ccefilefind::assertdonenext() const { // if you trip the assert in the else side, you've called // a get() function without having done at least one // findnextfile() call if (m_hfilehandle == null) assert( m_pfiledata == null && m_pnextdata == null); else assert( m_pfiledata != null && m_pnextdata != null); }
上一篇: 看似风光的梁山好汉,其实都是被逼无奈
下一篇: Vue-不允许嵌套式的渲染方法