Random Access files

Hi..this is my coding. Its compiled successfully. But i could not get my result. I don't know whether the data stored in the file successfully or not. Please Help me.
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
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
struct status 
{
    char name[80];
    double balance;
    unsigned long account_num;
};


class FileHandler
{
public:
    FileHandler(const string&);
    ~FileHandler();
    
    int getRecords(struct status &);
    int putRecords(struct status &)const;
    int ReadRecord(int ,struct status *);
    int WriteRecord(int ,struct status *);
    

private:
    string fileName;
    fstream fPointer; // ref
};
FileHandler :: FileHandler(const string& fname)
{
    fPointer.open(fname.c_str(), ios::out | ios::in | ios::binary);
}
int FileHandler :: WriteRecord(int RecNum,struct status *acc)
{
    if( fPointer.seekp(RecNum*sizeof(struct status), ios::beg) == 0 )
              if ( fPointer.write((char *) acc, sizeof(struct status) ))
                return 1;
        return 0;
}
int FileHandler :: ReadRecord(int RecNum,struct status *acc)
{
    if( fPointer.seekg(RecNum*sizeof(struct status), ios::beg) == 0 )
              if ( fPointer.read((char *) acc, sizeof(struct status) ))
                return 1;
        return 0;
}
FileHandler :: ~FileHandler()
{
    fPointer.close();
}
int FileHandler :: getRecords(struct status &acc)
{
    strcpy(acc.name, "INFANT");
    acc.balance = 1000;
    acc.account_num = 3;
    return 0;
}
int FileHandler :: putRecords(struct status &acc)const
{
    cout << acc.name << endl;
    cout << "Account # " << acc.account_num;
    cout.precision(2);
    cout.setf(ios::fixed);
    cout << endl << "Balance: $" << acc.balance<<endl;
    return 0;
}
int main()
{
    FileHandler fp("abc.cli");
    struct status acc;
    fp.getRecords(acc);
    fp.WriteRecord(0,&acc);
    strcpy(acc.name, "");
    acc.balance = 0;
    acc.account_num = 0;
    fp.ReadRecord(0,&acc);
    fp.putRecords(acc);
    
    
}
Perhaps this will help:

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
#include <iostream>
#include <fstream>
#include <cstring>
#include <exception>

using namespace std;

struct status
{
    char name[80];
    double balance;
    unsigned long account_num;
};


class FileHandler
{
public:
    FileHandler(const string&);
    ~FileHandler();

    int getRecords(struct status &);
    int putRecords(struct status &)const;
    int ReadRecord(int ,struct status *);
    int WriteRecord(int ,struct status *);


private:
    string fileName;
    fstream fPointer; // ref
};
FileHandler :: FileHandler(const string& fname)
{
	fPointer.exceptions(fstream::failbit | fstream::badbit);
    fPointer.open(fname.c_str(), ios::out | ios::in | ios::binary);
}
int FileHandler :: WriteRecord(int RecNum,struct status *acc)
{
    if( fPointer.seekp(RecNum*sizeof(struct status), ios::beg) == 0 )
              if ( fPointer.write((char *) acc, sizeof(struct status) ))
                return 1;
        return 0;
}
int FileHandler :: ReadRecord(int RecNum,struct status *acc)
{
    if( fPointer.seekg(RecNum*sizeof(struct status), ios::beg) == 0 )
              if ( fPointer.read((char *) acc, sizeof(struct status) ))
                return 1;
        return 0;
}
FileHandler :: ~FileHandler()
{
    fPointer.close();
}
int FileHandler :: getRecords(struct status &acc)
{
    strcpy(acc.name, "INFANT");
    acc.balance = 1000;
    acc.account_num = 3;
    return 0;
}
int FileHandler :: putRecords(struct status &acc)const
{
    cout << acc.name << endl;
    cout << "Account # " << acc.account_num;
    cout.precision(2);
    cout.setf(ios::fixed);
    cout << endl << "Balance: $" << acc.balance<<endl;
    return 0;
}
int main()
{
	try {
		FileHandler fp("abc.cli");
		struct status acc;
		fp.getRecords(acc);
		fp.WriteRecord(0,&acc);
		strcpy(acc.name, "");
		acc.balance = 0;
		acc.account_num = 0;
		fp.ReadRecord(0,&acc);
		fp.putRecords(acc);
	} catch (exception) {
		cout << "An exception occurred" << endl;
	}
    return 0;

}


HINT: Do you want to truncate the file, or append to the file when you open it? Also take a look at the return values of fstream's reads and writes.
Last edited on
Its showing this error :
An exception occurred
I have to append the file.
On your call to open, you need to pass along the fact that you wish to append to the file. Without that information, the file will not be opened (at least it wasn't on my machine).

Also, many functions withing fstream return the stream, not a 0. So you may wish to review lines 39, 40, 46, and 47 above.

Hope this helps.
Topic archived. No new replies allowed.