A
A
Anyosi2020-04-20 22:33:36
C++ / C#
Anyosi, 2020-04-20 22:33:36

What is the correct way to change an entry in a file (C++)?

The function should find the entry in the file by the student id and the subject code and change it. But after its work, only the changed record and zeros in the file remain. What needs to be done to make it work correctly?

void Register::setRecord() {
  int id, code;
  int count = 0;
  cout << "--Изменение данных--" << endl;
  cout << "Введите номер студенческого билета: ";
  cin >> id;
  cout << "Введите номер предмета: ";
  cin >> code;

  fileRead.open(file, ios::binary);

  if (fileRead.is_open())
  {
    Record record;
    while (fileRead.read((char*)&record, sizeof(Record)))
    {
      if (record.studentId == id && record.subId == code) {
        Record rec;
        fileWrite.open(file, ios::binary);
        fileWrite.seekp(sizeof(Record)*(count), ios::beg);

        cout << "Имя студента: ";
        cin >> rec.student;
        cout << "Номер билета: ";
        cin >> rec.studentId;
        cout << "Название предмета: ";
        cin >> rec.subject;
        cout << "Номер предмета: ";
        cin >> rec.subId;
        cout << "Оценка: ";
        cin >> rec.mark;
        
        fileWrite.write((char*)&rec, sizeof(Record));
        fileWrite.close();
      }

      count++;
    }
    fileRead.close();
  }
  else
  {
    cout << "File not founded." << endl;
  }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question