944,051 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1128
  • C++ RSS
Oct 17th, 2007
1

Help with holding data input to a file

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. void enterQuiz (int quiz)
  2. {
  3. nameFile.open("Names.dat", ios::in);
  4.  
  5. if (nameFile.fail())
  6. cout << "name file failed to open" <<endl;
  7.  
  8. //quizFile.open("quiz.dat", ios::out);
  9. nameFile.getline (studentNames[0], nameLnth, '\n');
  10.  
  11. for(int i=0; i < 50 && strlen(studentNames[i]) > 0; i++)
  12. {
  13. cout << "Enter quiz # " << (quiz + 1 ) << " for " << studentNames[i] << " " << endl;
  14.  
  15. cin >> (studentAvgQuiz[i][quiz]);
  16.  
  17. for(int col=0; col < quizsize; col++)
  18. {
  19. quizFile << studentAvgQuiz[i][col] <<' ' ;
  20. }
  21. //quizFile << endl;
  22. nameFile.getline (studentNames[i +1], nameLnth, '\n');
  23.  
  24. } //end for loop
  25. cin.get();
  26. nameFile.clear();
  27. quizFile.close();
  28. nameFile.close();
  29.  
  30. }
...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
Similar Threads
Reputation Points: 29
Solved Threads: 0
Newbie Poster
corruptsystem is offline Offline
4 posts
since Oct 2007
Oct 17th, 2007
0

Re: Help with holding data input to a file

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.
C++ Syntax (Toggle Plain Text)
  1. quizFile.open("quiz.dat", ios::out | ios::ate);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Oct 17th, 2007
0

Re: Help with holding data input to a file

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!
Reputation Points: 29
Solved Threads: 0
Newbie Poster
corruptsystem is offline Offline
4 posts
since Oct 2007
Oct 17th, 2007
0

Re: Help with holding data input to a file

I assume you want the file to look like this after entering the 4s
C++ Syntax (Toggle Plain Text)
  1. 0 0 0 0 5 0
  2. 0 0 0 0 5 0
  3. 0 0 0 0 5 0
  4. 0 0 0 0 4 0
  5. 0 0 0 0 4 0
  6. 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.
C++ Syntax (Toggle Plain Text)
  1. quizFile.seekp(ios_base::end);
Last edited by Ancient Dragon; Oct 17th, 2007 at 11:07 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Oct 17th, 2007
0

Re: Help with holding data input to a file

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.
Last edited by corruptsystem; Oct 17th, 2007 at 11:16 pm.
Reputation Points: 29
Solved Threads: 0
Newbie Poster
corruptsystem is offline Offline
4 posts
since Oct 2007
Oct 17th, 2007
0

Re: Help with holding data input to a file

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
Last edited by Ancient Dragon; Oct 17th, 2007 at 11:24 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Oct 17th, 2007
0

Re: Help with holding data input to a file

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!
Reputation Points: 29
Solved Threads: 0
Newbie Poster
corruptsystem is offline Offline
4 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Filename as a constant?
Next Thread in C++ Forum Timeline: Template coding





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC