简体   繁体   English

C ++ std :: stringstream / ostringstream和UTF字符

[英]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). 我正在编写一个处理某些数据的程序,将其输出到.csv文件,然后编写GNUplot脚本,并调用GNUplot来执行脚本并创建一个具有相同名称的图像文件(只有不同​​的扩展名)。 The filenames contain UTF characters (UTF-8 I believe?) such as °, φ and θ. 文件名包含UTF字符(我相信是UTF-8?),例如°,φ和θ。 All of this works perfectly fine when I compile and execute it in Linux with g++ 4.4.7. 当我使用g ++ 4.4.7在Linux中编译和执行它时,所有这一切都完美无缺。 I then altered my code to compile in Microsoft Visual Studio 2008, and the problems start when I run the program. 然后我改变了我的代码以在Microsoft Visual Studio 2008中编译,并且在运行程序时问题就开始了。

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 打开一个流来写入文件(GNUplot脚本和.csv文件之间的唯一区别是扩展名

     // 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. 其中ofs写入的ofstream文件的内容正确包含UTF字符,但是stringstream创建的字符串fileNameostringstream创建的文件名(即使没有使用fileName创建,我测试过它)也会错误地显示字符。

Example: 例:

What it should be - CDFvsRd Graph #32 - MWIR @ 300m, no-sun, 30kts, θ=all°.csv 它应该是什么 - CDFvsRd图#32 - MWIR @ 300m,无太阳,30kts,θ=全°.csv
What it ends up as - CDFvsRd Graph #32 - MWIR @ 300m, no-sun, 30kts, Ï=allË.csv 结果如何 - CDFvsRd Graph#32 - MWIR @ 300m,no-sun,30kts,Ï=allË.csv

What can I do to remedy this, with as much standard C++ as possible? 我可以用尽可能多的标准C ++来解决这个问题吗? Would converting my fileName string to wstring help? 将我的fileName字符串转换为wstring帮助?

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. 解决方案是编写代码的Windows部分,以便在没有图形标题的情况下导出文件名,省略文件名中的UTF-8字符。 This wasn't a true solution, only a workaround. 这不是一个真正的解决方案,只是一种解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM