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);
}
//ProgramMain.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib> //used for exit()
#include "myprogram.h"
constchar * file1 = "file.dat"; //Assuming the file exists
usingnamespace MYPROGRAM;
usingnamespace 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!
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);
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?