Binary files

Can you help me with creating a binary file? heres my code:

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
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

const int cant=2;
struct Registro {
    int cif;
    string nombre, apellido;
    int nota[3];
};
Registro alumno[cant];

void print()
{
     system("cls");
    for (int i=0; i<cant; i++)
    {
        cout << "\nEstudiante #" << i+1 << endl;
        cout << "\n\nCIF: "<<alumno[i].cif;
        cout << "\t\n\tNombre: "<<alumno[i].nombre;
        cout << "\t\tApellido: "<<alumno[i].apellido;
        cout << "\t\n\n\tPrimera nota: "<<alumno[i].nota[0];
        cout << "\tSegunda nota: "<<alumno[i].nota[1];
        cout << "\tNota final : "<< alumno[i].nota[2]<<"\n"<<endl;
    }

}


int main() {

   Registro usuario;

   ofstream fsalida("binario.dat",
      ios::out | ios::binary);
   strcpy

   void print()


   fsalida.write(void print);
   fsalida.close();

   ifstream fentrada("binario.dat",
      ios::in | ios::binary);

   fentrada.read(reinterpret_cast<char *>(usuario),
      sizeof(Registro));

  void print();
   fentrada.close();

   return 0;
}


line 40 expected ; before void... if i put the semicolon after strcpy it gives me another error: expected primary expression before
also: invalid reinterpret_cast from type 'Registro' to type 'char*'
Last edited on
You need to go back to your textbooks and learn the correct syntax for calling a function. You're doing it wrong in several different places, in several different ways. This is absolutely fundamental to writing C++, so better you go to your textbook and make sure to learn it properly.
so, what are you trying to do with strcpy? I don't see any use for it?

on line 40/52 you declare the function print(). You do not call it. Remove the return type void in order to make a call
thanks for the replies.
Looking at another example I used strcpy cause I understood that was for writing into binary files(?)

I have corrected the print calling. still getting errors.

no matching function for call to 'std::basic_ofstream<char, std::char_traits<ch

note: candidates are std::basic_ostream<_charT, _Traits>& std::basic_ostream<_charT, ...
invalid reinterpret_cast from type 'Registro' to type 'char*'
As MikeyBoy told you: you should really learn more about how things are done in C/C++

Looking at another example I used strcpy cause I understood that was for writing into binary files(?)
No.

line 43 doesn't make sense at all. What are you trying to do?

on line 49: usuario is an object and not a pointer. To make the cast work you need a pointer. Like so:
1
2
   fentrada.read(reinterpret_cast<char *>(&usuario), // Notice the &
      sizeof(Registro));


read and write is analogous here. So change line 43 and read this:

http://www.cplusplus.com/reference/istream/istream/read/?kw=read
http://www.cplusplus.com/reference/ostream/ostream/write/?kw=write
Syntax errors aside... you CANNOT read/write a Registro object directly to a binary file because it is a non-POD type. Doing so will have unexepected behavior and may cause your program to crash.

I explain why you can't do this... and also show examples of how to read/write binary files here:

http://www.cplusplus.com/forum/beginner/108114/#msg587223
http://www.cplusplus.com/articles/DzywvCM9/
Last edited on
Topic archived. No new replies allowed.