How to read in Hebrew letters in Visual Studios C++ - c++

I'm trying to read in a Hebrew text file, do some modifications, then send it to another text file. I've been successful in displaying Hebrew letters using UTF8, but I can't seem to read them in. This code successfully prints out the Hebrew letters to the txt file that it was redirected to, but when I try to read in Hebrew from another text file (that was redirected in) I get random garbage. How do I fix this?
#include <iostream>
#include <string>
#include <fcntl.h>
#include <io.h>
#include <fstream>
using namespace std;
int main() {
_setmode(_fileno(stdout), _O_U8TEXT);
wprintf(L"\x05D0");//works with courier new in terminal
wchar_t example[] = L"א";
wcout << endl << example << endl;
wstring x;
getline(wcin, x);
wcout << x;
return 0;
}
Output
א
א
×××× ×©××ת ×× × ×שר×× ××××× ×צר××× ×ת ××¢×§× ××ש ××××ª× ××× â¬

The problem has been figured out. It was what Barmak Shemirani said almost. I put in _setmode(_fileno(stdin), _O_U16TEXT); and changed my output to U16 and then still got garbage then I changed them both to U8 and I was able to read in and out perfectly.

Related

I can't output cyrillic text on the console (it suddenly stopped working)

I have been writing code like this to display cyrillic text on the console and it has always worked, but it suddenly stopped working for some reason, I don't understand why. What's the problem?
This is on Visual Studio, Windows
#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
using namespace std;
int main()
{
setlocale(LC_ALL, "Russian");
//SetConsoleCP(1251);
//SetConsoleOutputCP(1251);
ifstream input{ "in_text.txt" };
if (!input) {
cerr << "Error opening file" << endl;
return 1;
}
cout << "Displaying file contents: " << "\n\n";
string line{};
while (getline(input, line))
cout << line << endl;
input.close();
return 0;
}
I've previously been using setlocale, I've also now tried the windows SetConsoleCP nothing is working. This is the output everytime:
Displaying file contents:
Р?С?РёР?РчС'
Also, if there is a better way to output cyrillic text on the console, please let me know.
Windows OS sometimes behave strangely and unpredictable when it comes to cyrillic in console
You can try SetConsoleOutputCP(CP_UTF8);
it helped me once(<windows.h> header required)

C++ setlocale for any region

I've written a little program for testing purposes because when using cout, the German letters ü ö ä ß were not displayed as they should but rather rubbish was given out on the console. However, using these lines
#include <iostream>
#include <locale>
using namespace std;
int main()
{
setlocale(LC_ALL, "German");
cout << "üüü ööö äää ßßß" << '\n';
system("pause");
return 0;
}
have solved this problem, at least as far as the German letters go. When I tried the same for Russian, i. e.
#include <iostream>
#include <locale>
using namespace std;
int main()
{
setlocale(LC_ALL, "RUSSIAN");
cout << "Кирилица" << '\n';
system("pause");
return 0;
}
this doesn't work anymore. What is going wrong here? Am I missing something about the setlocale function? My goal is to adapt the respective program to the writing system that is used, for example Cyrillic like aboe or Chinese or whatever.
FOR GERMAN -> std::setlocale(LC_ALL, "de_DE");
FOR RUSSIAN -> std::setlocale(LC_ALL, "rus");

getline() not reading first lines

I am c++ beginner and this is for school..
I am trying to read a file about 28kb big. The program works but it doesnt print the first 41 lines. It works fine with a smaller file.
At first i was reading into a char array and switch it to strings.
i also tried changing the log buffer but it apparently it should be big enough..
I feel like this should be very simple, but just cant figure it out..
Any help will be greatly apreciated..
Thanks!
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstdio>
#include <cerrno>
using namespace std;
struct espion
{
char nom[30];
char pays[20];
char emploi[29];
};
int main()
{
const int MAX_NOM = 30, MAX_PAYS = 20, MAX_EMPLOI = 29;
char nomFichier[50] = "espion.txt";
ifstream aLire;
aLire.open(nomFichier, ios::in|ios::binary);
if(!aLire.is_open()){
exit(EXIT_FAILURE);
}
std::string infoEspion;
while(aLire)
{
infoEspion.clear();
std::getline(aLire, infoEspion);
cout << infoEspion ;
}
aLire.close();
system("pause");
return 0;
}
From the system("pause"), it looks like you're running on Windows. With ios::binary, the end-of-line marker is not translated, and the cout << infoEspion; statement prints these "raw" lines in such a way that all of the lines are written on top of each other. (More specifically, each line will end with a return but no newline, so the cursor goes back to the start of the same line after executing each cout statement.) If you take out the ios::binary, you will echo all of the input on a single, very long line. Changing the statement to cout << infoEspion << endl; will echo all of the lines.

ASCII characters not printed in WCOUT and COUT

I am trying to print some "special" characters (above 127) in the console, but somehow it doesn't get printed.
I have this small code snippet:
#include <iostream>
int main()
{
std::wcout << L"a■■■■■■■■■■■a■■■■■■■■■■■■■■a" << std::flush;
return 0;
}
it prints the 'a' but then... nothing. and it doesn't matter if I use cout/string or wcout/wstring. (with cout I only see "?" and in wcout nothing, it ends the stream)
The ascii code is 254 for this character. What can be happening here? I thought this is okay to print?
From the answer I linked to in the comments, I think this is your solution:
#include <fcntl.h>
#include <io.h>
_setmode(_fileno(stdout), _O_U8TEXT);

Unable to write a std::wstring into wofstream

I'm using Qt/C++ on a Linux system. I need to convert a QLineEdit's text to std::wstring and write it into a std::wofstream. It works correctly for ascii strings, but when I enter any other character (Arabic or Uzbek) there is nothing written in the file. (size of file is 0 bytes).
this is my code:
wofstream customersFile;
customersFile.open("./customers.txt");
std::wstring ws = lne_address_customer->text().toStdWString();
customersFile << ws << ws.length() << std::endl;
Output for John Smith entered in the line edit is John Smith10. but for unicode strings, nothing.
First I thought that is a problem with QString::toStdWString(), but customersFile << ws.length(); writes correct length of all strings. So I guess I'm doing something wrong wrong with writing wstring in file. [?]
EDIT:
I write it again in eclipse. and compiled it with g++4.5. result is same:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
cout << "" << endl; // prints
wstring ws = L"سلام"; // this is an Arabic "Hello"
wofstream wf("new.txt");
if (!wf.bad())
wf << ws;
else
cerr << "some problem";
return 0;
}
Add
#include <locale>
and at the start of main,
std::locale::global(std::locale(""));