VC++下文件内容及文件名的一些操作
VC++下文件内容及文件名的一些操作1.根据文件名全路径,取得文件后辍名
/**
* 取得文件后辍名
*/
CString CMyFun::GetFileExtNameFromFullPath(CString lpszFullPath)
{
// 同理,获取文件后缀名则为:
CString m_strOriFileName = lpszFullPath;
m_strOriFileName.Replace(_T("\\"), _T("\/"));
CString strFileType;
int ipos = m_strOriFileName.ReverseFind('.');
if (ipos == -1) {
strFileType = _T("");
}
else
{
int n = m_strOriFileName.GetLength() - ipos - 1;
strFileType = m_strOriFileName.Right(n);
}
return strFileType;
}2.判断文件是否存在
//判断文件是否存在
BOOL CMyFun::FileExist(CString FileName)
{
CFileFind fFind;
return fFind.FindFile(FileName);
}3.取得文件名(全名)
/**
* 取得文件名(全名)
*/
CString CMyFun::GetFileFullNameFromFullPath(CString lpszFullPath)
{
ASSERT(!lpszFullPath.IsEmpty());
// 获取文件名:
CString m_strOriFileName = lpszFullPath;
//m_strOriFileName.Replace(_T("\\"), _T("/"));
CString strPath;
int n = m_strOriFileName.GetLength() - m_strOriFileName.ReverseFind('\\') - 1;
strPath = m_strOriFileName.Right(n);
return strPath;
}4.取得文件名(不包含后辍)
/**
* 取得文件名
*/
CString CMyFun::GetFileNameFromFullPath(CString lpszFullPath)
{
ASSERT(!lpszFullPath.IsEmpty());
// 获取文件名:
CString m_strOriFileName = lpszFullPath;
//m_strOriFileName.Replace(_T("\\"), _T("/"));
CString strPath;
int n = m_strOriFileName.GetLength() - m_strOriFileName.ReverseFind('\\') - 1;
strPath = m_strOriFileName.Right(n);
//分解文件名
int ipos = strPath.Find('.');
CString strtmp = strPath.Left(ipos);
return strtmp;
}5.追加写入文件内容(追加到文件尾)
//追加写入文件内容(追加到文件尾)
void CMyFun::File_AppendLineToFile(CString filepath, CString strLineContent)
{
CFile fileDst;
fileDst.Open(filepath, CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate);
//保存数据
fileDst.SeekToEnd();
strLineContent += _T("\r\n");
fileDst.Write(strLineContent, strLineContent.GetLength());
//-------------
fileDst.Close();//
}6.写入内容到文件中(覆盖写入)
//写入内容到文件中(覆盖写入)
void CMyFun::File_WriteToFile(CString filepath, CString strContent)
{
CFile fileDst;
fileDst.Open(filepath, CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate);
fileDst.Write(strContent, strContent.GetLength());
fileDst.Close();
}7.清空文本文件内容
//清空文本文件内容
voidCMyFun::File_NullFile(CString strFilePath)
{
FILE* file;
fopen_s(&file, strFilePath, "wb+");
fclose(file);
}8.读取文本内容(按行读取)unicode环境下读取ansi文本中文乱码
//读取文本内容(按行读取)unicode环境下读取ansi文本中文乱码
CString CMyFun::File_ReadAllLine(CString filepath)
{
CStdioFile file;
//打开文件
if (!file.Open(filepath, CFile::modeRead))
{
AfxMessageBox(_T("文件打开失败。"));
return _T("");
}
CString strContent;
CString strText = _T("");
while (file.ReadString(strText))
{
//写入
strText += _T("\r\n");
strContent += strText;//-------注释部分,全部一起保存
}
//关闭文件
file.Close();
//
return strContent;
}9.从文件中读取数据到CStrngArray中
//从文件中读取数据到CStrngArray中
void CMyFun::File_ReadToCStringArray(CString filepath, CStringArray& dest)
{
CStdioFile file;
//打开文件
if (!file.Open(filepath, CFile::modeRead))
{
AfxMessageBox(_T("文件打开失败。"));
return;
}
CString strContent;
CString strText = _T("");
while (file.ReadString(strText))
{
//写入
if (strText != _T(""))
{
//声明标识符
//USES_CONVERSION;
//char* p = T2A(strText);//
dest.Add(strText);
}
}
//关闭文件
file.Close();
}10.将CStringArray中数据保存到file中
//将CStringArray中数据保存到file中
void CMyFun::File_SaveCStringArrayToFile(CString filepath, CStringArray &slist)
{
CFile fileDst;
fileDst.Open(filepath, CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate);
CString strContent = _T("");
if (slist.GetCount() > 0) {
for (int i = 0; i < slist.GetCount(); i++) {
CString temp = slist.GetAt(i);
strContent += temp + _T("\r\n");
}
}
fileDst.Write(strContent, strContent.GetLength());
fileDst.Close();
}
页:
[1]