S
S
Sergey Sablin2019-02-14 18:14:35
C++ / C#
Sergey Sablin, 2019-02-14 18:14:35

Why are strange characters output to the file?

In the program, I want to output the data structure to a binary file, but when I open the file, I see symbols of the form: Ёw‡ Pq‡ Фq‡ Фq‡ .
Program code:

#include <iostream>
#include <vector>
#include <stdio.h>

using namespace std;

struct t{
  string nazv;
    int semestr;
    vector <int> a;
    t()
};

t::t(){
  this->nazv = "default name";
  this->semestr = 1;
  this->a.push_back(1);
}

int main()
{
setlocale(LC_ALL, "Rus");
cout <<" введите количество предметов\n";
int n; //количество предметов
cin>>n;
  
for (int i=0;i<n;i++){
int tmp;

t name;	
cout <<"введите название предмета\n";
cin>> name.nazv;
cout << "введите количество семестров\n";
cin>> name.semestr ;//cin >> name.a ;
for (int i=0;i<name.semestr;i++){
  cout << "введите номера семестров\n";
  cin>>tmp;
  name.a.push_back(tmp);	
}
FILE * f;
f = fopen("5.txt", "wb+");
fwrite(&name, sizeof(name), 1, f);
fclose(f);
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VoidVolker, 2019-02-14
@VoidVolker

It is necessary to write to the file not the structures themselves, but the fields of the structure in turn. There may be another problem with the encoding.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question