load file data into 2D table

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 6
Reputation: nubicoder is an unknown quantity at this point 
Solved Threads: 0
nubicoder nubicoder is offline Offline
Newbie Poster

load file data into 2D table

 
0
  #1
Apr 30th, 2008
Hello,

I am trying to load data from a testfile.txt file which contains ten records and 3 fields with numbers into a 2 dimensional table and then print it.

I am not sure if I am accesing the data correctly and placing it into the array. I get no output.


first time posting hope I enclosed the code properly


Would appreciate any help,

Thanks
Nu


Here is the code:

[code language=c++]

#include "stdafx.h"
#include <fstream>
using std::ifstream;
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::cerr;
using std::ios;

#include <cstdlib>
using std::exit;
using namespace std;



int _tmain(int argc, _TCHAR* argv[])
{

const int ROWLIST=10;
const int COLLIST=3;

int inputARRAY[ROWLIST][COLLIST]={0};

ifstream inputFile( "textfile.txt", ios::in);
if ( !inputFile )
{
cerr << "file could not be opened" << endl;
exit(1);
} // end if
int row=0;
int col=0;
while( !inputFile.eof() )
{
for ( row=0; row < ROWLIST; row++)
for ( col=0; col < COLLIST; col++)
inputARRAY[row][col];
}

for ( int r=0; r< ROWLIST; r++)
for ( int c=0;c<COLLIST;c++)
cout << inputARRAY[r][c] << endl;


return 0;
}
[/code]
  1. textfile.txt
  2.  
  3. 12 72 87
  4. 11 71 81
  5. 6 70 78
  6. 19 70 89
  7. 38 71 109
  8. 35 72 110
  9. 1 72 73
  10. 8 72 81
  11. 9 72 83
  12. 5 71 76
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 6
Reputation: nubicoder is an unknown quantity at this point 
Solved Threads: 0
nubicoder nubicoder is offline Offline
Newbie Poster

Re: load file data into 2D table

 
0
  #2
Apr 30th, 2008
Sorry here is the corrected code entry

  1.  
  2. #include "stdafx.h"
  3. #include <fstream>
  4. using std::ifstream;
  5. #include <iostream>
  6. using std::cout;
  7. using std::cin;
  8. using std::endl;
  9. using std::cerr;
  10. using std::ios;
  11.  
  12. #include <cstdlib>
  13. using std::exit;
  14. using namespace std;
  15.  
  16.  
  17.  
  18. int _tmain(int argc, _TCHAR* argv[])
  19. {
  20.  
  21. const int ROWLIST=10;
  22. const int COLLIST=3;
  23.  
  24. int inputARRAY[ROWLIST][COLLIST]={0};
  25.  
  26. ifstream inputFile( "textfile.txt", ios::in);
  27. if ( !inputFile )
  28. {
  29. cerr << "file could not be opened" << endl;
  30. exit(1);
  31. } // end if
  32. int row=0;
  33. int col=0;
  34. while( !inputFile.eof() )
  35. {
  36. for ( row=0; row < ROWLIST; row++)
  37. for ( col=0; col < COLLIST; col++)
  38. inputARRAY[row][col];
  39. }
  40.  
  41. for ( int r=0; r< ROWLIST; r++)
  42. for ( int c=0;c<COLLIST;c++)
  43. cout << inputARRAY[r][c] << endl;
  44.  
  45.  
  46. return 0;
  47. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: load file data into 2D table

 
0
  #3
Apr 30th, 2008
  1. while( !inputFile.eof() )
  2. {
  3. for ( row=0; row < ROWLIST; row++)
  4. for ( col=0; col < COLLIST; col++)
  5. inputFile >> inputARRAY[row][col];
  6. }

I believe you need to add inputFile >> to line 5 above, as I have done. On a side note, you have a while loop as well as a nested for loop when reading in the data. If you know ahead of time how much data you are going to read, do you need the while loop?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 6
Reputation: nubicoder is an unknown quantity at this point 
Solved Threads: 0
nubicoder nubicoder is offline Offline
Newbie Poster

Re: load file data into 2D table

 
0
  #4
Apr 30th, 2008
Actually, I would not normally know so do I only use the while loop. If so then how do I assign the value to the table.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: load file data into 2D table

 
0
  #5
Apr 30th, 2008
Originally Posted by nubicoder View Post
Actually, I would not normally know so do I only use the while loop. If so then how do I assign the value to the table.

Thanks
I was thinking that if you DO know the number of columns and the number of rows, you can keep the for-loops and get rid of the while loop. If you know how many columns, but don't know how many rows, you can keep one of the for loops and keep the while loop and do something like this:

  1. ROWLIST = 0;
  2. while( !inputFile.eof() )
  3. {
  4. for ( col=0; col < COLLIST; col++)
  5. inputFile >> inputARRAY[ROWLIST][col];
  6.  
  7. ROWLIST++;
  8. }

You do have the potential problem where the program may try to go through the while loop one too many times because inputFile.eof () could return false even when all of the data has been read already. If that happens, let us know, as your while loop could be constructed slightly different to fix that problem.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 6
Reputation: nubicoder is an unknown quantity at this point 
Solved Threads: 0
nubicoder nubicoder is offline Offline
Newbie Poster

Re: load file data into 2D table

 
0
  #6
Apr 30th, 2008
Thanks again.

Well, honestly I'd like to learn the best method that's normally used for this. Can you show me how to construct this correctly?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: load file data into 2D table

 
0
  #7
Apr 30th, 2008
Originally Posted by nubicoder View Post
Thanks again.

Well, honestly I'd like to learn the best method that's normally used for this. Can you show me how to construct this correctly?
Here is one possible way that avoids the problem of the eof function not turning true fast enough. Simply don't use it. The eof function doesn't return true until you've tried to read something and failed, which often isn't soon enough if you aren't careful. This solution avoids that:

  1. ROWLIST = 0;
  2. while(inputFile >> inputArray[ROWLIST][0])
  3. {
  4. for ( int col=1; col < COLLIST; col++)
  5. inputFile >> inputArray[ROWLIST][col];
  6.  
  7. ROWLIST++;
  8. }
When you run out of data and try to read more in on line 2, that input read attempt will fail and bail you out of the while loop so you don't go through one too many times. Earlier, when you declare the array, try to determine the maximum number of rows it could possibly be and make the array that size so you can't potentially run out of room, like this:
  1. const int COLLIST = 3;
  2. const int MAXNUMROWS = 100;
  3. int inputArray[MAXNUMROWS][COLLIST];
If you have no idea what the maximum number of rows is, I'd use a vector of type int[COLLIST] instead of a 2-D integer array, though there are ways to use a 2-D array and resize it. I just prefer to use vectors in cases like that since the resizing is done for you by C++.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 6
Reputation: nubicoder is an unknown quantity at this point 
Solved Threads: 0
nubicoder nubicoder is offline Offline
Newbie Poster

Re: load file data into 2D table

 
0
  #8
Apr 30th, 2008
Thanks VernonDozier!

I'll give that a try.

Nu
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 6
Reputation: nubicoder is an unknown quantity at this point 
Solved Threads: 0
nubicoder nubicoder is offline Offline
Newbie Poster

Re: load file data into 2D table

 
0
  #9
May 1st, 2008
Thanks very much VernonDozier

Both solutions work well for me. Now I have two methods I can use

Nu
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2483 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC