| | |
load file data into 2D table
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2008
Posts: 6
Reputation:
Solved Threads: 0
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]
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]
C++ Syntax (Toggle Plain Text)
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
•
•
Join Date: Mar 2008
Posts: 6
Reputation:
Solved Threads: 0
Sorry here is the corrected code entry
C++ Syntax (Toggle Plain Text)
#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; }
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
C++ Syntax (Toggle Plain Text)
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? •
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
•
•
•
•
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
C++ Syntax (Toggle Plain Text)
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. •
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
•
•
•
•
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?
C++ Syntax (Toggle Plain Text)
ROWLIST = 0; while(inputFile >> inputArray[ROWLIST][0]) { for ( int col=1; col < COLLIST; col++) inputFile >> inputArray[ROWLIST][col]; ROWLIST++; }
C++ Syntax (Toggle Plain Text)
const int COLLIST = 3; const int MAXNUMROWS = 100; int inputArray[MAXNUMROWS][COLLIST];
![]() |
Similar Threads
- Converting Excel Data to MSAccess .MDB in VB.NET (VB.NET)
- Importing csv file to SQL Server Using VB.Net (VB.NET)
- Vb6 Data Report Repeatedly Shows Previous Report (Visual Basic 4 / 5 / 6)
- load bulk data files into mysql dynamically (Visual Basic 4 / 5 / 6)
- how do i import data from a csv file to my mysql database (MySQL)
- Automate Data from text file into MySql table? (MySQL)
- how VB get data from database randomly (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Can't include libraries, unresolved external errors..
- Next Thread: need help correcting "blackjack" c++ game
Views: 2483 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






