Help with holding data input to a file

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2007
Posts: 4
Reputation: corruptsystem is an unknown quantity at this point 
Solved Threads: 0
corruptsystem corruptsystem is offline Offline
Newbie Poster

Help with holding data input to a file

 
1
  #1
Oct 17th, 2007
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.

  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,502
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1479
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help with holding data input to a file

 
0
  #2
Oct 17th, 2007
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.
  1. quizFile.open("quiz.dat", ios::out | ios::ate);
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 4
Reputation: corruptsystem is an unknown quantity at this point 
Solved Threads: 0
corruptsystem corruptsystem is offline Offline
Newbie Poster

Re: Help with holding data input to a file

 
0
  #3
Oct 17th, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,502
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1479
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help with holding data input to a file

 
0
  #4
Oct 17th, 2007
I assume you want the file to look like this after entering the 4s
  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.
  1. quizFile.seekp(ios_base::end);
Last edited by Ancient Dragon; Oct 17th, 2007 at 11:07 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 4
Reputation: corruptsystem is an unknown quantity at this point 
Solved Threads: 0
corruptsystem corruptsystem is offline Offline
Newbie Poster

Re: Help with holding data input to a file

 
0
  #5
Oct 17th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,502
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1479
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help with holding data input to a file

 
0
  #6
Oct 17th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 4
Reputation: corruptsystem is an unknown quantity at this point 
Solved Threads: 0
corruptsystem corruptsystem is offline Offline
Newbie Poster

Re: Help with holding data input to a file

 
0
  #7
Oct 17th, 2007
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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC