Hi every one, I come in need of big help. I have a file that I want to open but the problem is that the file can be in a UTF directory (path can be in a cyrilic or latin). So I did an extensive search, read and tried almost 10 stack solution but came out empty, at this point am really desperate.
Here is my exact problem:
I get with a path, for example:
čovećž/test_file.txt
The way I can open this is with _wfopen, but the problem with this is that this function takes wchar_t.
And it can work if I code the path with unicode:
wchar_t path[100] = _T("\u010d\u006f\u0076\u0065\u0107\u017e/test_file.txt");
Once I knew I needed a Unicoded wchar_t string I tried converting it.
Things I tried bellow:
I am asking anyone to help mi out with this, either convert the string to unicode or use some other function (not wfopen)!!
You can also use BOOST lib, I already got it set up.
Targeted platform is: Windows only!
I would like if somebody can code an example, because links to articles won't do much, because I think I read EVERYTHING that is on this topic. :(
So basically I need this:
https://www.branah.com/unicode-converter[
^]
Thank you in advance.
What I have tried:
Manual conversion:
I tried converting it manually, going through the string and changing chars with unicode codes. But in C/C++ most of the characters are the same for example ć = č = š. So this did not work.
Then I turned to stackoverflow:
I tried this function:
std::wstring s2ws(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
std::wstring stemp = s2ws(x);
LPCWSTR result = stemp.c_str();
Did not work, then someone submitted this:
#include <codecvt>
#include <iostream>
#include <iomanip>
#include <string>
int main() {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> convert;
std::string s = "test";
std::cout << std::hex << std::setfill('0');
std::cout << "Input `char` data: ";
for (char c : s) {
std::cout << std::setw(2) << static_cast<unsigned>(static_cast<unsigned char>(c)) << ' ';
}
std::cout << '\n';
std::wstring ws = convert.from_bytes(s);
std::cout << "Output `wchar_t` data: ";
for (wchar_t wc : ws) {
std::cout << std::setw(4) << static_cast<unsigned>(wc) << ' ';
}
std::cout << '\n';
}
But this only works for ASCII code, but then i put put a Unicode char (ć) it converts into
003f, but it should be
0107.
So at this point I started thinking it wasn't possible in C++, and I installed
BOOST and tried some things with wpath
boost::filesystem::wpath dirPath
And it did not work..