write struct to textfile.txt

I'm trying to write a struct to a textfile (txt) after the user has entered: name, haircolor, age and weight. The issue I'm facing is the output in the textfile is korean characters.

This is a seperate file "hero.cpp" which handles the subprograms. There is also a main.cpp and hero.h file.

I tried writing the same code in a separate program and it works properly. I don't know if it's different because it is written in the main program (main.cpp).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  
#include <iostream>
#include <vector>
#include <fstream>
#include "hero.h"

// Struct heroes as parameter to function new_hero
void new_hero(heroes & myHero)
{
  heroes strukt;
  std::cout << '\n' << "Ange din hjältes namn: ";
  std::cin  >> strukt.name;
  std::cout << "Din Hero har nu namnet: "
  << strukt.name << std::endl;
    
  std::cout << "Ange hårfärg: ";
  std::cin >> strukt.haircolor;
  std::cout << "Hårfärgen på din Hero är: "
  << strukt.haircolor << std::endl;
    
  std::cout << '\n';
    
  std::cout << "Ange ålder: ";
  std::cin >> strukt.age;
  std::cout << "Din Hero's ålder: " << strukt.age
  << std::endl;
    
  std::cout << '\n';
    
  std::cout << "Ange din Hero's vikt: ";
  std::cin >> strukt.weight;
  std::cout << "Din Hero's vikt är: " << strukt.weight << std::endl;
    
  std::cout << std::endl;
    
  std::cout << "Ange din Hero's kön: ";
  std::cin >> strukt.sex;
  std::cout << "Din Hero är en: " << strukt.sex << std::endl;
    
  // Writing the heroes name from Struct to REGISTER.TXT
  std::ofstream utdata;
  utdata.open("/Users/username/C++ projects/Stream IO File/Stream IO 
  File/REGISTER.TXT", std::ios::app);
  utdata << strukt.name;
  utdata.close();
does this program have a locale setting in the compiler settings?

I don't see anything wrong at a glance..
I can't recall if you need to open an ofstream with unicode or wide settings somehow?
Last edited on
You also need to show what your overloaded operator<< looks like.
Thanks for fast reply!

jonnin I don't use an overloaded operator. Do you mean that "utdata << strukt.name;"
isn't the correct usage to send data to a textfile via a ofstream?

The thing is that I wrote another program to figure out how streams worked before writing the current program. The only difference is that the program below is written in the main.cpp file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <fstream>
using namespace std;

struct exempel
{
    string name;
    string last_name;
};

int main()
{
  exempel strukt;
  cout << "Skriv in ditt förnamn: " << endl;
  cin >> strukt.name;
  cout << "Skriv in ditt efternamn: " << endl;
  cin >> strukt.last_name;
    
  ofstream utdata;
  utdata.open("/Users/robertenglund/C++ projects/Opening files test/
  opening_files/utfil.txt", ios::app);
  utdata << strukt.name << '\t' << strukt.last_name << ' ' << '\n';
  utdata.close();
    
    
  return 0;
}


Thanks in advance
... ok, do this. print to the screen/console what you think you write to the file in the broken code version.

also, I didnt say anything about overloaded << ... you are using it correctly. You can overload << so the entire struct is written instead of doing it one field at a time, and that is cleaner, but that isnt related to your issue.

Registered users can post here. Sign in or register to post.