|
-
April 30th, 2012, 02:18 AM
#1
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
Code:
#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;
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|