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就在工程文件目录下,我试了好多方法,都不行,我也操作过其它的文件读书,都能打开,就这个老是打不开,是什么原因呢?
展开
 我来答
任明星Ming
2013-10-10 · TA获得超过798个赞
知道小有建树答主
回答量:1061
采纳率:100%
帮助的人:1181万
展开全部

删除文件的方法可以用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;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式