1

I want to create file having arabic name in C++. Below is the program that I tried.

#include <iostream>
#include <fstream>
#include <string>

int main() {

    const char *path="D:\\user\\c++\\files\\فثسف.txt";
    std::ofstream file(path); //open in constructor
    std::string data("Hello World");
    file << data;

    return 0;
}

But file gets created with junk characters: ÙØ«Ø³Ù.txt. I am using windows platform and g++ compiler.

Homam
  • 5,018
  • 4
  • 36
  • 39
  • Does it help using unicode escapes? – dan1st May 25 '19 at 05:41
  • What is the encoding of the `.cpp` source file? What IDE or editor are you using? – Nikos C. May 25 '19 at 05:54
  • I am using Visual Studio Code. Encoding of the file is UTF-8 – Homam May 25 '19 at 05:56
  • Are you able to simply print the name of the file? Related: https://stackoverflow.com/questions/12015571/how-to-print-unicode-character-in-c – Damien May 25 '19 at 05:57
  • Have you tried `const wchar_t *path` instead? Unfortunately, this is a bit of a pain in general. Also see: https://stackoverflow.com/questions/6975267/c-saving-file-with-unicode-name-problem-how-to-save-utf-8-filenames-correctl – Nikos C. May 25 '19 at 05:57
  • The compiler may or may not support embedding non-ascii in the source file -- try using unicode escape sequences instead (e.g. `\u00ab`). Also you should make the path be wide characters – M.M May 25 '19 at 06:19

2 Answers2

0

The default encoding for string literals can be specified with the -fexec-charset compiler option for gcc / g++.

In C++11 and later, can also use the u8, u, and U prefixes to strings to specify UTF8, UTF16, and UTF32 encodings:

const char * utf8literal = u8"This is an unicode UTF8 string! 剝Ц";
const char16_t * utf16literal = u"This is an unicode UTF16 string! 剝Ц";
const char32_t * utf32literal = U"This is an unicode UTF32 string! 剝Ц";

Using the above prefixes can upset some functions who aren't expecting these specific types of strings though; in general it may be better to set the compiler option.

There's a great writeup about this topic on this blog post: http://cppwhispers.blogspot.com/2012/11/unicode-and-your-application-3-of-n.html

I hope this helps. :)

catleeball
  • 781
  • 7
  • 16
0

Use UTF8:

#include <iostream>
#include <fstream>
#include <filesystem>

int main()
{
  namespace fs = std::filesystem;
  fs::path path { u8"فثسف.txt" };
  std::ofstream file { path };

  file << "Hello World";

  return 0;
}

Using <filesystem> library may require additional compiler/linker options. GNU implementation requires linking with -lstdc++fs and LLVM implementation requires linking with -lc++fs

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93