Our group has been banging our head against the wall in this one and cannot seem to figure it out. Our program is to be able to set up menus (done), allow user(s) to input 'student names' and save them to a data (*.dat) file and add names to the data file later (done), and be able to enter grades at any time. The problem that we are having is that we can enter grades fine IF the program is still running. As soon as we close the program, re-open the program, and then enter new test scores for the students, it clears the previous scores entered in the first program execute. I will paste our block of code (the function we call to do this for us). Note: I am new to the forum so if the code does not look desirable I am very sorry.

void enterQuiz (int quiz)
{	
	nameFile.open("Names.dat", ios::in);
	
	if (nameFile.fail())
	cout << "name file failed to open" <<endl;

	//quizFile.open("quiz.dat", ios::out); 
	nameFile.getline (studentNames[0], nameLnth, '\n');

	for(int i=0; i < 50 && strlen(studentNames[i]) > 0; i++)
	{	
		cout << "Enter quiz # " << (quiz + 1 ) << " for " << studentNames[i] << " " << endl;
	
		cin >> (studentAvgQuiz[i][quiz]);
	
		for(int col=0; col < quizsize; col++)
		{
			quizFile << studentAvgQuiz[i][col] <<' ' ;
		}
		//quizFile << endl;
		nameFile.getline (studentNames[i +1], nameLnth, '\n');

	} //end for loop
	cin.get();
	nameFile.clear(); 
	quizFile.close();
	nameFile.close();
	
}

...Any kind of help or follow-up to help us in getting our data straightened out would be GREATLY appreciated. What we are trying to do is to run the program, enter data, close the program, re-run the program, keep the old data in and add or replace existing data to the quiz.dat file. Thanks for everyone's help! Also note that 'studentNames', 'nameLnth', and 'studentAvgQuiz' are all arrays defined globally in our program

Ancient Dragon commented: Thanks for using code tags correctly +19

when you open the output file add the ios::ate flag so that the open function will move the file pointer to the end of the exting file data after it opens the file. Normally the file pointer is set to the beginning of the file and if you don't use seek() functions to set it to the end then your program will just overwrite whatever is there. The ios::app flag will always move the pointer to the end of the file for each write and the ios::ate does it only when the file is first opened.

quizFile.open("quiz.dat", ios::out | ios::ate);

Hey, thanks a bunch for your quick response to my problem. I appreciate you taking the time to help my group and I out. I tried changing my code where I input the file. The code now reads exactly the same (only I commented out the quizFile.open("quiz.dat", ios::out | ios::ate);
Unfortunately, I still have the same problem. My data file (quiz.dat) has an output that looks like this:

0 0 0 0 5 0
0 0 0 0 5 0
0 0 0 0 5 0

This is what happens when I input a '5' for each name in the quiz #5 of our menu. When I close the program and enter a '4' for each student in quiz #4, the output now looks like this:

0 0 0 4 0 0
0 0 0 4 0 0
0 0 0 4 0 0

As you can see, it wipes out the previous 5's in quiz #5, re-writes them to 0's and enters the newest data (the 4's for quiz #4). My group members and I are really having a tough time with this and really appreciate everyone's help. Thanks to Ancient Dragon for the quick response. Any more help would really be appreciated. Thank you!

I assume you want the file to look like this after entering the 4s

0 0 0 0 5 0
0 0 0 0 5 0
0 0 0 0 5 0
0 0 0 0 4 0
0 0 0 0 4 0
0 0 0 0 4 0

As I said before you have to open the output file with ios::ate or ios::app flag otherwise you will get the behavior you are experiencing. Or, if the file is never closed after calling enterQuiz() then at the beginning of that function call seekp() to move the file pointer to end-of-file before writing to it.

quizFile.seekp(ios_base::end);

Well, actually what I'm looking for in terms of output would be something like this:
1. Run the program and enter 5's for each student for quiz #5 and say 3's for each student for quiz #3. The output will read:

0 0 3 0 5 0
0 0 3 0 5 0
0 0 3 0 5 0

2. Run the program again, and add more scores, but add keep the old scores. For example, I will add 1's in the quiz #1. The output should read:

1 0 3 0 5 0
1 0 3 0 5 0
1 0 3 0 5 0

IF I stay within the program, and do not terminate it, I can get this output. For example, if I open the program, enter the scores, go back to the main menu, go back in and enter more scores, I can get the latter of the output above. However, if I close the program, re-open it and enter data in any one of the quizzes, it wipes everything out and just writes the data for that quiz. I'm sorry if I'm so slow at getting a grasp of what you're trying to say. Programming isn't really my cup of tea but I'm trying my best to figure this out. I really really appreciate your help. Thank you! Any more solutions or do you still think it is what you are telling me to do? I did change that line of code above that reads:
//quizFile.open("quiz.dat", ios::out);
to
quizFile.open("quiz.dat", ios::out | ios::ate)
but still got the same results.

Oh I see. When you first open the program read the output file into studentAvgQuiz array so that array looks just like it when when the program was shut down. Then when you write it out DO NOT open with the flags I mentioned previously -- if the file exists then you want to overwrite any exting data in it.

1. on program startup open the output file for reading and read all data into the array.
2. close the file in #1 above and reopen for output with the ios::trunc flag to delete all data in the file.
3. write the array to the file like you already posted

Wow, you're the man! haha. Thanks a lot for all your help. We got it working now ! Thanks a million I really appreciate you taking the time to help us out. You may be hearing from us again some time in the future as this is only part I of II parts (the other part being due in early December.) Thanks again for all your help I really really appreciate it!

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.