在不同于Windows的平台上,您可以轻松使用char *字符串并将其视为UTF-8.
问题是在Windows上,您需要使用wchar*字符串(W)接受和发送消息.如果您将使用ANSI函数(A),则不支持Unicode.
因此,如果您想编写真正的可移植应用程序,则需要在Windows上将其编译为Unicode.
现在,为了保持代码清洁,我想看看处理字符串的推荐方法是什么,这种方法可以最大限度地减少代码中的丑陋.
字符串的类型可能需要:std::string,std::wstring,std::tstring,char *,wchat_t *,TCHAR*,CString(ATL一个).
您可能遇到的问题:
cout/cerr/cin 和他们的Unicode变种 wcout,wcerr,wcinstrcmp,wcscmp和_tcscmp._T()宏填充代码.您认为哪种方法最好?(欢迎举例)
就个人而言,我会采取一种std::tstring方法,但我想看看如何对他们必要的转换做些什么.
Windows标头tchar.h中定义的两个符号TCHAR和_TCHAR类型之间有什么不同?用例子解释.简要描述在代码中使用TCHAR而不是_TCHAR的情况.(10分)
我是C++编码的新手,来自Java和C#背景.我对最基本的#define术语的扩散感到困惑:
#define _tmain wmain
Run Code Online (Sandbox Code Playgroud)
当我第一次学习C语言时,我的主要功能是:
int main(int argc, char *argv[])
Run Code Online (Sandbox Code Playgroud)
在我创建的Visual C++项目中,它创建了主要功能:
int _tmain(int argc, _TCHAR* argv[])
Run Code Online (Sandbox Code Playgroud)
我只是想知道为什么要起个名翻译需要wmain来_tmain?为什么不使用原始的C main函数原型?
一般来说,似乎有很多#define重命名的东西,看起来很清楚,看起来更神秘,更不清楚(我的意思wmain是_tmain?).
感谢您容忍可能是一个非常明显的问题.
当您使用Windows上开发应用TCHAR的支持,%s在_tprintf()手段char *建立串ANSI和wchar_t *为Unicode版本,同时%S意味着相反.
但是有没有格式说明符总是意味着char *字符串无论是Ansi还是Unicode构建?因为即使在Windows上,UTF-16并不真正用于文件或网络,但事实上你仍然需要处理基于字节的字符串,无论你编译应用程序的本地字符类型如何.
我被要求为现有的旧项目添加功能,但我无法构建它.它处理unicode字符串但我在使用TCHAR时遇到很多错误.具体而言,几乎每个错误都是TCHAR无法转换为或用作wchar_t.从我在许多不同的文章中看到我尝试使用#define _UNICODE或#define UNICODE,但没有一个解决了这个问题.
这是一段代码:
#include <windows.h>
#include <wininet.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
#include <strsafe.h>
#include <string>
#include <list>
#include <cctype>
#include <winnt.h>
#include <atlconv.h>
#pragma comment(lib,"wininet.lib")
using namespace std;
TCHAR *tags[] = { _T("aa"), _T("bb"), _T("cc"),
NULL };
int _tmain(int argc, _TCHAR* argv[])
{
int i = 0;
for (i = 1; i<argc; i++) {
if (wcscmp(argv[i], _T("-h")) == 0) {
...
}
else if (wcscmp(argv[i], _T("-f")) == 0) {
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
在上面的行中,例如,当使用wcscmp时,我得到了
argument of type …Run Code Online (Sandbox Code Playgroud) 我正在尝试结合2 tchar.
char username[UNLEN+1];
DWORD username_len = UNLEN+1;
GetUserName(username, &username_len);
TCHAR* appdatapath ="C:\\Users\\"+username+"\\AppData";
Run Code Online (Sandbox Code Playgroud)
但我在appdatapath行收到错误错误.我如何结合2 tchar?谢谢
我有一个TCHAR,价值如下:
TCHAR szDestPathRoot[MAX_PATH]="String This";
Run Code Online (Sandbox Code Playgroud)
现在我想要来自TCHAR的前三个角色,如下所示:
szDestPathRoot.substring(0,2);
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点.
我正在尝试在Windows上使用C++编写一个简单的OpenCV示例,而我的C++不仅仅是生锈的.
样本很简单:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], IMREAD_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE ); // …Run Code Online (Sandbox Code Playgroud) 我在行上遇到编译错误:
MessageBox(e.getAllExceptionStr().c_str(), _T("Error initializing the sound player"));
Error 4 error C2664: 'CWnd::MessageBoxA' : cannot convert parameter 1 from 'const wchar_t *' to 'LPCTSTR' c:\users\daniel\documents\visual studio 2012\projects\mytest1\mytest1\main1.cpp 141 1 MyTest1
Run Code Online (Sandbox Code Playgroud)
我不知道如何解决这个错误,我尝试了以下方法:
MessageBox((wchar_t *)(e.getAllExceptionStr().c_str()), _T("Error initializing the sound player"));
MessageBox(_T(e.getAllExceptionStr().c_str()), _T("Error initializing the sound player"));
Run Code Online (Sandbox Code Playgroud)
我正在使用"使用多字节字符集"设置,我不想更改它.
如何将 a 拆分TCHAR为其他变量?例子:
TCHAR comando[50], arg1[50], arg2[50];
Mensagem msg;
_tcscpy(msg.texto, TEXT("MOVE 10 12"));
Run Code Online (Sandbox Code Playgroud)
因此,msg.texto有字符串“MOVE 10 12”,我希望变量comando[50]为“MOVE”,变量arg1为“10”,变量arg2为“12”。我怎样才能做到这一点?抱歉有些可能的英语错误。提前致谢!
解决了:
TCHAR *pch;
pch = _wcstok(msg.texto, " ");
_tcscpy(comando, pch);
pch = _wcstok(NULL, " ");
_tcscpy(arg1, pch);
pch = _wcstok(NULL, " ");
_tcscpy(arg2, pch);
Run Code Online (Sandbox Code Playgroud)