Advertisement

C++ Getting unicode from an ini file

Started by July 16, 2017 02:08 AM
5 comments, last by User134 7 years, 4 months ago

Hi, this is my first post here and i hope to get an answer :)
My question is i am reading the section names from an file Unfortunately these section name contains unicode chars so when i read them i just get ???? 
here's my code ...
 


TCHAR buffer[4096];
int Lsize = sizeof(buffer),length;
GetPrivateProfileSectionNames(buffer,Lsize,".//Test.txt");
TCHAR *Get = buffer;
while (*Get){
length = strlen(Get);
Get += length;
Get++;
}

 

I'm pretty sure that strlen() doesn't work on unicode characters, You probably need wcslen() instead (assuming that actually is a multi-byte string)

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Advertisement

Also, is your project set to UNICODE? TCHAR will either be single byte char or WCHAR. It's probably better to explicitely go for the W versions of the APIs.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Okay guys the problem was that the ini file was UTF-8 and not unicode hhhhhhhh 
Anway i wanted to ask another question ...
Since the Get and buffer are not char and TCHAR 
So when i use MessageBox::Show(Get);
It will give me error so i have tried this :
String ^GetResult = gcnew String(Get);
MessageBox::Show(GetResult);
This one works but i don't know how this line works String ^GetResult = gcnew String(Get);
Can you please explain it ?

Ouch, you're using managed C++. That's one ugly beast and usually only used for interfacing :)

It's combining C++ with .NET, and therefore has a few warts to transfer data between runtimes. Any reason why you're using this?

 

^ is a managed reference (somewhat like a * pointer in C/C++).
gcnew is the new of C++, creating a managed reference.
String( Get) is the constructor of the .NET String class which lets you pass in a char array.

So all you do in that line is creating a new managed instance of String, initialised with your char array.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Becouse i am using C++/CLR so gcnew is like new and ^ is like * and String is like string

This topic is closed to new replies.

Advertisement