
MFC CFile打不开文件
想实现一个功能,当磁盘空间快满时,自动删除文件。当创建文件时,我把文件名写在了videoname.txt文件中,要删除文件时再一行一行地读出,可是现在打不开文件。CStd...
想实现一个功能,当磁盘空间快满时,自动删除文件。当创建文件时,我把文件名写在了videoname.txt文件中,要删除文件时再一行一行地读出,可是现在打不开文件。
CStdioFile filename;
CFileException e;
if(!filename.Open("videoname.txt",CFile::modeCreate|CFile::typeText|CFile::modeReadWrite,&e))
{
AfxMessageBox("打开文件出错!");
}
if(filename.Open("videoname.txt",CFile::modeCreate|CFile::typeText|CFile::modeReadWrite,&e))
{
AfxMessageBox("打开文件成功!");
}
if循环进不去,文件没有打开成功,videoname.txt就在工程文件目录下,我试了好多方法,都不行,我也操作过其它的文件读书,都能打开,就这个老是打不开,是什么原因呢? 展开
CStdioFile filename;
CFileException e;
if(!filename.Open("videoname.txt",CFile::modeCreate|CFile::typeText|CFile::modeReadWrite,&e))
{
AfxMessageBox("打开文件出错!");
}
if(filename.Open("videoname.txt",CFile::modeCreate|CFile::typeText|CFile::modeReadWrite,&e))
{
AfxMessageBox("打开文件成功!");
}
if循环进不去,文件没有打开成功,videoname.txt就在工程文件目录下,我试了好多方法,都不行,我也操作过其它的文件读书,都能打开,就这个老是打不开,是什么原因呢? 展开
1个回答
展开全部
删除文件的方法可以用CFileFind类,请试试下面的代码:
void DelFiles(CString& strDirPath,CString strFileName, BOOL bDelAll)
{
CFileFindFileFinder;
strDirPath = strDirPath + _T("\\");
CStringstrFilePath = strDirPath + strFileName;
while(TRUE)
{
if(FileFinder.FindFile(strFilePath))
{
FileFinder.FindNextFile();
CStringstrDelFilePath =FileFinder.GetFileName();
strDelFilePath= strDirPath + strDelFilePath;
DelDirectory(strDelFilePath);
if(!bDelAll)
{
break;
}
}
else
{
break;
}
}
FileFinder.Close();
}
更多追问追答
追问
可是要获得文件名还得从文本文件中读取啊,我现在连文本文件都打不开,就得不到文件名
追答
试试我的读txt的程序,一行行的读取。
BOOL ReadTXTFile(const CString&strFilePath, CString& strContents)
{
CStdioFileFile;
if( File.Open(strFilePath, CFile::modeRead) )
{
CStringstr;
while( File.ReadString(str) )
{
str= str + _T("\n");
strContents= strContents + str;
}
returnTRUE;
}
returnFALSE;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询