9 Replies - 739 Views - Last Post: 06 October 2011 - 04:09 AM Rate Topic: -----

#1 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

delete and update from file function

Posted 28 September 2011 - 03:52 PM

Hey!

I've been getting the hang of this project my teacher gave me finally!
I was confused about how to open and close the iofile, but now I get that.
I'm only building the program with 2 things in the struct at the moment but later on I will update this...
The main problem I'm having is with the delete and update functions... I don't really understand the whole concept behind it... If anyone could help me with how these work that would be much appreciated


#include <iostream>
#include <conio.h>
#include <fstream>

using namespace std;

const char FILE_PATH[] = "D:\\iofile";
//------------------------------------------------------------------------------

struct Customer
{
   char names [ 16 ];
   int other;
};

void switchFoo();
void menu();
void addRec();
void updateRec();
void deleteRec();
void printAll();
void createFile();
int displayRec();
int searchRec( int );

fstream MyFile ( FILE_PATH, ios :: binary | ios :: out | ios :: in );
//------------------------------------------------------------------------------
int main ( int argc, char* argv [] )
{
	createFile();
	switchFoo();

	return 0;
}
//------------------------------------------------------------------------------
void switchFoo()
{
   char choice;

	menu();
	choice = getch();
   while ( choice != 54 )
	{
		switch ( choice )
      {
         case '1' :
            addRec();
				break;

			case '2' :
				displayRec();
				break;

			case '3' :
				updateRec();
				break;

			case '4' :
				deleteRec();
				break;

			case '5' :
				printAll();
				break;

			case '6' :
				break;

			default :
				cout << choice << " is not a vaild choice" << endl
					  << "\t\t Please try again: ";
            getch();
      }
      menu();
      choice = getch();
	}
}
//------------------------------------------------------------------------------
void menu()
{
   system("cls");
   cout << "\t\t|--------------------------------------------------|" << endl
        << "\t\t|                  .::MAIN MENU::.                 |" << endl
        << "\t\t|--------------------------------------------------|" << endl
        << "\t\t| 1.   Register New Account                        |" << endl
        << "\t\t| 2.   Look Up Existing Customer                   |" << endl
        << "\t\t| 3.   Deposit / Withdraw                          |" << endl
        << "\t\t| 4.   Remove Account                              |" << endl
        << "\t\t| 5.   Show All Accounts                           |" << endl
        << "\t\t| 6.   Exit                                        |" << endl
        << "\t\t|--------------------------------------------------|" << endl
		  << "\t\t Selection: ";
}
//------------------------------------------------------------------------------
void addRec()
{
	Customer temp;

	system("cls");
	cout << "\t\t|--------------------------------------------------|" << endl
		  << "\t\t|              .::NEW ACCOUNT MENU::.              |" << endl
		  << "\t\t|--------------------------------------------------|" << endl
	     << "\t\t  Welcome new customer!                             " << endl
		  << "\t\t  We're going to set you up with a new account...   " << endl
		  << "\t\t  All we need is a few details, let's begin:        " << endl;

	cout << endl << "\t\t  Enter first name : ";
	cin >> temp.names;
	cout << "\t\t  And your age : ";
	cin >> temp.other;

	cout << endl << "\t\t  Hello " << temp.names << ". " << endl
        << "\t\t  We've now set up that account for you. " << endl
        << "\t\t  Press any key to continue back to main menu...";

	getch();

	MyFile.open ( FILE_PATH , ios :: binary | ios :: app | ios :: out );
	MyFile.clear();
	MyFile.write ( ( const char* ) &temp, sizeof ( Customer ) );
	MyFile.clear();
	MyFile.close();
}
//------------------------------------------------------------------------------
void printAll()
{
	Customer temp;

	MyFile.open ( FILE_PATH , ios :: binary | ios :: in );
	MyFile.clear();
	MyFile.read ( ( char* ) &temp, sizeof ( Customer ) );

	system("cls");
	cout << "\t\t|--------------------------------------------------|" << endl
		  << "\t\t|             .::ALL ACCOUNTS MENU::.              |" << endl
		  << "\t\t|--------------------------------------------------|" << endl;

	while ( ! MyFile.eof() )
	{
		cout << "\t\t| Name: " << temp.names << "\t\t\t\t\t   |" << endl
			  << "\t\t| Age:  " << temp.other << "\t\t\t\t\t   |" << endl
			  << "\t\t --------------------------------------------------" << endl;
		MyFile.read ( ( char* ) &temp, sizeof ( Customer ) );
	}

	if ( ! MyFile )
	{
		cout << "\t\t No records to show yet... ";
	}
	cout << endl << "\t\t Press any key to continue back to main menu...";
	getch();

	MyFile.close();
}
//------------------------------------------------------------------------------
int displayRec()
{
	Customer temp;
	int age;
	int position;

	system("cls");
	cout << "\t\t|--------------------------------------------------|" << endl
		  << "\t\t|              .::ACCOUNT RECORDS::.               |" << endl
		  << "\t\t|--------------------------------------------------|" << endl;

	cout << "\t\t Enter age of person you are seeking: ";
	cin >> age;

	position = searchRec ( age );

	MyFile.open ( FILE_PATH , ios :: binary | ios :: in );
	MyFile.clear();
	MyFile.seekp ( position * sizeof ( Customer ), ios :: beg );
	MyFile.read ( ( char* ) &temp, sizeof ( Customer ) );

	if ( age != temp.other )
	{
		cout << endl << "\t\t No record found. Press any key to continue...";
	}
	else
	{
		cout << endl << "\t\t We found your record: " << endl << endl
			  << "\t\t --------------------------------------------------" << endl
			  << "\t\t| Name: " << temp.names << "\t\t\t\t\t   |" << endl
			  << "\t\t| Age:  " << temp.other << "\t\t\t\t\t   |" << endl
			  << "\t\t --------------------------------------------------" << endl;
	}

	cout << endl << "\t\t Press any key to continue...";
	getch();
	MyFile.close();

}
//------------------------------------------------------------------------------
void deleteRec()
{
   system("cls");
   cout << "deleteRecord() called" << endl;
}
//------------------------------------------------------------------------------
void updateRec()
{
   system("cls");
   cout << "updateRecord() called" << endl;
}
//------------------------------------------------------------------------------
int searchRec( int age )
{
	Customer temp;
	int position;
	int i = 0;

	MyFile.open ( FILE_PATH , ios :: binary | ios :: in );
	MyFile.clear();
	MyFile.seekp ( 0 , ios :: beg );

	MyFile.read ( ( char* ) &temp, sizeof ( Customer ) );
	while ( ! MyFile.eof() )
	{
		if ( age == temp.other )
		{
			position = i;
		}
		i++;
		MyFile.read ( ( char* ) &temp, sizeof ( Customer ) );
	}
	return position;
}
//------------------------------------------------------------------------------
//void createFile()
//Purpose:		0
void createFile()
{
	if ( ! MyFile )
	{
		fstream MyFile ( FILE_PATH, ios :: binary | ios :: out );
	}
	MyFile.close();
	MyFile.open ( FILE_PATH, ios :: binary | ios :: in | ios :: out );
	MyFile.close();

	if ( ! MyFile )
	{
		cout << "File Error: File does not exist. Creating new file..." 
                     << endl << "Click to continue to main menu...";
		getch();
	}
}
//-------------------------------------------------------------------------------



So far...

Is This A Good Question/Topic? 0
  • +

Replies To: delete and update from file function

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 974
  • View blog
  • Posts: 3,390
  • Joined: 19-February 09

Re: delete and update from file function

Posted 28 September 2011 - 07:50 PM

With delete record, the file is read and written to another file, missing out the record to be deleted. If all goes well the original file is deleted and the new file is renamed to the name of the original file.

Update record is handled in a similar way.
Was This Post Helpful? 0
  • +
  • -

#3 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: delete and update from file function

Posted 29 September 2011 - 08:07 PM

View Post#define, on 28 September 2011 - 07:50 PM, said:

With delete record, the file is read and written to another file, missing out the record to be deleted. If all goes well the original file is deleted and the new file is renamed to the name of the original file.

Update record is handled in a similar way.


how do you do that? Sorry the delete is really cnofusing me
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon

Reputation: 3044
  • View blog
  • Posts: 9,280
  • Joined: 25-December 09

Re: delete and update from file function

Posted 29 September 2011 - 08:29 PM

To remove a "record" from a file in C/C++ you will need to copy all the "records" that you want to keep from your file to a temporary file then either copy the temporary file to the original file and then delete the temporary file. Or if all the "records" are in memory you can delete the file and then recreate the file from the data held in memory.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 974
  • View blog
  • Posts: 3,390
  • Joined: 19-February 09

Re: delete and update from file function

Posted 30 September 2011 - 09:21 PM

Ahh, you are using a fixed width record.

struct Customer
{
   char names [ 16 ];
   int other;
};



You could set a value in your record to say whether the record was being used or not. You could use names, probably a more professional way would be to create a separate flag variable.

struct Customer
{
   char names [ 16 ];
   int other;
   char used; 
};


This post has been edited by #define: 02 October 2011 - 12:48 PM

Was This Post Helpful? 0
  • +
  • -

#6 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: delete and update from file function

Posted 05 October 2011 - 06:09 PM

I hate to say this but I've been trying to figure this out for the last week and gotten nowhere.. I'm finding it almost impossible :(
Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 974
  • View blog
  • Posts: 3,390
  • Joined: 19-February 09

Re: delete and update from file function

Posted 05 October 2011 - 09:47 PM

There are different ways to do this but you can do it in the following manner.
The concept is : if you have a list and copy it to another list but miss one item out. Then you have deleted one item from the list.

list
item A 
item B 
item C
item D



list 2
item A 
item B 
item D



So you need to :
1) ask user which record to delete (name?),
2) open input file and output file,
3) read record from input file,
4) write record to output file unless it is the record to be deleted,
5) close input file and output file,
6) delete input file and rename output file (if a record has been deleted.

If you have any further questions just ask. :)
Was This Post Helpful? 1
  • +
  • -

#8 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: delete and update from file function

Posted 05 October 2011 - 11:59 PM

View Post#define, on 05 October 2011 - 09:47 PM, said:

There are different ways to do this but you can do it in the following manner.
The concept is : if you have a list and copy it to another list but miss one item out. Then you have deleted one item from the list.

list
item A 
item B 
item C
item D



list 2
item A 
item B 
item D



So you need to :
1) ask user which record to delete (name?),
2) open input file and output file,
3) read record from input file,
4) write record to output file unless it is the record to be deleted,
5) close input file and output file,
6) delete input file and rename output file (if a record has been deleted.

If you have any further questions just ask. :)



Right so... Think I've almost done it... although it isn't working properly? Anyone got an idea?
void deleteRec()
{
   Customer temp;
	char choice;
	int position;

	position = displayRec ();

	MyFile.open( FILE_PATH, ios :: binary | ios :: in | ios :: out );
	MyFile.clear();
	MyFile.seekp( position * sizeof( Customer ), ios :: beg );
	MyFile.read( ( char* )&temp, sizeof( Customer ) );
	
	cout << endl << endl;
	cout << "\t\t Would you like to delete this record? Yes (y) or No (n): ";
	cin >> choice;
	
	choice = getch();

	if( choice == 'y' && temp.isDeleted == false )
	{
		temp.isDeleted = true;
		cout << "Customer " << temp.userId << " was deleted. ";
	}
	else
		if( choice == 'y' && temp.isDeleted == true )
		{
			temp.isDeleted = false;
			cout << "Customer " << temp.userId << " was undeleted. ";
		}

	MyFile.seekp( position * sizeof( Customer), ios :: beg );
	MyFile.write( ( const char* )&temp, sizeof( Customer ) );

	MyFile.close();
}


Was This Post Helpful? 0
  • +
  • -

#9 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: delete and update from file function

Posted 06 October 2011 - 02:48 AM

anyone?
Was This Post Helpful? 0
  • +
  • -

#10 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: delete and update from file function

Posted 06 October 2011 - 04:09 AM

oh and here's what I've done for the update function, which seems to do what it's meant to but then doesn't work...

Customer temp;
	time_t t;
	char choice;
	int position;

	position = displayRec ();

	if( position != -1 )
	{
		cout << endl << "\t\t Do you want to Update this Record( y or n )";
		choice = getch();
	}

	if( choice == 'y' )
	{
		cout << endl << "\t\t Enter first name : ";
	   cin >> temp.first;

 	   cout << endl << "\t\t Enter family name : ";
	   cin >> temp.second;
	
	   cout << endl << "\t\t And your age : ";
	   cin >> temp.age;
	
	   cout << endl << "\t\t Please enter type of account you'd like to use: "
	        << endl << "\t\t Savings or Cheque? : ";
      cin >> temp.accType;
   
      cout << endl << "\t\t Please enter a 5 digit account number that you will"
           << " remember \t\t\t  we'll be using this to identify you: ";
      cin >> temp.userId;
		cout << endl;
		
		temp.isDeleted = false;
		time( &t );
		strcpy( temp.whenUpdated, ctime( &t ) );
		
		MyFile.open( FILE_PATH, ios :: binary | ios :: out );
	   MyFile.clear();
	   MyFile.seekp( position * sizeof( Customer ), ios :: beg );
      MyFile.clear();
		MyFile.write( ( const char* ) &temp, sizeof( Customer ) );
      MyFile.close();
   }
}



It's also meant to show when a change has been made, hence the
strcpy( temp.whenUpdated, ctime( &t ) ); 
line.. hope these are easy fixes
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1