Problem with my File IO class

Hey guys,I am still stuck in doing file IO and heres the program thats not working.

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
 #ifndef PHONEBOOK_H_INCLUDED
#define PHONEBOOK_H_INCLUDED

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>


using namespace std;

class Phonebook
{
private:
    string fname;
    char name[30],num[15];
    fstream io;
public:
    Phonebook(){}
    ~Phonebook(){io.close();}
    void get_fname();
    void new_Book();
    void add_record();
    void addwh_record();
    void println_record();
    void printwh_record();
    int getsize(char* x);
};

void Phonebook::get_fname()
{
    cout<<"Enter the file name"<<endl;
    cin>>fname;
}

void Phonebook::new_Book()
{
    Phonebook::get_fname();
    io.open(fname.c_str(),ios::in|ios::out|ios::binary);
}

void Phonebook::add_record()
{
    char ans='y';
    Phonebook::get_fname();
    io.open(fname.c_str(),ios::out|ios::app|ios::binary);
    while(ans=='y'||ans=='Y')
    {
       cout<<"Enter the name"<<endl;
        cin>>name;
        io<<name<<endl;
        io.flush();
        cout<<"Enter the number"<<endl;
        cin>>num;
        io<<num<<endl;
        io.flush();
        cout<<"Wanna add more??"<<endl;
        cin>>ans;
    }
    io.flush();
    io.close();
}

void Phonebook:: addwh_record()
{
    Phonebook::get_fname();
    io.open(fname.c_str(),ios::out|ios::app|ios::binary);
    streampos size;
    char* memblock;
    char c='y';
    while(c=='y'||c=='Y')
    {
        cout<<"Enter the name "<<endl;
        cin>>name;
        size=getsize(name);
        memblock=new char[size];
        io.write(memblock,size);
        io.flush();
        cout<<"Enter the number"<<endl;
        cin>>num;
        size=getsize(num);
        memblock=new char[size];
        io.write(memblock,size);
        io.flush();
        cout<<"Wanna add more??"<<endl;
        cin>>c;
    }
    io.close();
}

void Phonebook::println_record()
{
 Phonebook::get_fname();
    io.open(fname.c_str(),ios::in|ios::app|ios::binary);
    while(!io.eof())
    {
        io.getline(name,30);
        cout<<endl<<name;
        io.getline(num,15);
        cout<<endl<<num<<endl;
    }
    io.close();
}

void Phonebook:: printwh_record()
{
    Phonebook::get_fname();
    io.open(fname.c_str(),ios::in|ios::binary);
    int i;
    char* memblock;
    while(!io.eof())
    {
        io>>name;
        i=getsize(name);
        memblock= new char[i];
        io.read(memblock,i);
        io>>num;
        i=getsize(num);
        memblock=new char[i];
        io.read(memblock,i);
        io.flush();
    }
    io.close();
}

 int Phonebook::getsize(char* x)
{
    int i;
    for(i=0;x[i]!='\0';i++);
    return i;
}

#endif // PHONEBOOK_H_INCLUDED 



The problem is ,when it starts to read from a file,it opens the file,reads all that is there in the file,and then the cursor just zooms down to like nowhere. I mean the console window goes black!!!

I dont understand where the problem lies,so can you guys help me???
Thnx for your patience!!
Topic archived. No new replies allowed.