• 先引用相关头文件
#include <iostream>
#include <string>
#include <Windows.h>
#include <shlwapi.h>
#pragma comment(lib,"Shlwapi.lib")

添加封装好的方法

using namespace std;
//创建文件
//param:filePath: 需要创建的文件名称路径:
bool CreateTxtFile(std::wstring filePath)
{
//判断文件是否存在
if (PathFileExists(filePath.c_str()))
{
cout << "当前文件已存在" << endl;
return false;
}

HANDLE pFile = CreateFile(filePath.c_str(),GENERIC_ALL,FILE_SHARE_READ,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
if (pFile==NULL)
{
cout << "当前文件创建失败" << endl;
CloseHandle(pFile);
return false;
}
else
{
cout << "当前文件创建成功" << endl;
CloseHandle(pFile);
return true;
}

}
  • 简单的调用:
int _tmain(int argc, _TCHAR* argv[])
{
CreateTxtFile(L"F:\\dssemo.txt");
system("pause");
return 0;
}