Need help writing to binary file

Well, as the title says, I need help writing to the binary file. The program compiles just fine, but when I try writing to the file, it doesn't work. It does print out the information just because the information is stored within the structure. But when I try to print out information from the file, it shows nothing. Even when I try opening the file itself in Notepad, it's empty. Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
//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
44
45
46
47
//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
   }
   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
   cout << "First name: " << person.firstName << endl;
   cout << "Last name: " << person.lastName << endl;
   cout << "Age: " << person.age << endl;

   //prints out information of person from file
   while(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;
}


Thank you for taking the time in looking at my post and any help is appreciated! Thanks!
Last edited on
How and where did you write data to the file?

I don't know if it's a good idea to open the file for both reading and writing at the same time. I have never done that so I don't know if it will work somehow. It looks like you only read from the file so you could try to open the file for reading only.

Should be writing the data to the file on lines 22-24. I wrote this code up pretty fast, so I forgot to add in finout.read((char *) &person, sizeof person) but it still doesn't work.

Opening a file for both reading and writing is perfectly fine. Even the book I'm learning from does it, hence the reason why I'm trying to do this. The difference between mine and the book's code, though, is that he doesn't use a header file or a different namespace like I do. It's all under one code, hell, he even did it all under the main function. He did say that you shouldn't be putting it under one function, but his still works fine...
Line 22-24? You mean the addPerson function? It only prints with cout and read input from cin. It doesn't touch the file.

To write to the file you probably want to use the write function similar to how you use the read function.
finout.write((char *) &person, sizeof person);
Last edited on
Ok, so now when I put that before calling the function, it definitely does write to the file (checked through opening the actual file in Notepad) but there's a problem.... when I try printing out the information, I get nothing but jibberish, which means it isn't writing the right thing to the file.... any way to fix this?
Last edited on
You should probably write to the file after you have called addPerson because it is not until after that person contains the correct values.
Last edited on
It's still printing out jibberish, but when I go into the file itself, I do see what I inputted, but with a lot of random characters.
Topic archived. No new replies allowed.