Binary file help!

So I'm trying to create a program that writes to a binary file, then displays the information. However, I'm getting a problem... whenever I try to print out the information of the file, I get nothing but jibberish, which does not make sense at all. When I check the binary file, I do see that information was written to it (however, I have no clue what it means). Here are my codes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//myprogram.h

namespace MYPROGRAM
{

   struct STRUCT1   //structure for adding a person
   {
      char firstName[20];
      char lastName[20];
      int age;
   };

   void addPerson(STRUCT1 & person);   
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Program.cpp
#include <iostream>
#include "myprogram.h"

namespace MYPROGRAM
{
   using std::cout;
   using std::cin;
   using std::endl;

   void addPerson(STRUCT1 & person)   //adds the person's information
   {
      cout << "First name: ";
      cin.getline(person.firstName, 20);
      cout << "Last name: ";
      cin.getline(person.lastName, 20);
      cout << "Age: ";
      cin >> person.age;
      cout << endl;
   }
}


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


//ProgramMain.cpp

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>   //used for exit()
#include "myprogram.h"

const char * file1 = "file.dat";   //Assuming the file exists

using namespace MYPROGRAM;
using namespace std;

int main()
{
   STRUCT1 person;

   //opening the file for reading and writing.
   fstream finout;
   finout.open(file1, ios_base::in | ios_base::out | ios_base::binary);

   if (finout.is_open())    
   {
      addPerson(person);   //If the file did open, call to this function
	  finout.write((char *) &person, sizeof person);
   }
   else   //If the file did not open, print out that it did not open, then terminate program
   {
      cerr << file1 << " could not be opened. Terminating program.\n";
      exit(EXIT_FAILURE);
   }

   //prints out information of person from file
   finout.read((char *) &person, sizeof person);
   cout << "First name: " << person.firstName << endl;
   cout << "Last name: " << person.lastName << endl;
   cout << "Age: " << person.age << endl;
   
   finout.close();   //closes the file
   return 0;
} 
Aha, standard file operation mistake, just close the file after writing, open it once again before reading or reposition file pointer to begining before reading.
Last edited on
Every time you run this program, you write one STRUCT1 to the beginning of the file (overwriting whatever was there before) and attempt to read from the file directly afterwards without an intervening seekg(). So, not only are you very likely trying to read from the end of the file, it would fail anyway.

You probably want to seekp() to the end of the file to add new STRUCT1 objects to the file and seekg() to a place where you know one already exists (such as the beginning of the file) before attempting to read one from the file.
anirudh sn, thank you very much sir! Repositioning the file pointer to the beginning before reading did the trick. I can't believe I missed that while looking at the example in my book :P. Thanks again!
Topic archived. No new replies allowed.