hey guys, im having a slight issue with overwriting a certain record that is selected by a user

Basically my application is meant to get user in puts and store them in a file which is refered to when updating, adding a new record, deleting, searching etc

here is the just of it

Select 1 to create a file
Select 2 to show all records
Select 3 to Add a record
Select 4 to update a record
select 5 to delete a record

so far everything up to 3 works... including a search function used to update and delete a record specified by the user

heres the code anyway

//-------------------------------------------------------
//This is used to display the recently modified, or added
//record for the useres info
//-------------------------------------------------------
void displayRecord()
{
	fstream recordFile;
	recordFile.open( "H:\\records.txt", ios::in);
	recordFile.seekg(-sizeof(participant), ios::cur );
	participant person;

	cout << "\n\t**********Current Record**********";



	recordFile.read((char*) &person, sizeof (participant));

	cout << endl;
	cout << "\nName: \t\t" << person.name
		  << "\nAge: \t\t" << person.age
		  << "\nGender: \t" << person.gender
		  << "\nBelt: \t\t" << person.belt
		  << "\nWeight: \t" << person.weight
		  << "\nHeight: \t" << person.height
		  << "\nDate modified: \t" << ctime(&person.modified);

  getch();

}
//---------------------------------------------------------------------------
//This is the update of the record where the user selects a choice thorugh a 
//switch statement which goes to the search (below) and sends it back here
//once the choice of record to be modified has been entered
//---------------------------------------------------------------------------
void updateRecord()
{
	clrscr();
	fstream recordFile;
	/*this below is where i think the issue lies for some reason
        the ios::ate | ios::out | ios::app is not editing the field in the
        manner i would like, in one way it would just append in another
        it would overwrite and append, and the last just clears info*/
        recordFile.open( "H:\\records.txt", ios::binary | ios::ate );

	participant person;



	cout << "\t**********Edit a new Record**********\n\n";

	cout << "\nPlease specify the participants gender: ";
	cin >> person.gender;
	cout << "\nPlease enter the participants belt category: ";
	cin >> person.belt;
	cout << "\nPlease enter the participants name: ";
	cin >> person.name;
	cout << "\nPlease enter the participants age: ";
	cin >> person.age;
	cout << "\nPlease enter the participants wieght: ";
	cin >> person.weight;
	cout<< "\nPlease enter the participants height (in cm): ";
	cin >> person.height;
	time(&person.modified);

	//recordFile.seekp(0, ios::cur); i used this just to see what would happen
	recordFile.write((char*) &person, sizeof(participant)); // i also tried using
        //recordFile.write( reinterpret_cast < const char * >( &person), participant( //Tools ) );
	recordFile.close();

	cout << "Below is the submitted record";
	getch();


}
//---------------------------------------------------------------------------
//searches for a specific record by going through the participants entry number
//and subtracting 1 to get its record location (ps this works the above bit doesnt)
//---------------------------------------------------------------------------
void search()
{
	clrscr();
	cout << "\t**********Search for a record**********\n\n";
	fstream recordFile;
	recordFile.open( "H:\\records.txt", ios::in | ios::binary);

	int participantNum;
	participant person;

	cout << "Which Participant would you like to search for: ";
	cin >> participantNum;

	recordFile.seekg((participantNum - 1)*sizeof(participant), ios::beg);

	if (recordFile.eof() && participantNum == 0)
	{
		search();
	}

	recordFile.read((char*) &person, sizeof (participant));
	cout << "\n\nReading request please hold...";



	recordFile.close();


}

To delete a record you have 2 options depending upon the size of Database:

1. Copy everything into a buffer, delete values in the buffer & output them to file again.
2. Start copying everything to buffer skip the matching record & output them again.
3. Instead of allocating a buffer open another output stream & stream everything minus the record to be deleted. Then delete the original file & rename new one to old one.

well the thing is all i need to figure out is how to make that one line of code overwrite just a record instead of the whole file.

what you are suggesting is the delete section i just want to copy for now

I am suggesting the 3rd if you have a big database & allocating memory for buffer would not be such a good idea. Just put an if-else condition to skip streaming the selected record.

nah its just a class assignment and the tutor is apparently not allowed to help us and we not allowed to ask each other, (for some reason as they consider it copying) yet we are allowed to go online for info which contradictory is not considered copying

we not allowed to ask each other yet we are allowed to go online for info which contradictory is not considered copying

That my friend is the irony of life. :)

And I still suggest you go for 3rd. Sorry I missed that you infact were using a class.
I also suggest you not to use eof() as it makes you read 1 record extra.

while(recordFile.read((char*) &person, sizeof (participant)))
{
    if(person.SOME_VALUE!=MATCH_VALUE)
        tempFile.write((char*) &person, sizeof(participant));
}
recordFile.close();
tempFile.close();
remove("recordFile.txt");
rename("tempFile.txt",recordFileName.txt");

yea i know hey...

Ok but is that going to delete a record rather than update it...

like for example i have the input as follows

Gender: male
name: bob
age: 10
weight: 70


and i want to change just that record not any other record to this

Gender: male
name: bob
age: 20
weight: 70

Oh! Apologies I thought you wanted to delete it. But still it IS gonna work. Just put an else statement to do the updating & print it in else.

C++ Syntax (Toggle Plain Text)
while(recordFile.read((char*) &person, sizeof (participant)))
{
    if(person.SOME_VALUE!=MATCH_VALUE)
        tempFile.write((char*) &person, sizeof(participant));
    else
        //Change values of person & write it to temp.
}
recordFile.close();
tempFile.close();
remove("recordFile.txt");
rename("tempFile.txt",recordFileName.txt");
commented: this worked +1

is there no way to just edit what i have already done and then use teh delte function later or im gonna feel so depreesed for spending 2 hours on nothing

#
clrscr();
#
fstream recordFile;
#
/*this below is where i think the issue lies for some reason
#
  the ios::ate | ios::out | ios::app is not editing the field in the
#
  manner i would like, in one way it would just append in another
#
  it would overwrite and append, and the last just clears info*/
#
recordFile.open( "H:\\records.txt", ios::binary | ios::ate );
#
 
#
participant person;
#
 
#
 
#
 
#
cout << "\t**********Edit a new Record**********\n\n";
#
 
#
cout << "\nPlease specify the participants gender: ";
#
cin >> person.gender;
cout << "\nPlease enter the participants belt category: ";
cin >> person.belt;
cout << "\nPlease enter the participants name: ";
cin >> person.name;
cout << "\nPlease enter the participants age: ";
cin >> person.age;
cout << "\nPlease enter the participants wieght: ";
cin >> person.weight;
cout<< "\nPlease enter the participants height (in cm): ";
cin >> person.height;
time(&person.modified);

//recordFile.seekp(0, ios::cur); i used this just to see what would happen
recordFile.write((char*) &person, sizeof(participant)); // i also tried using
//recordFile.write( reinterpret_cast < const char * >( &person), participant( //Tools ) );
recordFile.close();
 
cout << "Below is the submitted record";
getch();

is there no area you can see there to fix the update up?

Don't be disheartened, I gave you the working Logic, you on the other hand have to implement it in your code. Just change this line:

recordFile.write((char*) &person, sizeof(participant)); // i also tried using

to

participant temp;
while(recordFile.read((char*) &temp, sizeof (participant)))
{
    if(person.SOME_VALUE!=MATCH_VALUE)
        tempFile.write((char*) &temp, sizeof(participant));
    else
        tempFile.write((char*) &person, sizeof(participant));
}
recordFile.close();
tempFile.close();
remove("recordFile.txt");
rename("tempFile.txt",recordFileName.txt");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.