Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Chuyển đổi std::string sang LPCSTR trong C++


LPCSTR (Long Pointer to Constant String) là chuỗi kiểu C. Chuyển std::string sang LPCSTR bằng c_str() và strcpy. Cần Windows.h.

Ví dụ

#include <iostream>
#include <Windows.h>
using namespace std;

int main() {
    string my_str = "Hello World";
    LPTSTR long_string = new TCHAR[my_str.size() + 1];
    strcpy(long_string, my_str.c_str());
    cout << "my_str is : " << my_str << endl;
    cout << "Long String is : " << long_string << endl;
    delete[] long_string;
    return 0;
}

Kết quả

my_str is : Hello World
Long String is : Hello World