一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

C++中判断文件是否存在

时间:2013-07-14 编辑:简简单单 来源:一聚教程网

判断文件

 代码如下 复制代码


#includevoid main(){  
ofstream f("c:\test.txt");
//打开文件test.txt,用于写,若文件不存在就先创建它 

 代码如下 复制代码

#include
#include
using namespace std;
#define FILENAME "stat.dat"
int main()
{
     fstream _file;
     _file.open(FILENAME,ios::in);
     if(!_file)
     {
         cout<       }
      else
      {
          cout<       }
      return 0;
}

char szDirName[] = "文件路径";

 代码如下 复制代码
CreateDirectory(szDirName, NULL) ;

这样就可以了

CreateDirectory
功能:
This function creates a new directory. If the underlying file system supports security on files and directories, the function applies a specified security descriptor to the new directory.
(创建一个新的文件夹。如果基本的文件系统在文件或文件夹上支持安全描述,那么该函数将在新建的文件夹上应用指定的安全描述)
原型:

 代码如下 复制代码
BOOL CreateDirectory(
  LPCTSTR lpPathName,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes
);

参数:
lpPathName:包含将被创建的文件夹路径的字符串,字符串的长度不超过MAX_PATH。
lpSecurityAttributes:忽略,设为NULL
返回值:
成功则返回非零,失败则返回零

热门栏目