VS2010 MFC中 创建文件夹及文件判空的方法


1. MFC中 创建文件夹的方法如下:

CString   strFolderPath = "./Output"; 
        //判断路径是否存在   
        if(!PathIsDirectory(strFolderPath))   
        {
            CString strMsg;
            strMsg.Format(_T("指定路径"%s"不存在,是否创建?"),strFolderPath);   
            if(AfxMessageBox(strMsg,MB_YESNO) == IDYES)   
            {   
                //判断文件夹是否创建成功 
                if(!CreateDirectory(strFolderPath,NULL))   
                {   
                    strMsg.Format(_T("创建路径"%s"失败!是否继续?"),strFolderPath);   
                    if(AfxMessageBox(strMsg,MB_YESNO)==IDYES)   
                        return;   
                }
                else
                {
                    MessageBox(_T("创建成功。"),_T("提示"));
                }
            }   
        }

BOOL CreateDirectory(LPCTSTR lpPathNameLPSECURITY_ATTRIBUTES lpSecurityAttributes);
      第一个参数值为文件夹名称,第二个参数值为安全属性,一般设置为NULL即可。如果正确创建,返回值为1,如果没有正常创建文件夹,则返回0。
特别的:该函数每次调用时都只能创建一级文件夹,即文件夹中不能再包含子文件夹。
      当希望创建含有子文件夹的文件夹时,可以先使用该函数创建一级文件夹,然后再使用该函数在一级文件夹下创建子文件夹。如:
希望创建:d:\TEST\temp,
      则:CString str = “d:\TEST”;
            CreateDirectory(str, NULL);
            str = str + “\temp”;
            CreateDirectory(str, NULL);
 
 
 

2. C++中 判断文件是否为空的方法如下:

  //写入excel文件
  FILE *factor = NULL;
  factor = fopen(strFolderPath + "/factor.csv", "a");
  //移动指针到文件末尾
  fseek( factor, 0, SEEK_END );
  //判断文件是否为空
  if(ftell(factor) == 0)
   {
     fprintf(factor, "序号,算法名称
");
    }
  fprintf(factor, "%s,%s
", str, S1);
  fclose(factor);
  factor  = NULL;
原文地址:https://www.cnblogs.com/lxt287994374/p/3418773.html