Arrays

Hi! I am suppose to read a data file into 2d char array.
The data file is like this:
open meeting work business quiz journal
open meeting work business quiz journal
....
contains 31 rows and 6 columns.
How can achieve this task....
Thx in adv.
d_caesarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

WynCommented:
char** data=new char*[31];
for(int i=0;i<=30;i++)
char* data[i]=new char[6];

ifstream ifile("x.dat");
//read the data from the input file in order

for(int i=0;i<=30;i++)
for(int j=0;j<=5;j++)
while(!ifile.eof())
ifile.getline(data[i][j],100);

for (int i = 0; i <=30; i++)
delete []data[ni];
delete []data;

Regards
Wyn
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WynCommented:
I dash this off ,sorry:) I must go.

See you 1 hour later.
Regards.
Wyn
0
WynCommented:
opps:
char*** data=new char**[31];
for(int s=0;s<=30;s++)
data[s]=new char*[6];
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

WynCommented:
reject me,I must go.
0
WynCommented:
hi,I back home.Here is the code:

char*** data=new char**[31];

for(int s=0;s<=1;s++)
data[s]=new char*[6];

for(int m=0;m<=30;m++)
for(int h=0;h<=5;h++)
data[m][h]=new char;


ifstream ifile("x.txt");
//read the data from the input file in order
for(int i=0;i<=30;i++)
for(int j=0;j<=5;j++)
while(!ifile.eof())
ifile.getline(data[i][j],100);

for(int q = 0; q <=30; q++)
for(int y=0;y<=5;y++)
delete []data[q][y];

for(int a=0;a<=30;a++)
delete []data[a];

delete []data;

 
0
WynCommented:
hi,I back home.Here is the code:

char*** data=new char**[31];

for(int s=0;s<=30;s++)
data[s]=new char*[6];

for(int m=0;m<=30;m++)
for(int h=0;h<=5;h++)
data[m][h]=new char;


ifstream ifile("x.txt");
//read the data from the input file in order
for(int i=0;i<=30;i++)
for(int j=0;j<=5;j++)
while(!ifile.eof())
ifile.getline(data[i][j],100);

for(int q = 0; q <=30; q++)
for(int y=0;y<=5;y++)
delete []data[q][y];

for(int a=0;a<=30;a++)
delete []data[a];

delete []data;

 
0
WynCommented:
why you must use 2d array?
0
WynCommented:
the code before my last comment is the code you should read.
Sorry for this mess,there are some problems.
you'd reject me for this back mess.
Wyn.
0
WynCommented:
Ok,it works on my computer.
How about you d_caesar?
Wyn.
0
d_caesarAuthor Commented:
Doesn;t works!! It gives an output as zzz
Can U please write a little function that outputs it to the screen and test it, in that way I will make sure it comes the same on my screen.

Thx
PS: Why are u using char*** data ???
0
d_caesarAuthor Commented:
I am having memory leaks!!!!!!
0
WynCommented:
>>PS: Why are u using char*** data ???
See this diagram.
data->char**->char*->each string in your file,so data should be char***.

No,there is no leak,use this code:
for example:

int main(int argc, char* argv[])
{
char*** data=new char**[31];

for(int s=0;s<=30;s++)
data[s]=new char*[6];

for(int m=0;m<=30;m++)
for(int h=0;h<=5;h++)
data[m][h]=new char;


ifstream ifile("x.txt");
//read the data from the input file in order
for(int i=0;i<=30;i++)
for(int j=0;j<=5;j++)
data[i][j]="dffd";

for(int q = 0; q <=30; q++)
for(int y=0;y<=5;y++)
delete []data[q][y];

for(int a=0;a<=30;a++)
delete []data[a];

delete []data;
return 0;
}

.......................

Try this code and you will find it works.then just replace the
data[i][j]="fdf" with
getline(data[i][j],100);
Regards.
Wyn










0
LucHoltkampCommented:
You allocate only one byte:
data[m][h] = new char;
later you place a string into it of several bytes:
ifile.getline(data[i][j], 100);
ofcourse this will fail.

Declare a temporary buffer:
char buffer[256];
ifile.getline(buffer, sizeof(buffer));
and then:
data[i][j] = strdup(buffer);

Luc
0
WynCommented:
Luc and d_caesar,yes,it's my negligence,
sorry for this shallow error.
Thanks Luc.
Sorry to d_caesar.
0
d_caesarAuthor Commented:
Ok thx for the help... I haven;t tried your latest version though
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.