c++ . Reading txt and outputting by console using Spanish letters - c++

I have a problem reading a txt file and getting it out of the console. I need to use Spanish letters. The txt file is encoded in "utf-8 with BOM"
This is text of the texInput.txt file:
Hello. Look how it shows the letter " ñ ",
and the letters with tilde: "á é í or ú"
I want to process texts in Spanish.
Please help.
Here the main.cpp code:
#include <fstream>
#include <iostream>
#include <string>
#include <locale.h>
using namespace std;
wstring inputStr;
wchar_t wc;
wifstream te;
void openTxt();
void readTxt();
void closeTxt();
void wait();
int main(){
setlocale(LC_ALL,"Spanish");
openTxt();
readTxt();
closeTxt();
wait();
return 0;
}
void openTxt(){
te.open("texInput.txt",ios::in);
if (te.fail()){
cout<<"No se pudo abrir el archivo de texto";
exit(1);
}
}
void readTxt(){
while(te.good()){
te.get(wc);
inputStr.push_back(wc);
}
wcout<<inputStr<<endl;
}
void closeTxt(){
te.close();
}
void wait(){
wcout<<L"type something to exit: "<<endl;
wcin>>wc;
}
This is what it looks like on the console:
Hello. Look how it shows the letter " ñ ",
and the letters with tilde: "á é í or ú"
I want to process texts in Spanish.
Please help.
type something to exit:

Related

I can't use accents with string in C++

I can set the accents with SetConsoleOutputCP(1252) or locale::global(locale"FR-fr"), but I can't use it with strings. Seems like it's one or the other. I can output text with accents, or I can output the string with accents, not both. Any ideas?
The code below can be used to reproduce the problem. Simply add locale::global(locale"FR-fr") or SetConsoleOutputCP(1252):
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
int main()
{
string mot;
cout << "Inscrivez un mot avec des accents (é ou è): ";
cin >> mot;
cout << mot;
}
I suggest that you set both the input and output code pages:
SetConsoleCP(1252); // input
SetConsoleOutputCP(1252); // output

fstream not working properly with russian text?

I work with russian a lot and I've been trying to get data from a file with an input stream. Here's the code, it's supposed to output only the words that contain no more than 5 characters.
#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
using namespace std;
int main()
{
setlocale(LC_ALL, "ru_ru.utf8");
ifstream input{ "in_text.txt" };
if (!input) {
cerr << "Ошибка при открытии файла" << endl;
return 1;
}
cout << "Вывод содержимого файла: " << "\n\n";
string line{};
while (input >> line) {
if (line.size() <= 5)
cout << line << endl;
}
cout << endl;
input.close();
return 0;
}
Here's the problem:
I noticed the output didn't pick up all of the words that were actually containing less than 5 characters. So I did a simple test with the word "Test" in english and the translation "тест" in russian, the same number of characters. So my text file would look like this:
Test тест
I used to debugger to see how the program would run and it printed out the english word and left the russian. I can't understand why this is happening.
P.S. When I changed the code to if (line.size() <= 8) it printed out both of them. Very odd
I think I messed up my system locale somehow I don't know. I did one time try to use std::locale
without really understanding it, maybe that did something to my PC I'm not really sure. Please help
I'm very unsure about this but using codecvt_utf8 and wstring_convert seems to work:
#include <codecvt> // codecvt_utf8
#include <string>
#include <iostream>
#include <locale> // std::wstring_convert
int main() {
// ...
while (input >> line) {
// convert the utf8 encoded `line` to utf32 encoding:
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> u8_to_u32;
std::u32string u32s = u8_to_u32.from_bytes(line);
if (u32s.size() <= 5) // check the utf32 length
std::cout << line << '\n'; // but print the utf8 encoded string
}
// ...
}
Demo

Is there a simple way to print out special characters from a file to console in c++?

I have a problem where I am attempting to add appropriate articles to the beginning of French country names. That part wasn't an issue but trying to print it out hasn't worked so well. There are special characters in the file and while it seems to read them just fine it won't print them out just fine. I am using visual studios and here are examples of words from the text file named "FrenchCountriesUnfinished.txt" that I am trying to print out
l'Algérie,
la Norvège,
la Zélande,
la Suède,
le Zaïre
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream file("FrenchCountriesUnfinished.txt");
string str;
while (getline(file, str)) {
cout << str << endl;
}
return 0;
}

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.