adding new data into text file?

Hi, I need help in how do we add new data into a text file. But then the file has only a limit for 20 students. If there is no space for another new student, replacing the No.1 student into the new student detail is required. For example;

In a text file
No student year
1 chris 2nd
2 hanna 1st

The question now is that, I don't know how to enter the new student detail without changing the No.1 student. And if the limit has passed, I don't know how to replace the No.1 student into a new student. Can someone help me?

So far I only managed to write in the No.1 student only into the text file.

cout << "number " << j+1 << endl;

cout << "Enter student name: " << endl;
cin >> student_name;
cout << setw(10) << fixed << student_name << endl;

cout << "Enter student level: ";
cin >> level;


cout << "\nEnter the name of the file you want to create: ";
fflush(stdin);
cin.getline (FileName,20);
ofstream Student(FileName);
{
Student << j+1 << " " << student_name << " " << level << endl;
}
I had tried to do 'for loop' for the entering the new student_name and level, but then, the No.1 student will eventually lost.

What I have done is this
for(j=0;j<20;j++){
cout << "number " << j+1 << endl;

cout << "Enter student name: " << endl;
cin >> student_name;
cout << setw(10) << fixed << student_name << endl;

cout << "Enter student level: ";
cin >> level;
}

Is there any other way to not the data (only apply when there is still space in the text file)?
Correction;
Is there any other way to not lost the data (only apply when there is still space in the text file)?
I don't really understand what you are saying, and I don't really understand why you had to split it up into 3 posts, but couldn't you just put all of the students into an array before writing them to a file, and just write the last 20 of them? If the file is opened in write mode, it should overwrite all previous content so the student list having changed should be a non-issue.
Sure there's definitely a way.... If I understand correctly you only want to put 20 students into a text file, correct?

So if, for some reason, the first time you don't output the full 20 students, you want to append the data with the rest of the students?
Topic archived. No new replies allowed.