Binary File Error

The code compiles but does not record (or) show the data properly. It only displays the last entered data. I couldn't figure out what exactly is wrong.
I would really appreciate any 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
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
134
135

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include<string.h>

using namespace std;

//prototypes:
void findEntry(fstream&);
void addNewEntry(fstream&);
char *encrypt(char *var);
char *decrypt(char *var);
void printData(fstream&);

struct Data
{
	char username[20], password[30], info[50], website[30];
};
int main()
{
	fstream ioFile("Record2.ros",  ios::out | ios:: in | ios::binary);
	if(!ioFile){
	          cout << "Error opening file! Aborting ...";
	          return 0;
	 }
	 ioFile.clear();
    int choice;
    bool ex = true;
    while(ex == true){
    //Menu:
    cout << " Main Manu\n 1-Add New Entry\n 2-Find an Entry\n 3-Print All Data\n 4-Exit";
    cout << "Enter your choice: ";
    cin >> choice;
    while(choice > 4 || choice < 1)
    {
         cout << "choose between 1-4: ";
         cin >> choice;
    }

    switch(choice)
    {
         case 1:
              addNewEntry(ioFile);
              break;
         case 2:
              //findEntry(ioFile);
              break;
         case 3:
              printData(ioFile);
              break;
         default:
              cout << "Exiting ..." << endl;
              ex = false;
              break;
    }
    }
    system("pause");
    ioFile.close();


    return 0;
}
void printData(fstream& ioFile)
{
	
	Data dataObj;

	ioFile.read(reinterpret_cast<char *>(&dataObj), sizeof(dataObj));
	ioFile.seekg(0L, ios::beg);//move to the first byte
	while(! ioFile.eof())
	{
		//ioFile.read((char *)&dataObj, sizeof(dataObj));

		cout << "Printing Data:" << endl; 								//Read & Write the Website info:

		cout << "Website: " << decrypt(dataObj.website) ;
																		//Read & Write the UserName:
		cout << "\tUser Name: " << decrypt(dataObj.username) ;
																		//Read & Write the Password:
		cout << "\tPassword: " << decrypt(dataObj.password);
																		//Read & Write the Info:
		cout << "\nInfo: " << decrypt(dataObj.info);
																		//Write Object to file:
		cout << endl;
		ioFile.read(reinterpret_cast<char *>(&dataObj), sizeof(dataObj));
	}

}

void addNewEntry(fstream& ioFile)
{
    //ofstream ofs("Records2.ros", ios::binary);

    Data dataObj;
    char temp[100];
    cout << "New Entry" << endl;
    											//Read & Write the Website info:
    cout << "Enter the website Name: ";
    cin >> temp;
    encrypt(temp);
    strcpy(dataObj.website, temp);
    											//Read & Write the UserName:
    cout << "Enter the User Name: ";
    cin >> temp;
    encrypt(temp);
    strcpy(dataObj.username, temp);
    											//Read & Write the Password:
    cout << "Enter the Password: ";
    cin >> temp;
    encrypt(temp);
    strcpy(dataObj.password, temp);
    											//Read & Write the Info:
    cout << "Enter Any Additional Info (up to 100 character): ";
    cin >> temp;
    encrypt(temp);
    strcpy(dataObj.info, temp);
    											//Write Object to file:
    ioFile.seekp(0L, ios::end);//Move to the last byte
    ioFile.write(reinterpret_cast<char *>(&dataObj), sizeof(dataObj));//write the record
    cout << "Entry Added!" << endl;
}

char *encrypt(char *var)
{
    return var;
}

char *decrypt(char *var)
{
    return var;
}
Topic archived. No new replies allowed.