Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading the last few characters of a file problems...

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
I've been working on a small bank-like program that writes to a file like this:

Opening Balance >>>>> $546.50
Deposit >>>>>> $52.50; New Balance: $599.00
Withdrawl >>>>>> $49.00; New Balance: $550.00

The writing to the file part works great, but when I re-open the file, I need to somehow grab the balance from the last time, otherwise I just start printing the last spot in memory when I call the function to display the balance (obviously). I am not really great with file manipulation yet (still learning a lot). Is there anyone who can give me an idea on how to grab the balance from the file when I open it again?

Thanks,
Dustin
 
I don't have time for research so I will give you the first program I could find. It deals with binary files, so be warned.
Code:
//START binFilesHeader.h

#include <iostream>
#include <fstream>

using namespace std;

struct MemRec
{
    int num;
    char name[26];
    char type;
    float dues;
};

void InitializeFile(char fname[]);

void PrintFile(char fname[]);

void UpdateFile(char fname[], int choice);

void DeleteMem(char fname[]);

long ReturnByte(long val);

//THIS IS THE END OF binFilesHeader.h

//START binFilesHeader.cpp
#include &quot;binFilesHeader.h&quot;

void InitializeFile(char fname[])
{
    fstream outFile(fname, ios::out|ios::binary);
    MemRec tempRec = {0, &quot; &quot;, ' ', 0.0};
    
    for (int i=0; i<500; i++)
    {
        outFile.seekp(ReturnByte(i), ios::beg);
        outFile.write((char*)&tempRec, sizeof(tempRec));
    }
    
    outFile.close();
}

void PrintFile(char fname[])
{
    fstream inFile(fname, ios::in|ios::binary);
    MemRec memInfo;
    bool flag = false;
    int counter = 0;
    
    while(!inFile.eof())
    {
        inFile.seekg(ReturnByte(counter), ios::beg);
        inFile.read((char*)&memInfo, sizeof(memInfo)); 
        
        if (memInfo.num != 0)
        {
            cout << endl
                 << &quot;Member Number: &quot; << memInfo.num << endl
                 << &quot;Member Name: &quot; << memInfo.name << endl
                 << &quot;Member Type: &quot; << memInfo.type << endl
                 << &quot;Monthly Dues: &quot; << memInfo.dues << endl;
            
            flag = true;
        }
        
        counter++;
    }
    
    if (flag==false)
    {
        cout << endl
             << &quot;There are only \&quot;dummy\&quot; records at this time.&quot;;
    }
    
    inFile.close();
}


void UpdateFile(char fname[], int choice)
{
    fstream outFile(fname, ios::in|ios::out|ios::binary);
    MemRec memInfo;
    int selection;
    int memNum;
    
    cout << endl
         << &quot;Enter member number: &quot;;
    cin >> memNum;
    cin.ignore(1, '\n');
            
    outFile.seekg(ReturnByte(memNum-1), ios::beg);
    outFile.read((char*)&memInfo, sizeof(memInfo));

    if (choice==1)
    {
		    if (memInfo.num != 0)   	        
		  	{
		  	    cout << &quot;Sorry, that member already exists:&quot;
		  	         << endl
		  	         << endl
		  	         << &quot;Member Number: &quot; << memInfo.num << endl
		  	         << &quot;Member Name: &quot; << memInfo.name << endl
		  	         << &quot;Member Type: &quot; << memInfo.type << endl
		  	         << &quot;Monthly Dues: &quot; << memInfo.dues;
		  	}
		  	else
		  	{
		  	    memInfo.num = memNum;
		  	
		  	    cout << &quot;Member number: &quot; << memInfo.num << endl
		  	         << endl
			           << &quot;Enter Member's Name: &quot;;
			      cin.getline(memInfo.name, 26);
			          
			      cout << &quot;Enter Member's Type: &quot;;
			      cin >> memInfo.type;
			      
			      cout << &quot;Enter Monthly Dues: &quot;;
			      cin >> memInfo.dues;
			                          
			      outFile.seekp(ReturnByte(memNum-1), ios::beg);
			      outFile.write((char*)&memInfo, sizeof(memInfo));
			  }
    }
    else
    {
        cout << &quot;Select field to update 1)name, 2)type, 3)dues: &quot;;
        cin >> selection;
        cin.ignore(1,'\n');
        
        switch(selection)
        {
            case 1: cout << &quot;Enter new name: &quot;;
                    cin.getline(memInfo.name, 26);
            break;
            case 2: cout << &quot;Enter membership type (F or S): &quot;;
                    cin >> memInfo.type;
            break;
            case 3: cout << &quot;Enter monthly dues: &quot;;
                    cin >> memInfo.dues;
            break;
        }
        
        outFile.seekp(ReturnByte(memNum-1), ios::beg);
        outFile.write((char*)&memInfo, sizeof(memInfo));
        
        cout << endl
             << &quot;Member Number: &quot; << memInfo.num << endl
             << &quot;Member Name: &quot; << memInfo.name << endl
             << &quot;Member Type: &quot; << memInfo.type << endl
             << &quot;Monthly Dues: &quot; << memInfo.dues;

    }
    
    outFile.close();
}

void DeleteMem(char fname[])
{
    fstream outFile(fname, ios::out|ios::in|ios::binary);
    int memNum;
    
    MemRec nullRec = {0, &quot; &quot;, ' ', 0.0};
    
    cout << &quot;Enter member number: &quot;;
    cin >> memNum;
    
    outFile.seekp(ReturnByte(memNum-1), ios::beg);
    outFile.write((char*)&nullRec, sizeof(nullRec));
}

long ReturnByte(long val)
{
    return sizeof(MemRec) * val;
}
//THIS IS THE END OF binFilesHeader.cpp

//START binFilesDriver.cpp
#include &quot;binFilesHeader.h&quot;

void main(void)
{
    int choice;
    fstream checkFile(&quot;members.dat&quot;, ios::in|ios::binary);
    
    if (checkFile.fail())
    {
        InitializeFile(&quot;members.dat&quot;);
    }
    else
    {
        checkFile.close();
    }
    
    do
    {
        cout << endl
	           << endl
    	       << &quot;Hard Body Health Club&quot; << endl
    		     << endl
    		     << &quot;1) Enter a new member&quot; << endl
    		     << &quot;2) Update a member&quot; << endl
    		     << &quot;3) Delete a member&quot; << endl
    		     << &quot;4) Print member list&quot; << endl
    		     << &quot;5) Quit&quot; << endl
    		     << endl
    		     << &quot;Enter your choice (1-5): &quot;;
    	  cin >> choice;  
        
        if (choice == 1 || choice == 2)
        {
            UpdateFile(&quot;members.dat&quot;, choice);
        }
        else if (choice == 3)
    	  {
    	      DeleteMem(&quot;members.dat&quot;);
    	  }
    	  else if (choice == 4)
    	  {
    	      PrintFile(&quot;members.dat&quot;);
    	  }
   }  
	 while (choice != 5);
	 
	 cout << endl
	      << &quot;END PROGRAM&quot;;
}
//THIS IS THE END OF binFilesDriver.cpp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top