Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hello Programmers,

I am converting my ascii C++ project to unicode.I am facing troubles with getline function.

Have a look on the code pasted below.

C++
// Ascii code:
 
         string OutputString;
	ifstream IniFile ("D:\\FileName.INI");
	
	while ( /*some condition*/ )
	{
		getline(IniFile, OutputString);
		
	}

// After conversion in unicode:

         twstring OutputString;
	ifstream IniFile (_T("D:\\FileName.INI"));
	
	while ( /*some condition*/ )
	{
		getline(IniFile, OutputString);
		
	}

When I try to build in unicode, it ends up with below error.

error: no instance of overloaded function "getline" matches the argument"

I understand that getline does not accept argument of twstring type. Based on that i have below questions.

Questions:
- Do we have unicode version of getline function?
- If not how to handle these situations?

Please help.

Regards,
Joy
Posted
Updated 1-Nov-21 3:24am
v3

I stumbled upon this question while trying to understand why I get gibberish text when I convert PDF files into text, as part of this [^] project. This question is still relevant today, and there is no real answer. An ideal answer would be the same code blocked which was included in the original question, but fixed. So I did some research...

First, you need to use imbue[^] in order to set the encoding of your choice.

Second, use wifstream [^] instead of ifstream[^].

The following code works with wstring[^].

Note that some Visual Studio default projects contain
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
If so, remove this block. See this answer about this Macro[^].

So here is the fixed code:

#include <locale>
wstring OutputString;
wifstream IniFile (_T("D:\\FileName.INI"));
IniFile.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t>));

while (true )
{
    getline(IniFile, OutputString);
}

Tested on my own project.
Before[^]:
After[^]:
 
Share this answer
 
 
Share this answer
 
Comments
Michael Haephrati 1-Nov-21 8:25am    
What was the actual solution for getting the line as UNICODE?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900