0

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:

  • 1
    The problem could be your terminal settings and functionality and not your code. – drescherjm Jan 14 '23 at 16:34
  • Does this answer your question? [Read Unicode UTF-8 file into wstring](https://stackoverflow.com/questions/4775437/read-unicode-utf-8-file-into-wstring) – JosefZ Jan 14 '23 at 17:39
  • If you are printing UTF-8 to the console. The the console has to support the character set you are printing. When I run your code it works perfectls in both wide character and if I convert to normal char it also works just fine. If I look at the environmnt in my console I see that my console is using UTF-8 `LANG="en_US.UTF-8"` – Martin York Jan 14 '23 at 18:06
  • Side Note (not relevant to the question): `Spanish` is not a local. It should probably be: `es` or `es_AR` but I am not an expert on locals. – Martin York Jan 14 '23 at 18:08

0 Answers0