Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 dimensional array

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
hi all,


I am having difficulty getting C++ to read
data into a char type 2 dimensional array from a "Data.text" file.I have work with one dimensional array before but not a two dimensional.So its new to me. The data file looks like this,
------------------------------------------------------

T1 T2 T3 T4 T5 T6 T7 T8

Gigalo, Donald 8 9 10 7 9 82 76 72
Lennon, John 6 8 9 7 8 68 74 83
Garriss, Tom 10 10 10 10 10 100 100 100
Marriss, Nick 9 8 9 8 9 90 80 90
Ivano, Erikas 6 5 4 0 0 65 58 34

-------------------------------------------------------
1.The first line from T1 all the way right to T8, I am able to get C++ to read it into char T[80]; successfully.

2. Now comes the problem. The problem I am having is getting C++ to read the last name Gigalo and all the names on that row down to Ivan and store it into a char type two dimension array.
I dont know if Im doing it right or what, but when I c-out it keeps printing out junk characters.

This is the code Im using to get C++ to read just the lastnames in and printing it out on screen.
3.What did I do wrong? please help.I have takin like a bottle of Advil now trying to alleviate my head-ache. Thnkx in advance.
-------------------------------------------------------
void main ()
{
ifstream Infile("data.txt");
char Lastname[9][10];

while(Infile)
{
for(int row=0;row<9;row++)
{
Infile.getline(Lastname[row],10,'\n');
}
}
for(int count1=0;count1<9;count1++)
{
for(int count2=0;count2<10;count2++)
{
cout<<Lastname[count1][count2];
}
}

}
------------------------------------------------

 
what about using CArray ? (browse msdn;-) or try&ask more.
bubak
 
Hi C-saw,

there's several things to think about here. First of all testing InFile with while() doesn't make too much sense. This just tests that the file open succeeded, and you need to do it just once. OTOH, in your loop you need to test for two conditions, EOF and max lines exceeded. The central problem is however that istream::getline() with count is not intended to read part of a line, but to set a maximum limit corresponding to the length of the buffer you are reading into. In fact you get failbit set after the first call, which is why the rest is garbage. Better than just testing for eof is to test for good(), i.e. not eof and file is OK. Here is a slightly rewritten version of your code (some of the changes just for my own convenience):

#include &quot;stdafx.h&quot;
#include <iostream>
#include <fstream>
using namespace std;

const int rows = 3;

void prtline(char* cp,int l)
{
for(int i=0;i<l;i++) cout<< cp;
cout << endl;
}

int main(int argc, char* argv[])
{
ifstream Infile(&quot;c:\\xyz.txt&quot;);
char Lastname[rows][10];
char line[100];
int row = -1;
if(Infile)
{
while(Infile.good())
{
if (++row==rows) break;
Infile.getline(line,100,'\n');
strncpy(Lastname[row],line,10);
}
for(int i=0;i<rows;i++) prtline(Lastname,10);
}
else
MessageBox(NULL,&quot;Can't open file!&quot;,&quot;Error&quot;,0);

}

So anyway, I just read each full line into a buffer and copy the bit I need into the array. BTW, you could leave the &quot;,'\n'&quot; off getline(), it's the default value anyway. Hope this helps you in some way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top