Binary File Error

Discussion in 'C++' started by NatProg, Apr 30, 2012.

  1. NatProg

    NatProg New Member

    Joined:
    Apr 19, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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

    HTML:
    #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;
    }
    
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Shouldn't printData move to the beginning of the file _before_ reading anything from it?

    Have you checked there are multiple rows of data in the file when you call printData (e.g. by viewing it in Notepad or similar)?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice