3 Replies - 553 Views - Last Post: 18 May 2011 - 09:22 PM Rate Topic: -----

#1 BabySeal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 18-May 11

Finding end of an array after being read from a text file

Posted 18 May 2011 - 08:03 PM

So I'm currently writing a supposedly simple database, but I've been having problems with the display portion of it, because I have an array that is defined with 55 elements, but I have to keep 5 places free for input later by the teacher. If any of you know about Pokemon, it is a simple Pokemon database.
my function here:
void listdisplay(int numrecords[55][7] , string stringrecords[55][2])
{
     fstream file;
     int length;
     file.seekg(0 , ios::end);
     length = file.tellg();
     file.seekg(0 , ios::beg);
     for (int i = 0; i < length; i += 1) {
           cout<< setw(3) << numrecords[i][0] << setw(12) << stringrecords[i][0];
           cout<< setw(12) << stringrecords[i][1] <<endl;
     }
}

is what I had come up with after:
void listdisplay(int numrecords[55][7] , string stringrecords[55][2])
{
     for (int i = 0; i < 55; i += 1) {
           cout<< setw(3) << numrecords[i][0] << setw(12) << stringrecords[i][0];
           cout<< setw(12) << stringrecords[i][1] <<endl;
     }
}

The second thing will, for some reason, have random zeros after 50, as there was no data read after that, and suddenly loops again, and prints it one more time.
The top thing just doesn't even print out the array

Just as clarification,
numrecords[i][0] is the Pokemon number
stringrecords[i][0] is the Pokemon name
and stringrecords[i][1] is the Pokemon type


All in all, is there a way to find the end of an array, because reading the file till end of file is just a conditional loop with
!file.eof()
!file.eof() as the condition.

Sorry for the long post :P

Is This A Good Question/Topic? 0
  • +

Replies To: Finding end of an array after being read from a text file

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Finding end of an array after being read from a text file

Posted 18 May 2011 - 08:27 PM

What does that fstream file have to do with the length of your array? It's just an fstream object that isn't even associated with an actual file. So it serves no purpose here whatsoever.

And of course the end of the numrecords array will contain random garbage, because you didn't assign any values to the last 5 elements. (Any unassigned elements of the stringrecords array will contain empty strings.)

Are you reading data into the arrays from a file someplace else in your code? If so, you can create a counter variable in that function to keep a record of how many entries were read from the file, and then pass that value as an argument to the listdisplay function.
Was This Post Helpful? 1
  • +
  • -

#3 BabySeal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 18-May 11

Re: Finding end of an array after being read from a text file

Posted 18 May 2011 - 08:35 PM

View Postr.stiltskin, on 18 May 2011 - 08:27 PM, said:

What does that fstream file have to do with the length of your array? It's just an fstream object that isn't even associated with an actual file. So it serves no purpose here whatsoever.

And of course the end of the numrecords array will contain random garbage, because you didn't assign any values to the last 5 elements. (Any unassigned elements of the stringrecords array will contain empty strings.)

Are you reading data into the arrays from a file someplace else in your code? If so, you can create a counter variable in that function to keep a record of how many entries were read from the file, and then pass that value as an argument to the listdisplay function.


thanks! :D
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3033
  • View blog
  • Posts: 10,580
  • Joined: 08-August 08

Re: Finding end of an array after being read from a text file

Posted 18 May 2011 - 09:22 PM

Of course if you used a vector you could just push elements onto it and then get the vector size to see how many you've read in when you've reached the end of the file.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1