文件操作的几个函数简述

文件操作的几个函数简述

[1]判断文件是否存在,并判断该文件有什么属性
     access(filename,mode);

[2]显示对话框,取得文件名

      CString FilePathName;
      CFileDialog dlg(TRUE);///TRUE为OPEN对话框,FALSE为S***E AS对话框
      if (dlg.DoModal() == IDOK)
      FilePathName=dlg.GetPathName();

     相关信息:CFileDialog 用于取文件名的几个成员函数:
      假如选择的文件是C:/WINDOWS/TEST.EXE,则:
      (1)GetPathName() ;取文件名全称,包括完整路径。取回C:/WINDOWS/TEST.EXE
      (2)GetFileTitle();      取文件全名 TEST.EXE
      (3)GetFileName();   取文件名 TEST
      (4)GetFileExt();         取扩展名 EXE

[3]打开文件
      CFile file("C:/HELLO.TXT",CFile::modeRead);//只读方式打开
     //CFile::modeRead可改为 CFile::modeWrite(只写),
     //CFile::modeReadWrite(读写),CFile::modeCreate(新建)
     例子:
     CFile file;
     file.Open("C:/HELLO.TXT",CFile::modeCreate|Cfile::modeWrite);

[4]移动文件指针
      file.Seek(100,CFile::begin);///从文件头开始往下移动100字节
      file.Seek(-50,CFile::end);///从文件末尾往上移动50字节
      file.Seek(-30,CFile::current);///从当前位置往上移动30字节
      file.SeekToBegin();///移到文件头
      file.SeekToEnd();///移到文件尾

[5]文件/目录操作
如果是目录 if(finder.IsDirectory() && !finder.IsDots())  
删除文件   DeleteFile(LPCSTR filename);
删除空目录 RemoveDirectory(LPCSTR filename);
删除非空目录 function如下:

BOOL DeleteDirectory(char *DirName)
{
   CFileFind tempFind;
   char tempFileFind[200];
   sprintf(tempFileFind,"%s//*.*",DirName);
   BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind);
   while(IsFinded)
   {
      IsFinded=(BOOL)tempFind.FindNextFile();
      if(!tempFind.IsDots())
      {
         char foundFileName[200];
         strcpy(foundFileName,tempFind.GetFileName().GetBuffer(200));
         if(tempFind.IsDirectory())
         {
            char tempDir[200];
            sprintf(tempDir,"%s//%s",DirName,foundFileName);
            DeleteDirectory(tempDir);
         }
         else
         {
            char tempFileName[200];
            sprintf(tempFileName,"%s//%s",DirName,foundFileName);
            DeleteFile(tempFileName);
         }
      }
   }
   tempFind.Close();
   if(!RemovwDirctory(DirName))
   {
      MessageBox(0,"删除目录失败!","警告信息",MK_OK);
      return FALSE;
   }
   return TRUE;
}

创建文件夹/imags*
void CreatFoleder(CString strDirectory)
{
      if(strDirectory.Right(1) != "//")
     {
            strDirectory += "//"; 
    } 
    CFileFind ff;
    for(int i = 1; i <= 100 ; i++)
    {
          CString strTempDiretory;
          CString strTempDiretoryNum;
          strTempDiretoryNum.Format(_T(imags"%d") , i);
          strTempDiretory = strDirectory;
          strTempDiretory += strTempDiretoryNum;
          BOOL res = ff.FindFile(strTempDiretory);
          ff.FindNextFile;
          if(!res)
         {
               DebugPrint(L"find folder min num!");
               return;
         }
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值