首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法将“std::__cxx11::string {aka std::__cxx11::basic_string<char>}”转换为“LPCSTR {aka *}”

无法将“std::__cxx11::string {aka std::__cxx11::basic_string<char>}”转换为“LPCSTR {aka *}”
EN

Stack Overflow用户
提问于 2021-08-19 07:43:04
回答 1查看 612关注 0票数 0
代码语言:javascript
复制
#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)之前;”

有办法解决这个问题吗??

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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来修复此问题,如下所示。

代码语言:javascript
复制
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());
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68843847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档