c++ string UTF-8 encoding [duplicate] - c++

This question already has answers here:
How do I print UTF-8 from c++ console application on Windows
(8 answers)
Closed 9 years ago.
I'm new in c++, and I tried to write a very simple code, but the result is wrong, and I don't know how to fix it.
The code is:
#include <iostream>
#include <string>
using namespace std;
int main() {
string test_string = "aáeéöôőüűč♥♦♣♠";
cout << test_string << endl;
return 0;
}
But the result is: a├íe├ę├Â├┤┼Ĺ├╝┼▒─ŹÔÖąÔÖŽÔÖúÔÖá
I am on Windows, using Code::Blocks.

Save file as UTF-8 without BOM signature, and try use printf().
//Save As UTF8 without BOM signature
#include <stdio.h>
#include <windows.h>
int main() {
SetConsoleOutputCP(65001);
char test_string[] = "aáeéöôőüűč♥♦♣♠";
printf(test_string);
return 0;
}
And the result is: aáeéöôőüűč♥♦♣♠

Unfortunately working with UTF-8 on Windows is very problematic.
On Linux, you can simply wstring like this:
Does this code work universaly, or is it just my system?
But unfortunately Windows doesn't have an UTF-8 locale, so you are left with Windows API.
http://www.siao2.com/2007/01/03/1392379.aspx

Related

is there an easy way to get output of system commands into a string? [duplicate]

This question already has answers here:
c++: subprocess output to stdin
(2 answers)
popen equivalent in c++
(3 answers)
Closed 6 years ago.
Is there an easy way to get output of system commands into a string in C++?
Heres and example of what I mean, trying to get the epoch into a string.
It doesnt work of course.
#include <stdlib.h>
#include <iostream>
#include <string.h>
using namespace std;
int main(){
string t = system("date +%s");
cout << "Time " << t << endl;
return 0;
}
For that specific task you probably want to use time and ctime (or something similar).
For the more general case, see popen (or, on Microsoft compilers, _popen). This doesn't return a string directly; it returns a FILE *, which you can then read like you would a file.

c++ Unable to output unicode characters even though I can write them directly

So thing is I can copy paste unicode characters like chess pieces directly to terminal( I'm using debian jessie linux) but whenever I write c++ code to do that, I get these � instead
here is my code
enter code here
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
int main()
{
setlocale(LC_ALL,"");
wchar_t piece='♗';
wcout<<piece;
}
I tried to use the hex or decimal code of the characters but it does not work
I also use vim to edit and it does show the characters while I'm typing.
There's no specification of what encoding should be used for wchar_t. I need to use mbstowcs function to convert that character. Like this, for example:
#include <iostream>
#include <clocale>
#include <cstdlib>
using namespace std;
int main(void) {
setlocale(LC_ALL, "");
wchar_t piece;
mbstowcs(&piece, "♗", 1);
wcout << piece << endl;
return 0;
}
assuming your source file encoding matches the encoding of your locale.
Oddly enough what worked was going at it normally and putting the special character into a string it's so ridiculously simple I didn't even think to use it.
#include<iostream>
using namespace std;
int main()
{
string piece="♗";
cout<<piece;
}

Trying to print chess pieces in Visual Studio 2013 [duplicate]

This question already has answers here:
Unicode character Visual C++
(2 answers)
Closed 8 years ago.
I am trying to print a "pawn" while using visual studio and it doesn't recognize the unicode.
can anyone tell me how to fix it?
this is a basic example:
#include <iostream>
using namespace std;
int main()
{
cout << "\33[37;42m\u2659\33[0m";
}
and the output i get is:
"<-[37;42m?<-[0m".
The ordinary Windows console windows do not support ANSI escape sequences.
To display general Unicode characters you can
use the Windows console functions instead, or
set up standard data streams to Unicode (Microsoft extension, see _setmode) and use wide streams, or
e.g. display a text file with the desired text, e.g. encoded in UCS-2.
Example 1: Using the Windows console functions.
Code:
#undef UNICODE
#define UNICODE
#include <windows.h>
#include <string>
namespace console {
using std::wstring;
const HANDLE output = GetStdHandle( STD_OUTPUT_HANDLE );
void write( const wstring& text )
{
if( text.length() == 0 ) { return; }
DWORD n_chars_written = 0;
WriteConsole( output, &text[0], text.length(), &n_chars_written, 0 );
}
}
auto main() -> int
{
console::write( L"Oh look, a \u2659!\n" );
}
Running this in the ordinary Windows console will most likely produce a square instead of a pawn symbol, even in Windows 8.x with e.g. Lucida Console font. That’s because the console window implementation simply does not support presentation of such characters. The character is correctly stored, and you can copy it out and e.g. present it in Windows Write (standard accessory program), but that’s a tad impractical, shall we say, for the ordinary end user.
A simple solution is to require use of e.g. the Console console window replacement.
Then you get nifty tabs, too. ;-)
H:\dev\test\so\0208>g++ using_console_api.cpp
H:\dev\test\so\0208>a
Oh look, a ♙!
H:\dev\test\so\0208>_
Example 2: using wide streams.
Code:
#include <assert.h>
#include <iostream>
#include <fcntl.h> // _O_WTEXT
#include <io.h> // _setmode, _isatty
namespace console {
struct Usage
{
Usage()
{
const int out_fileno = _fileno( stdout );
assert( _isatty( out_fileno ) );
_setmode( out_fileno, _O_WTEXT );
}
};
} // console
const console::Usage console_usage;
auto main() -> int
{
using namespace std;
wcout << L"Oh look, a \u2659!" << endl;
}
I recall that at one time, probably with the original MinGW g++, one had to define some special preprocessor symbol to get the Microsoft library extensions defined. However, this compiled nicely as-is with MinGW g++ 4.9.1 from the Nuwen distribution. And of course, it also compiles nicely with Visual C++.

Getline Function Messing with Code

Picking up C++ and having a go at it on OS X 10.9 using XCode 5.0.2 and using Alex Allain as reference.
The following code compiles just fine and outputs correctly
#include <iostream>
#include <string>
using namespace std;
int main()
{
std::string user_first_name = "test";
std::cout << user_first_name << "\n";
return 0;
}
When I add a getline function, code appears to compile but with no output.
#include <iostream>
#include <string>
using namespace std;
int main()
{
std::string user_first_name = "test";
std::getline( std::cin, user_first_name, '\n' );
std::cout << user_first_name << "\n";
return 0;
}
In fact debug navigator has memory filling up with bars (although actual memory use is fixed at 276 KB). Why am I getting stumped on such a simple thing/concept?
I did a bit of digging around and its quite likely this is related to a text encoding issue. I'm using defaults which is Unicode (UTF-8). Encoding is not something I'm familiar with, never something I had to deal with when learning on Windows. How do I get past this?
I can't comment regarding the use of XCode or OS X, but it was my understanding that std::cin always gives you a narrow (single-byte) character stream. In Windows (at least with Visual Studio), I think it works whether you compile for UTF8 (single-byte for all ASCII characters) or UTF16 (2-bytes for all ASCII characters). The runtime library presumably does the conversion for you as necessary.
I'm not sure what "filling up with bars" means, but maybe it's just that you're looking at uninitialized memory. If you think that it is an encoding issue, perhaps try using wstring/wcin instead of string/cin and see if that helps.

How to use C++11 std::stoi with gcc? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert a number to string and vice versa in C++
I am using Qt Creator 2.5.0 and gcc 4.7 (Debian 4.7.2-4). I added "QMAKE_CXXFLAGS += -std=c++11" to .pro file. Everything seems to be OK, I used C++11 std::for_each and so on. But when I included "string" header and wanted to use stoi, i got the following error:
performer.cpp:336: error: 'std::string' has no member named 'stoi'
I found some questions related to MinGW and one more, to Eclipse CDT and they had their answers. But I use Linux, why it is NOT working here?
#include <iostream>
#include <string>
int main()
{
std::string test = "45";
int myint = stoi(test);
std::cout << myint << '\n';
}
or
#include <iostream>
#include <string>
using namespace std
int main()
{
string test = "45";
int myint = stoi(test);
cout << myint << '\n';
}
look at http://en.cppreference.com/w/cpp/string/basic_string/stol
std::stoi is a function at namespace scope, taking a string as its argument:
std::string s = "123";
int i = std::stoi(s);
From the error message, it looks like you expect it to be a member of string, invoked as s.stoi() (or perhaps std::string::stoi(s)); that is not the case. If that's not the problem, then please post the problematic code so we don't need to guess what's wrong with it.