Hello guys, i'm having a problem with a function in my program. The function is supposed to open a file, ask the user which part of the file they would like to edit, and then replace that part of the file with the users input. My function for doing this works, but the problem it has is it always replaces the first record in the file.
void change_info()
{
long num_change;
fstream change("people.dat", ios::in | ios::out | ios::binary);
if (!change)
{
cout << "Error opening file. Program aborting.\n";
}
cout << "which file would you like to edit\n";
cin >> num_change;
change.seekg(num_change* sizeof(person), ios::beg);
change.read(reinterpret_cast<char *>(&person), sizeof (person));
cout << "Enter the following information about a person:\n";
cout << "Name: ";
cin.ignore();
cin.getline(person.name, NAME_SIZE);
cout << "Phone: ";
cin.getline(person.phone, PHONE_SIZE);
// I BELEIVE THE ERROR IS HERE, SINCE THIS WHAT ACTUALLY WRITES BACK INTO THE FILE
change.seekp((num_change*sizeof(person), ios::beg));
change.write(reinterpret_cast<char *>(&person), sizeof (person));
change.close();
}
If anyone could give me any pointers I sure would appreciate it :)