5

I'm writing a program which processes some data, outputs it to a .csv file, then writes a GNUplot script, and calls GNUplot to execute the script and create an image file all with the same name (only different extensions). The filenames contain UTF characters (UTF-8 I believe?) such as °, φ and θ. All of this works perfectly fine when I compile and execute it in Linux with g++ 4.4.7. I then altered my code to compile in Microsoft Visual Studio 2008, and the problems start when I run the program.

I use the following two bits of code to

  1. Make a standard filename string (to which I just add extensions for the various files)
  2. Open a stream to write to a file (the only difference between the GNUplot script and the .csv files is the extensions

    // Generate a file name string
    stringstream ss;
    ss << type << " Graph #" << gID << " - " << title;
    string fileName = ss.str();
    
    // Open a stream for the output file
    ostringstream outfile;
    outfile << fileName << ".gplt" << ends;
    ofstream ofs( outfile.str().c_str() );
    

The contents of the ofstream files where ofs writes contain the UTF characters properly, however the stringstream-created string fileName and the ostringstream created filename (even when not created with fileName, I tested it) show the characters incorrectly.

Example:

What it should be - CDFvsRd Graph #32 - MWIR @ 300m, no-sun, 30kts, θ=all°.csv
What it ends up as - CDFvsRd Graph #32 - MWIR @ 300m, no-sun, 30kts, Ï=allË.csv

What can I do to remedy this, with as much standard C++ as possible? Would converting my fileName string to wstring help?

David G
  • 94,763
  • 41
  • 167
  • 253
pavichokche
  • 91
  • 1
  • 8
  • Is this related to the general topic of using C++ to access unicode filenames? http://stackoverflow.com/questions/2316672/opening-fstream-with-file-with-unicode-file-name-under-windows-using-non-msvc-co Also this: http://stackoverflow.com/questions/14158018/how-to-read-binary-file-with-unicode-filename-c – pzed Jul 28 '14 at 14:18
  • I suppose, and I had already looked at both of the pages you linked, but neither has an actual solution. – pavichokche Jul 28 '14 at 14:29
  • @pavichokche: [UTF-8 Everywhere](http://www.utf8everywhere.org/) has some recommendations on how to deal with utf-8 reasonably under MSVC. Unfortunately I don't think you're going to find a nice solution. – user786653 Jul 28 '14 at 16:35
  • Can you amend your question by adding hexadecimal dump both of the **output** and **source code** where the non-ASCII are? – Karol S Oct 08 '14 at 12:10

1 Answers1

1

The solution was to write the Windows portion of the code to not export the filenames without the graph titles, omitting the UTF-8 characters from the filename. This wasn't a true solution, only a workaround.

pavichokche
  • 91
  • 1
  • 8