File Handling :Weird Text being written onto the file

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?

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
57
58
#include<stdlib.h>
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string>
#include<string.h>
using namespace 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 ;


}
The reason is line 44: You cannot write complex data like std::string like so.

I would suggest that you change showInfo() so that it takes std::ostream &:
1
2
3
4
5
6
void showInfo(std::ostream &out)
    {
        out<<"\nName Of Account Holder : " << name ;
        out <<"\nCurrent Balance :"    << balance ;
        out<<"\nPhone Number : "    << mobile_num ;
    }
This way you can pass cout and f1:
1
2
obj[i].getInfo(cout);
obj[i].getInfo(f1);


The same applies for reading -> istream
Last edited on
I dont think you quite got my question.

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?
Last edited on
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.
Topic archived. No new replies allowed.