写入:
CString strCMD = L“E:\CompressionRar\CompressionRar\Bin\Release\abc.txt”;
USES_CONVERSION;
std::string strFptxt(W2A(strCMD));
FILE *fp=fopen(strTextPath.c_str(),"at");
fprintf(fp,"%s\n",strFptxt.c_str());
fclose(fp);
读出:
//UniCode CString 转 char*
USES_CONVERSION;
char * cFileName = T2A(pFileName);
char cTxt[100] = {0};
FILE *fp=fopen(strTextPath.c_str(),"rt");
while(fscanf(fp,"%s",cTxt) != EOF)
{
CString abc(cTxt);
AfxMessageBox(abc);
}
fclose(fp);
删除:
第一种方法:定义一个文件类对象来操作
CFile TempFile;
TempFile.Remove(指定文件名);
第二种方法:
DeleteFile("c:\\abc\\test.exe ");//MFC框架中可直接调用此函数
删除目录
_rmdir()
DeleteDirectory(sTempDir)
RemoveDirectory(sTempDir)
//删除文件夹目录(非空)
方法一
bool DeleteDirectory( CString DirName)
{
AfxMessageBox("执行删除文件夹:"+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();
AfxMessageBox("strDirName :"+strDirName);
if(tempFind.IsDirectory())
{
//strDirName += PUBPATH;
DeleteDirectory(strDirName);
}
else
{
SetFileAttributes(strDirName,FILE_ATTRIBUTE_NORMAL); //去掉文件的系统和隐藏属性
DeleteFile(strDirName);
}
}
}
tempFind.Close();
if(!RemoveDirectory(PUBPATH))
{
return false ;
}
AfxMessageBox("文件夹删除成功...");
return true;
}
方法二
bool DeleteDirectory( char* DirName)
{
HANDLE hFirstFile = NULL;
WIN32_FIND_DATA FindData;
char currdir[MAX_PATH] = {0};
sprintf(currdir,"%s\\*.*",DirName);
hFirstFile = ::FindFirstFile(currdir,&FindData);
if( hFirstFile == INVALID_HANDLE_VALUE )
return false;
BOOL bRes = true;
while(bRes)
{
bRes = ::FindNextFile(hFirstFile,&FindData);
if( (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) //发现目录
{
if( !strcmp(FindData.cFileName,".") || !strcmp(FindData.cFileName,"..") ) //.或..
continue;
else
{
char tmppath[MAX_PATH] = {0};
sprintf(tmppath,"%s\\%s",DirName,FindData.cFileName);
DeleteDirectory(tmppath);
}
}
else //发现文件
{
char tmppath[MAX_PATH] = {0};
sprintf(tmppath,FindData.cFileName);
::DeleteFile(tmppath);
}
}
::FindClose(hFirstFile);
if(!RemoveDirectory(DirName))
{
return false ;
}
return true;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。