2013-05-02 2 views
4
char* stheParameterFileName = argv[1]; //I'm passing the file name as a parameter. 
TCHAR szName [512]; 

char*TCHAR []으로 변환하려면 어떻게해야합니까?char *을 TCHAR []로 변환하는 방법?

+2

'_tmain (int argc, TCHAR * argv [])'즉 Visual Studio에서 처음으로 프로젝트를 처음 만들 때 처음으로 설치하는 방식을 사용하지 않는 특별한 이유가 있습니까? – WhozCraig

+0

TCHAR szName [512]; hMapFile =하여 CreateFileMapping ( INVALID_HANDLE_VALUE, // 사용 페이징 파일 NULL, // 기본 보안 PAGE_READWRITE은, // 읽기/쓰기 액세스 0, 의 buf_size, szName); // 매핑 객체의 이름 그래서, 이미 가지고있는 파일 이름을 전달하려고하지만, char * 타입의 szName으로 넘겨 주길 원합니다. –

+1

2의 힘으로 마술처럼 버퍼를 만들 수 있습니다. 스택을 넘치지 않으면 서 충분히 큰가? 즉, CHAR 또는 WCHAR을 명시 적으로 사용하려면 * A 및 * W 함수가 있습니다. –

답변

10

당신이 헤더 파일을 포함하는 경우 :

#include "atlstr.h" 

그런 다음 아래로 A2T 매크로를 사용할 수 있습니다

// You'd need this line if using earlier versions of ATL/Visual Studio 
// USES_CONVERSION; 

char* stheParameterFileName = argv[1]; 
TCHAR szName [512]; 
_tcscpy(szName, A2T(stheParameterFileName)); 
MessageBox(NULL, szName, szName, MB_OK); 

Details on MSDN

+0

@ John Sibly :이 방법이 효과적이었습니다. 감사!! –

+0

그래,이게 효과가 있지만 앱이 경로에서 유니 코드 문자를 처리 할 수 ​​없음을 의미합니다. 보다 나은 해결책은 WhozCraig가 제안한 것을 수행하고 진입 점을 올바르게 프로토 타입하거나'GetCommandLine' 함수의 결과를'CommandLineToArgv' 함수에 전달하여 직접 배열을 생성하는 것입니다. –

+4

(VS 2013, C++ 사용) 오류 오류 C2065 : '_lpa':이 줄에 대해 선언되지 않은 식별자 \t : _tcscpy (szName, A2T (stheParameterFileName)); – qqqqq

0

양식 MSDN : 정의는 자주 TCHAR는 유니 코드 또는 ANSI를 사용하는지에 따라됩니다

// convert_from_char.cpp 
// compile with: /clr /link comsuppw.lib 

#include <iostream> 
#include <stdlib.h> 
#include <string> 

#include "atlbase.h" 
#include "atlstr.h" 
#include "comutil.h" 

using namespace std; 
using namespace System; 

int main() 
{  
// Create and display a C style string, and then use it 
// to create different kinds of strings. 
char *orig = "Hello, World!"; 
cout << orig << " (char *)" << endl; 

// newsize describes the length of the 
// wchar_t string called wcstring in terms of the number 
// of wide characters, not the number of bytes. 
size_t newsize = strlen(orig) + 1; 

// The following creates a buffer large enough to contain 
// the exact number of characters in the original string 
// in the new format. If you want to add more characters 
// to the end of the string, increase the value of newsize 
// to increase the size of the buffer. 
wchar_t * wcstring = new wchar_t[newsize]; 

// Convert char* string to a wchar_t* string. 
size_t convertedChars = 0; 
mbstowcs_s(&convertedChars, wcstring, newsize, orig, _TRUNCATE); 
// Display the result and indicate the type of string that it is. 
wcout << wcstring << _T(" (wchar_t *)") << endl; 
... 
} 

.

here 참조 :

Tchar.h를 사용하여 같은 소스에서 단일 바이트, 멀티 바이트 문자 세트 (MBCS) 및 유니 코드 응용 프로그램을 구축 할 수 있습니다.
Tchar.h는 올바른 선행 처리기 정의로 str, _mbs 또는 wcs 함수에 적절하게 매핑되는 매크로 (접두사가 _tcs 인 매크로)를 정의합니다. MBCS를 빌드하려면 _MBCS 기호를 정의하십시오. 유니 코드를 빌드하려면 _UNICODE 기호를 정의하십시오. 싱글 Y이트 응용 프로그램을 빌드하려면 어느 것도 정의하지 마십시오 (기본값).
기본적으로 _MBCS는 MFC 응용 프로그램에 대해 정의됩니다. _TCHAR 데이터 형식은 조건부로 Tchar.h에 정의됩니다. 빌드에 심볼 _UNICODE이 정의되어있는 경우 은 wchar_t;으로 정의되고 그렇지 않으면 1 바이트 및 MBCS 빌드의 경우 char로 정의됩니다. (기본 Unicode 와이드 문자 데이터 유형 인 wchar_t는 8 비트 부호가있는 char에 대한 16 비트 카운터입니다.) 국제적인 응용 프로그램의 경우, _TCSAR 계열 (바이트가 아닌)으로 작동하는 _tcs 함수 제품군을 사용하십시오. 예를 들어 _tcsncpy는 n 바이트가 아닌 n _TCHAR을 복사합니다.

0

프로젝트가 유니 코드를 사용하도록 설정 될 수 있습니다. 유니 코드는 지구상에서 대부분의 언어를 처리하려는 프로그램 용입니다. 이 기능이 필요하지 않은 경우 프로젝트 속성/일반/문자 집합으로 이동하여 유니 코드에서 멀티 바이트로 전환하십시오.

관련 문제