Help finding the end of file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2009
Posts: 10
Reputation: pinsickle is an unknown quantity at this point 
Solved Threads: 0
pinsickle pinsickle is offline Offline
Newbie Poster

Help finding the end of file

 
0
  #1
Sep 12th, 2009
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.
  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Help finding the end of file

 
0
  #2
Sep 12th, 2009
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
  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
  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?
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: pinsickle is an unknown quantity at this point 
Solved Threads: 0
pinsickle pinsickle is offline Offline
Newbie Poster

Re: Help finding the end of file

 
0
  #3
Sep 12th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 155
Reputation: amrith92 is on a distinguished road 
Solved Threads: 18
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

Re: Help finding the end of file

 
0
  #4
Sep 12th, 2009
I'm not sure whether this is what you are looking for, but try this:

  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 :)
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: pinsickle is an unknown quantity at this point 
Solved Threads: 0
pinsickle pinsickle is offline Offline
Newbie Poster

Re: Help finding the end of file

 
0
  #5
Sep 12th, 2009
wow that is fantastic thanks
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC