Ok Thanks to God its solved and here is the final code which runs just fine on UTF-8 contents such as farsi
Code:
#include "stdafx.h"
#include <iostream>
#include <regex>
#include <fstream>
#include <string>
#include <map>
#include <fcntl.h> // for _wfopen_s
#include <io.h> //for _setmode
using namespace std;
int main()
{
string path = "";
map<wstring, int> container;
FILE* fp;
_wfopen_s (&fp, L"ftest.txt", L"r");
_setmode (_fileno (fp), _O_U8TEXT);
wifstream file(fp);
wregex reg(L"\\w+");
wstring s = L"";
while (file.good())
{
getline(file,s);
for ( wsregex_iterator it (s.begin(), s.end(), reg), it_end ; it != it_end ; ++it)
{
container[(wstring)(*it)[0]]++ ;
}
}
cout <<"\nDone..."<< endl;
fclose(fp);
_wfopen_s (&fp, L"list.txt", L"w");
_setmode (_fileno (fp), _O_U8TEXT);
wofstream output(fp);
for (auto item : container)
{
wcout<<item.first <<" : "<<item.second <<endl;
//write output to list.txt
output<<item.first <<" : "<<item.second <<endl;
}
fclose(fp);
system("pause");
return 0;
}
for a portable solution check this link out.