I know it's a lot of code but any help anyone can give would be amazing. Basically when I re-write out to the binary file it must not be working because when I try to print it it only prints the record I updated.

void updateRecord()
{
   int key;
  int gpos;
 
 cout << "Enter the ID of the employee whose information you would like to update"<<endl;
 cin >> key;
 bool posnotfound;
 long pos=0;
 posnotfound = search (pos, binfile, key);
 binfile.open ("data.dat", ios::in);
 if (posnotfound == true)
   cout << "RECORD NOT FOUND";
 else 
  {  
   binfile.seekg(pos-sizeof(person), ios::beg);
   binfile.read ((char*)&person,sizeof(person));
   cout << person.ID<<endl;
   cout << person.lname;
   cout << person.fname<<endl;
   cout << person.position<<endl;
   cout << person.hours<<endl;
   cout << person.salary<<endl;
   int switcher;
   cout << "What do you want to update?"<<endl;
   cout << "1. Position"<<endl;
   cout << "2. Hours"<<endl;
   cout << "3. Salary"<<endl;
   cin >> switcher;
   switch (switcher)
   {
     case 1:
       cout <<"Enter the employee's new position"<<endl;
       cin >> person.position;
     break;
     case 2:
       cout << "Enter the employee's new hours"<<endl;       
       cin >> person.hours;
     break;
     case 3:
       cout << "Enter the employee's new salary"<<endl;
       cin >> person.salary;
     break;
   }
   cout << person.ID<<endl;
   cout << person.lname;
   cout << person.fname<<endl;
   cout << person.position<<endl;
   cout << person.hours<<endl;
   cout << person.salary<<endl;
   binfile.close();
   binfile.open("data.dat",ios::out);
   binfile.seekp(pos-sizeof(person),ios::beg);
   binfile.write ((char*)&person,sizeof(person));
   binfile.close();
   binfile.open("data.dat",ios::in);
  }
 return;
}

Recommended Answers

All 2 Replies

>> lines 12 and 53: binfile.open ("data.dat", ios::in);

If this is a binary file then you need to open it in binary move binfile.open ("data.dat", ios::in | ios::binary); line 52: add binfile.clear() to clear all file errors before attempting to reuse the same fstream.

line 53: I think you have to open the file for in|out when attempting to use seekg().

Opening with ios:out will truncate the file.
You need to open it (for writing) with:
ios::out | ios::in | ios::ate
(as well as in binary mode) to seek within with with write capabilities.

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.