vc文件操作汇总—支持wince
一、判断文件及文件夹是否存在
// 判断文件是否存在
BOOL IsFileExist(const CString& csFile)
{
DWORD dwAttrib = GetFileAttributes(csFile);
return INVALID_FILE_ATTRIBUTES != dwAttrib && == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
}
// 判断文件夹是否存在
BOOL IsDirExist(const CString & csDir)
{
DWORD dwAttrib = GetFileAttributes(csDir);
return INVALID_FILE_ATTRIBUTES != dwAttrib && != (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 = { };
return != 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 = ;
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 = ; 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 == )
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[] == '.')
{
if (pFindData->cFileName[] == '\0' ||
(pFindData->cFileName[] == '.' &&
pFindData->cFileName[] == '\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()-] != 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 ;
}
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);
}
vc文件操作汇总—支持wince的更多相关文章
- VC文件操作
VC文件操作 重命名文件: 注意: 该操作对文件夹一样有效! CFileFind Finder; CString sOldPath = _T("D:\\tt.txt"); CStr ...
- VC++文件操作之最全篇
一.剖析VC中的文件操作 各种关于文件的操作在程序设计中是十分常见,如果能对其各种操作都了如指掌,就可以根据实际情况找到最佳的解决方案,从而在较短的时间内编写出高效的代码,因而熟练的掌握文件操作是十分 ...
- python文件操作汇总day7
Python处理文件 文件操作分为读.写.修改,我们先从读开始学习 读文件 示例1: f = open(file='D:/工作日常/兼职白领学生空姐模特护士联系方式.txt',mode='r',enc ...
- python文件操作汇总
1.创建文件 f = open(filename,'w+')
- c++对文件操作的支持(一)
#include <stdio.h> #include <iostream> #include <fstream> using namespace std; voi ...
- c++对文件操作的支持(二)
#include <stdio.h> #include <iostream> #include <fstream> using namespace std; voi ...
- 4 文件操作 支持图片 视频 mp3 文本等
#文件操作:send_file,支持图片 视频 mp3 文本等@app.route("/img")def img(): return send_file("1.jpg&q ...
- go语言文件操作,这期资料比较详细( 欢迎加入go语言群: 218160862 )
go语言文件操作,这期资料比较详细 欢迎加入go语言群: go语言深圳群 golang深圳 218160862 点击加入 文件操作 func Open(name string) (file *File ...
- C# 文件操作总结
一.需求分析 1.将信息记录到本地记事本中. 2.将记录的信息读取出来. 3.计算出某个文件夹下所有后缀名为txt的数量和dll的数量. 4.从网络上下载文件. 二.二话不说上代码 using Sys ...
随机推荐
- Python多继承C3算法
Python3 多继承的MRO算法选择.MRO(Method Resolution Order):方法解析顺序. Python3 只保留了C3算法! C3算法解析: 1.C3算法解析 C3算法:MRO ...
- LindAgile~缓存拦截器支持类的虚方法了
写它的原因 之前写过一个缓存拦截器,主要在方法上添加CachingAspect特性之后,它的返回值就可以被缓存下来,下次访问时直接从缓存中返回结果,而它有一个前提,就是你的方法需要是一个接口方法,缓存 ...
- (转载)Python中模块的发布与安装
模块(Module) Python中有一个概念叫做模块(module),这个和C语言中的头文件以及Java中的包很类似,比如在Python中要调用sqrt函数,必须用import关键字引入math这个 ...
- JAVA 框架之面向对象设计原则
面向对象设计原则: 单一职责原则 SRP : 一个类或者行为只做一件事 . 降低代码冗余,提高可重用性,可维护性,可扩展性,可读性 使用组合形式 里氏替换原则 LSP : 所有引用基类 ...
- Mybatis基础配置及增删查改操作
一.简介 平时我们都用JDBC访问数据库,除了需要自己写SQL之外,还必须操作Connection, Statement, ResultSet 这些其实只是手段的辅助类. 不仅如此,访问不同的表,还会 ...
- B/S架构 C/S架构 SOA架构
一.什么是C/S和B/S 第一.什么是C/S结构.C/S (Client/Server)结构,即大家熟知的客户机和服务器结构.它是软件系统体系结构,通过它可以充分利用两端硬件环境的优势,将任务合理分配 ...
- zuul prefix
经过测试,书上应该是写错了,如果要全部的路由加前缀,需要将zuul.stripPrefix=true进行设置 而不是书上所说的false
- IDEA Spring Boot 项目创建
1.创建新项目 2.选择 Spring Initializr 3.选择默认项或者修改其名称也可,直接下一步 4.选择 web选项以及spring boot版本(我用的是2.0.2) 5.为项目创建名称 ...
- 'gets' undeclared here (not in a function)
原文:http://www.cnblogs.com/hjj801006/p/3988220.html 1.在命令行输入:find -name stdio.in.h.查到有两个文件中含有stdio.in ...
- Android的bitmap和优化
内存管理是个永恒的话题! 内存溢出:就是分配的内存不足以放下数据项序列.如在一个域中输入的数据超过了它的要求就会引发数据溢出问题,多余的数据就可以作为指令在计算机上运行.就是你要求分配的内存超出了系统 ...