Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ std::stringstream/ostringstream and UTF characters

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?

like image 591
pavichokche Avatar asked Nov 10 '22 04:11

pavichokche


1 Answers

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.

like image 98
pavichokche Avatar answered Nov 15 '22 12:11

pavichokche