返回当前运行程序的路径,并在上一级创建新文件夹,然后复制一个文件到该新文件夹。
环境:win10 VS2019 控制台应用
头文件有点乱我也不太记得必须的头文件是哪几个了0..0
#pragma once
#include <iostream>
#include <cmath>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <string.h>
#include <direct.h>
#include <Windows.h>
//#include <shlwapi.h>
#include <tchar.h>
#include <vector>
#include <io.h>
#ifndef UNICODE
typedef std::string String;
#else
typedef std::wstring String;
#endif
using namespace std;
char* TCHARToChar(TCHAR*);//将TCHAR装换为char
vector<string> split(string, string);//以第二个string分割第一个string,然后放入vector返回
LPCWSTR stringToLPCWSTR(string);//将string转换为LPCWSTR
int main()
{
TCHAR szPath[MAX_PATH];//用于保存返回的路径,用TCHAR是由于函数定义就这样
stringstream aaa;string bbb;
string lujing{};
if (!GetModuleFileName(NULL, szPath, MAX_PATH))//获取当前进程已加载模块的文件的完整路径,该模块必须由当前进程加载。
{
printf("Cannot get the module file name, error: (%d) \n", GetLastError());
}
else
{
//printf("Module file name: %ls \n", szPath);
aaa << TCHARToChar(szPath);aaa >> bbb;//将TCHAR szPath转换为string bbb
//cout << bbb << endl;
}
//---------------------------------------------
string pattern = "\\";//用"\"分割
std::vector<std::string> result = split(bbb, pattern);//分割字符串,以vector形式放入result中
//std::cout << "The result:" << std::endl;
//for (int i = 0; i < result.size(); i++)
//{
// std::cout << result[i] << std::endl;
//}
//cout << "\\\\" << endl;
lujing = "";//置空字符串
for (int i = 0; i < result.size() - 2; i++)
{
lujing = lujing + result[i] + "\\\\";
}
//cout << lujing << endl;
//----------在当前路径新建一个叫result的文件夹-----------------------------------------
string folderPath = lujing + "result";
string command;
if (_access(folderPath.c_str(), 0) == -1)
{
command = "mkdir " + folderPath;
cout << command << endl;
system(command.c_str());
}
else
{
cout << "已经存在" << endl;
}
//----------从fileP路径复制到filePN路径-----------------------------------------
string filePN, fileP;
fileP = "C:\\Users\\hys\\Desktop\\新建文本文档(2).txt";
filePN = lujing +"result\\\\"+ "data.txt";//四条斜杠其实只是两条
cout << filePN << endl;
LPCWSTR LfileP, LfilePN;//这里是因为Copyfile函数对参数类型的要求
LfileP = stringToLPCWSTR(fileP);
LfilePN = stringToLPCWSTR(filePN);
//CopyFile(LfileP, LfilePN, FALSE);//C:\\Users\\hys\\Desktop\\新建文本文档 (2).txt C:\\b.txt
CopyFile(LfileP, LfilePN, FALSE);//复制文件
}
//这几个函数参考了一个博文,实在是忘了在哪了0..0
LPCWSTR stringToLPCWSTR(std::string orig)
{
size_t origsize = orig.length() + 1;
const size_t newsize = 1000;
size_t convertedChars = 0;
wchar_t* wcstring = (wchar_t*)malloc(sizeof(wchar_t) * (orig.length() - 1));
mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str(), _TRUNCATE);
return wcstring;
}
char* TCHARToChar(TCHAR* pTchar)
{
char* pChar = nullptr;
int nLen = wcslen(pTchar) + 1;
pChar = new char[nLen * 2];
WideCharToMultiByte(CP_ACP, 0, pTchar, nLen, pChar, 2 * nLen, NULL, NULL);
return pChar;
}
std::vector<std::string> split(std::string str, std::string pattern)
{
std::string::size_type pos;
std::vector<std::string> result;
str += pattern;//扩展字符串以方便操作
int size = str.size();
for (int i = 0; i < size; i++)
{
pos = str.find(pattern, i);
if (pos < size)
{
std::string s = str.substr(i, pos - i);
result.push_back(s);
i = pos + pattern.size() - 1;
}
}
return result;
}
使用unicode和多字节字符集都可以无警告运行。