C 语言的io流
寫入:
? ? 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, "%s\\%s", DirName, FindData.cFileName);
??? ::DeleteFile(tmppath);????
?? }
}?
::FindClose(hFirstFile);
if(!RemoveDirectory(DirName))
{
?? return false ;
}
return true;
}
總結
- 上一篇: Qt:Qt实现飞秋拦截助手—Mac地址扫
- 下一篇: 数据结构与算法:企业级链表实现(超详细)