#include <iostream>
#include <string>
#include <sstream>
#include <windows.h>
#include <vector>
using namespace std;
int main() {
string PNGFilePath, WEBPFilePath;
int number, c;
char title[256];
cout << "Enter a Number: ";
cin >> number;
cout << endl;
cout << "Title: ";
cin.getline(title, 256, ';');
cout << "Enter PNG directory: ";
cin >> PNGFilePath;
cout << endl;
cout << "Enter WEBP directory: ";
cin >> WEBPFilePath;
cout << endl;
std::string OldPNGFolder = std::string(PNGFilePath + "\\");
c = 1;
while (title[c] != '\0') {
OldPNGFolder += title[c];
c++;
}
std::string NewPNGFolder = std::string(PNGFilePath + "\\[");
c = 1;
NewPNGFolder += to_string(number);
NewPNGFolder += "]";
while (title[c] != '\0') {
NewPNGFolder += title[c];
c++;
}
MoveFile(OldPNGFolder, NewPNGFolder);
}我尝试添加"(OldPNGFolder.c_str()“),它仍然显示相同的错误消息,也显示了系统(OldPNGFolder.c_str());仍然显示相同的消息。
添加"LPCTSTR“显示错误" error :预期的主-表达式在'OldPNGFolder‘MoveFile(LPCTSTR OldPNGFolder,NewPNGFolder)之前;”
有办法解决这个问题吗??
发布于 2021-08-19 07:56:43
您使用吗?
如果项目的Character Set是项目属性窗口中的Use Unicode Character Set,则MoveFile表示该MoveFileW。
它的参数类型是LPCTSTR,即const wchar_t *,而不是const char *。
您的错误不是从string转换为const *,而是在MoveFile中的参数类型错误。
您可以通过使用MoveFileA、MoveFileA(OldPNGFolder.c_str(), NewPNGFolder.c_str());或将字符串转换为wstring或LPCWSTR来修复此问题,如下所示。
wstring a2w(std::string & string_a)
{
int length = MultiByteToWideChar(CP_UTF8, 0, string_a.c_str(), -1, NULL, 0);
wchar_t* temp = new wchar_t[length];
MultiByteToWideChar(CP_UTF8, 0, string_a.c_str(), -1, temp, length);
wstring string_w = temp;
delete[] temp;
return string_w;
}
int main() {
...
MoveFile(a2w(OldPNGFolder).c_str(), a2w(NewPNGFolder).c_str());
}https://stackoverflow.com/questions/68843847
复制相似问题