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:

#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;
}
textfile.txt

12 72 87
11 71 81
6 70 78
19 70 89
38 71 109
35 72 110
1 72 73
8 72 81
9 72 83
5 71 76

Sorry here is the corrected code entry

#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;
}
while( !inputFile.eof() )
{
     for ( row=0; row < ROWLIST; row++)
          for ( col=0; col < COLLIST; col++)
               inputFile >> inputARRAY[row][col];
}

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?

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

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:

ROWLIST = 0;
while( !inputFile.eof() )
{
          for ( col=0; col < COLLIST; col++)
               inputFile >> inputARRAY[ROWLIST][col];

          ROWLIST++;
}

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.

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?

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:

ROWLIST = 0;
     while(inputFile >> inputArray[ROWLIST][0])
     {
          for ( int col=1; col < COLLIST; col++)
               inputFile >> inputArray[ROWLIST][col];
                    
          ROWLIST++;     
     }

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:

const int COLLIST = 3;
const int MAXNUMROWS = 100;
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++.

Thanks VernonDozier!

I'll give that a try.

Nu

Thanks very much VernonDozier

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

Nu

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.