I tried to make a program which takes a class BANK and stores information such as name,balance,phone etc... but when i write that information onto the file using fstream it displays some weird text on the file screen is attached see for yourself please : http://prntscr.com/jxi62a
but when i display the information in the code via read() it shows normal text,i just want to know why in the notepad it's different than it console?
#include<stdlib.h>
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string>
#include<string.h>
usingnamespace std;
class BANK
{
private :
string name ;
char balance[10] ;
char mobile_num[11] ;
public :
void getInfo()
{
cout<<"\nEnter The Name Of The Account Holder : " ;
getline(cin,name) ;
cout<<"\nEnter The Current Balance In The Account : " ;
gets(balance) ;
cout<<" \nEnter The Mobile Number Associated With The Account : " ;
gets(mobile_num) ;
}
void showInfo()
{
cout<<"\nName Of Account Holder : " << name ;
cout <<"\nCurrent Balance :" << balance ;
cout<<"\nPhone Number : " << mobile_num ;
}
};
int main()
{
BANK obj[5]; ;
fstream f1 ;
int i ;
f1.open("Bank.txt" , ios :: out | ios :: app) ;
cout<<"\nEnter Information For 5 Persons : " ;
for (i = 0 ; i < 5 ; ++i)
{
cout<<"\nPerson " << i + 1 << "\t";
obj[i].getInfo();
f1.write((char*)&obj[i],sizeof(obj[i]));
}
system("cls");
cout<<"\nInformation For 5 Persons : " ;
for (i = 0 ; i < 5 ; ++i)
{
cout<<"\nPerson " << i + 1 << "\t";
f1.read((char*)&obj[i],sizeof(obj[i]));
obj[i].showInfo();
}
return 0 ;
}
I want to know that when i write a structure/class via objects on a file,is that kind of binary dump or anything it's called like shown in the screen shot,or it is possible to write like we normally write english,idk if i still got my question right,please help?
If you write an object like on line 44 you write the internal data of this object.
The internal data may consists of several pointers. Hardly the characters themselves. It is actually unknown. Hence you get rather garbage then useful output in that case.
or it is possible to write like we normally write english
Unlikely. If your object contains only characters without garbage then yes.