@sueruan012 mình vừa nghĩ ra một cách giải quyết mới nhưng vẫn chưa khả quan lắm
mình có đặt lại hàm output_list_subject(list_subject,n);
dưới hàm main và phần update thì mình đã bỏ khúc đọc xuất file
thay vào đó mình đổi lại là so sánh mã môn vừa nhập với mã môn có sẵn , nếu trùng thì bắt đầu cập nhật tên môn , mã , số tiết lý thuyết và thực hành, nhập tới đâu lưu vào file tới đó , nhưng có một vấn đề là khi mình nhập và xuất danh sách xong tới phần update thì khi mình nhập mã vào nó không cho mình cập nhật mà nó hiện ra luôn danh sách file.
đây là quá trình mình nhập, về phần hiển thị thì nó đã ok rồi , duy chỉ có phần update là còn lỗi thôi, bạn xem giúp mình nó sai ở chỗ nào vậy. thank. 
đây là code phần update của mình
void update_subject(int n)
{
subject list_subject[100];
char old_sub[30],old_code_sub[30];
char new_sub[30],new_code_sub[30];
fstream myfile;
myfile.open(file_mon_hoc,ios::out|ios::app);
cout << endl;
cout << "Enter old subject code: ";
fflush(stdin);
gets(old_code_sub);
for(int i = 0;i<n;i++)
{
if(strcmp(list_subject[i].subject_code,old_code_sub)==0)
{
fflush(stdin);
cout << "Enter code : ";
gets(list_subject[i].subject_code);
myfile << list_subject[i].subject_code << endl;
cout << "Enter name: ";
fflush(stdin);
gets(list_subject[i].subject_name);
myfile << list_subject[i].subject_name << endl;
cout << "Enter number of theoretical subjects: ";
fflush(stdin);
cin >> list_subject[i].number_theory_of_subject;
myfile << list_subject[i].number_theory_of_subject << endl;
cout << "Enter number of practicetical subjects: ";
fflush(stdin);
cin >> list_subject[i].number_of_subject_practice;
myfile << list_subject[i].number_of_subject_practice << endl;
}
}
myfile.close();
}
đây là full code
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stdlib.h>
#include <conio.h>
#include <iomanip>
#define file_mon_hoc "monhoc.txt"
using namespace std;
typedef struct subject
{
char subject_code[10];
char subject_name[100];
int number_theory_of_subject;
int number_of_subject_practice;
};
void enter_sub(subject list_subject[],int n)
{
fstream myfile;
myfile.open(file_mon_hoc,ios::out|ios::app);
cout << "*****ENTER SUBJECT*****" << endl;
for(int i = 0;i<n;i++)
{
fflush(stdin);
cout << "Enter code : ";
gets(list_subject[i].subject_code);
myfile << list_subject[i].subject_code << endl;
cout << "Enter name: ";
fflush(stdin);
gets(list_subject[i].subject_name);
myfile << list_subject[i].subject_name << endl;
cout << "Enter number of theoretical subjects: ";
fflush(stdin);
cin >> list_subject[i].number_theory_of_subject;
myfile << list_subject[i].number_theory_of_subject << endl;
cout << "Enter number of practicetical subjects: ";
fflush(stdin);
cin >> list_subject[i].number_of_subject_practice;
myfile << list_subject[i].number_of_subject_practice << endl;
}
myfile.close();
}
void output_list_subject(subject list_subject[],int n)
{
cout << "\t\t|----------------------------|" << endl;
cout << "\t\t|******* LIST SUBJECT *******|" << endl;
cout << "\t\t|----------------------------|" << endl;
cout << "|---------------+---------------+"<< "----------------+---------------+" << endl;
cout << "| CODE SUBJECT |"<< " NAME SUBJECT |" << endl;
cout << "|---------------+---------------+"<< "----------------+---------------+" << endl;;
for(int i = 0;i<n;i++)
{
cout <<"| " << setw(20) << list_subject[i].subject_code << setw(5) << "\t|";
cout << setw(24) << list_subject[i].subject_name << "\t |" << endl;
cout << "|---------------+---------------+" << "----------------+---------------+"<< endl;
}
cout << "+---------------+---------------+" << "----------------+---------------+" << endl;
cout << "| THEORY |" << " PRACTICE |" << endl;
cout << "+---------------+---------------+" << "----------------+---------------+" << endl;
for(int i = 0;i<n;i++)
{
cout << "| " << list_subject[i].subject_name << ": " << setw(3) << list_subject[i].number_theory_of_subject << "\t\t| ";
cout << list_subject[i].subject_name << ": " << setw(3) << list_subject[i].number_of_subject_practice << "\t\t |" << endl;
cout << "+---------------+---------------+" << "----------------+---------------+" << endl;
}
}
void output_sub()
{
fstream myfile;
myfile.open(file_mon_hoc,ios::in);
char count1[50];
int flag = 1;
cout << "***** CONTENT FILE ***** " << endl;
cout << "|-----------------------|-----------------------|-----------------------|" << endl;
while(!myfile.eof())
{
myfile.getline(count1,50);
if(flag%4==0)
{
cout << setw(12)<< count1 << setw(12) << endl;
}
else
{
cout << setw(19) << count1 << setw(19);
}
flag++;
}
cout << endl;
cout << "|-----------------------|-----------------------|-----------------------|" << endl;
myfile.close();
}
void update_subject(int n)
{
subject list_subject[100];
char old_sub[30],old_code_sub[30];
char new_sub[30],new_code_sub[30];
fstream myfile;
myfile.open(file_mon_hoc,ios::out|ios::app);
cout << endl;
/*cout << "Enter old subject name: ";
fflush(stdin);
gets(old_sub);
cout << "Enter new subject name: ";
fflush(stdin);
gets(new_sub);*/
cout << "Enter old subject code: ";
fflush(stdin);
gets(old_code_sub);
for(int i = 0;i<n;i++)
{
if(strcmp(list_subject[i].subject_code,old_code_sub)==0)
{
fflush(stdin);
cout << "Enter code : ";
gets(list_subject[i].subject_code);
myfile << list_subject[i].subject_code << endl;
cout << "Enter name: ";
fflush(stdin);
gets(list_subject[i].subject_name);
myfile << list_subject[i].subject_name << endl;
cout << "Enter number of theoretical subjects: ";
fflush(stdin);
cin >> list_subject[i].number_theory_of_subject;
myfile << list_subject[i].number_theory_of_subject << endl;
cout << "Enter number of practicetical subjects: ";
fflush(stdin);
cin >> list_subject[i].number_of_subject_practice;
myfile << list_subject[i].number_of_subject_practice << endl;
}
}
myfile.close();
}
int main()
{
int n;
cout << "Enter number subject: ";
cin >> n;
subject list_subject[100];
enter_sub(list_subject,n);
output_sub();
output_list_subject(list_subject,n);
update_subject(n);
output_sub();
return 0;
}