mercredi 18 avril 2018

type in special characters and save in file

So i want to use a plain textfile to save input from a user to read it out later In the application it is possible that there occur german and spanish special signs. unfortunately the code below doesnt save the signs properly in the file. How is it best to solve this issue?

I solved this issue in a similar application in C by saving in a .bin file instead of a .txt but isnt there a better solution in c++?

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

int main() {


    std::string s = "abcöäüÑ"; //(alt + 165 for Ñ)
    std::ofstream ofs{ "test.txt" };
    ofs << s <<'\n';            //this works fine  "abcöäüÑ"


    std::string s2;
    std::cin >> s2;     //typeing in the app abcöäüÑ   (alt+165 for Ñ)
    // i use a windows system with a german keyboard setting

    ofs << s2 <<'\n';       //this doesn't it gets   "abc”„¥"
}

I use a windows 7 64 system with visual studio 2017 with a german keyboard setting.

Aucun commentaire:

Enregistrer un commentaire