• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

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.
0
d_caesar
Asked:
d_caesar
  • 11
  • 3
1 Solution
 
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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 11
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now