943,868 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 641
  • C++ RSS
Sep 12th, 2009
0

Help finding the end of file

Expand Post »
Hello,

To start thanks in advance for any help. Now I will note this question is about homework so I just want help to get started not everything laid out for me. Ok here is the deal. I have a class called StudentArrayV4, it is a dynamic array of pointers. I also have the class Student that actually holds the student information and is pointed to by StudentArrayV4. I added the ability to save the student data as well as load it from a file. I made sure the code worked by using (!infile.eof) as my test to find the end of file. However, i know .eof can be unreliable so I don't want to use it, not to mention I'd probably lose points on my homework too. The way I was required to set it up was by having the ifstream opened in main and then passed by reference to StudentArrayV4 which then eventually passes it to the Student Constructor. Any ideas on how I can know when I hit the end of the file without using .eof Here is the code for my various methods.
C++ Syntax (Toggle Plain Text)
  1. void StudentArrayV4::read (ifstream& infile){
  2. StudentArrayV4 loadedFile;
  3. if (!infile){
  4. cout << "Error no file found!\n";
  5. }
  6. else{
  7. while (!infile.eof )
  8. add(infile);
  9. }
  10. }
  11.  
  12. void StudentArrayV4::add (ifstream & infile){
  13. Student **temp = new Student*[numberOfStudents + physicalArraySize];
  14. for (int j = 0; j < numberOfStudents; j++){
  15. temp[j] = members[j];
  16. }
  17. delete [] members;
  18. physicalArraySize += 3;
  19. members = temp;
  20. members[numberOfStudents] = new Student(infile);
  21. numberOfStudents++;
  22. }
  23.  
  24. Student::Student (ifstream& infile){
  25. char tempName[300];
  26. int length;
  27. infile >> tempName >> idNumber; // loading data from file
  28. for (int j = 0; j < 3; j++){
  29. infile >> scores[j];
  30. }
  31. length = strlen(tempName);
  32. name = new char[length + 1]; // for the null character
  33. strcpy (name, tempName);
  34. calculateAverageAndGrade();
  35. }
Similar Threads
Reputation Points: 27
Solved Threads: 1
Junior Poster in Training
pinsickle is offline Offline
74 posts
since Sep 2009
Sep 12th, 2009
0

Re: Help finding the end of file

Firstly, using the >> method to read the student name assumes every student has a "one word" name, which is a bit unrealistic. If that is the condition of your problem, then consider
C++ Syntax (Toggle Plain Text)
  1. if( infile >> tempName )
  2. {
  3. infile >> idNumber; // loading data from file
  4. for (int j = 0; j < 3; j++){
  5. infile >> scores[j];
  6. }
  7. length = strlen(tempName);
  8. name = new char[length + 1]; // for the null character
  9. strcpy (name, tempName);
  10. calculateAverageAndGrade();
  11. }
When there is no name left in the file, there's no point in attempting to read further data.

But, you'll have to in some manner return an indicator of whether it was a successful read or not. I don't think using the Student constructor for the reading is a good idea.

Perhaps having the void StudentArrayV4::add () method do the actual reading, to temp variables. If successful, store the read values to the next available Student object.

This line
C++ Syntax (Toggle Plain Text)
  1. Student **temp = new Student*[numberOfStudents + physicalArraySize];
confuses me. If you know the number of students, why are you worried about finding end of file? Just loop that number of times when reading. What is physicalArraySize, where does that come from and why are you using it to set number of students in the temp array?
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Sep 12th, 2009
0

Re: Help finding the end of file

Thanks for the help. The names are one word, the professor wanted it way to make it more simplistic. I couldn't have studentArrayV4 read the file as it is a dynamic array of pointers that points to the students as they are created by the Student Constructor. numberOfstudents is a counter, starts at zero and is incremented for every student made. physical size, which is currently set at 3, helps test the size of the array that way if a student is called to be construted and there is not enough room a new larger array can be created filled with the current student info and then the new student can be added. i.e. the array starts blank and has three elements. If a fourth student is called for then a new array is created that has six elements, the data from the 3 element array is copied over and the 3 element array is deleted. Finally the student** (pointer to array of pointers) is redirected to the new array. The process can be repeated as many times as needed.
Reputation Points: 27
Solved Threads: 1
Junior Poster in Training
pinsickle is offline Offline
74 posts
since Sep 2009
Sep 12th, 2009
0

Re: Help finding the end of file

I'm not sure whether this is what you are looking for, but try this:

C++ Syntax (Toggle Plain Text)
  1. infile.seekg(0, std::ios::end);
  2. long fileEnd = infile.tellg();
  3. long counter = 0;
  4. while(counter != fileEnd) {
  5. // do something
  6. ++counter;
  7. } // end while

What's happening here is that you make the "get" pointer to point to the end of your file, and store this position(which will be the size of your file in bytes) in a variable (through tellg() ). Then you can iterate through the file with a simple loop, whilst updating the current position, and check that it hasn't exceeded the end-of-file.

Apart from this, I would suggest you using the concept of "binary" files to store class or structure data. This way, you wouldn't have to mess around with so many arrays and loops.

Hope this helped!

// EDIT: Looks like vManes solved this as I was typing...
Last edited by amrith92; Sep 12th, 2009 at 4:03 am. Reason: Looks like I was too late in aswering :)
Reputation Points: 130
Solved Threads: 22
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008
Sep 12th, 2009
0

Re: Help finding the end of file

wow that is fantastic thanks
Reputation Points: 27
Solved Threads: 1
Junior Poster in Training
pinsickle is offline Offline
74 posts
since Sep 2009

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: Selection Sort
Next Thread in C++ Forum Timeline: How do i create a pointer to a linked list as a member variable?





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


Follow us on Twitter


© 2011 DaniWeb® LLC