Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ read Arabic text from file

In C++, I have a text file that contains Arabic text like:

شكلك بتعرف تقرأ عربي يا ابن الذين

and I want to parse each line of this file into a string and use string functions on it (like substr, length, at...etc.) then print some parts of it to an output file.

I tried doing it but it prints some garbage characters like "\'c7\'e1\'de\'d1\" Is there any library to support Arabic characters?

edit: just adding the code:

#include <iostream>
#include <fstream>
using namespace std;
int main(){
  ifstream ip;
  ip.open("d.rtf");
  if(ip.is_open() != true){
    cout<<"open failed"<<endl;
    return 0;
  }
  string l;
  while(!ip.eof()){
    getline(ip, l);
    cout<<l<<endl;
  }

  return 0;
}

Note: I still need to add some processing code like

if(l == "كلام بالعربي"){
    string s = l.substr(0, 4);       
    cout<<s<<" is what you are looking for"<<endl;
 }
like image 764
CSawy Avatar asked May 26 '14 14:05

CSawy


1 Answers

You need to find out which text encoding the file is using. For example, to read an UTF-8 file as a wchar_t you can (C++11):

std::wifstream fin("text.txt");
fin.imbue(std::locale("en_US.UTF-8"));
std::wstring line;
std::getline(fin, line);
std::wcout << line << std::endl;
like image 172
vz0 Avatar answered Oct 30 '22 01:10

vz0