Velocity Reviews > How can I know how many elements are there in multi-dimentional array?

# How can I know how many elements are there in multi-dimentional array?

Abby
Guest
Posts: n/a

 09-16-2003
From my code below, it will open a file specified in argv[1], then
read line by line and store each line in an array call IPTemp.
After reading until EOF, it will return array IPTemp to main, which
you'll see a for loop here "for(i=0;i<6;i++)" ... I use 6 here,
assuming there's 6 lines in the file I opened. How can I know exactly

#include <stdio.h>

#define MAXLINES 200
#define MAXLINELENGTH 30

char (*ReadIPFile(int argc, char * argv))[MAXLINELENGTH];

int main(int argc, char * argv[]){
char (*IP)[MAXLINELENGTH];
int i, j;

//take out newline character
for(i=0;i<6;i++){ //<------- problem here
j = strlen(IP[i]);
if (IP[i][j - 1] == '\n') {
IP[i][j - 1] = '\0';
}
}

return 0;
}

char (*ReadIPFile(int argc, char * argv))[MAXLINELENGTH]{

char buff[30];
int count=0, i, j;
static char IPTemp[MAXLINES][MAXLINELENGTH];

printf("Error opening file\n");
exit(1);
}

i = 0;

//store each line to IPTemp[i]
if (IPTemp[i] == NULL){
if(i != 0){
i--;
}
}
else {
i++;
}

}

return IPTemp;

}

Al Bowers
Guest
Posts: n/a

 09-16-2003

Abby wrote:
> From my code below, it will open a file specified in argv[1], then
> read line by line and store each line in an array call IPTemp.
> After reading until EOF, it will return array IPTemp to main, which
> you'll see a for loop here "for(i=0;i<6;i++)" ... I use 6 here,
> assuming there's 6 lines in the file I opened. How can I know exactly
>
>
> #include <stdio.h>
>
> #define MAXLINES 200
> #define MAXLINELENGTH 30
>

Instead of fixing the maxlines and maxstringlength, you would
create a struct like

struct IP
{
char **ip;
unsigned total;
};

Then dynamically allocate the file lines into the struct. Member ip
is an array of strings of the ips and the member total represents the
total lines read and stored in the array. The max quantity of data
stored in the array would be moderated by the available memory.

--
Al Bowers
Tampa, Fl USA
mailto: http://www.velocityreviews.com/forums/(E-Mail Removed) (remove the x)
http://www.geocities.com/abowers822/

Jeff
Guest
Posts: n/a

 09-16-2003

"Abby" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> From my code below, it will open a file specified in argv[1], then
> read line by line and store each line in an array call IPTemp.
> After reading until EOF, it will return array IPTemp to main, which
> you'll see a for loop here "for(i=0;i<6;i++)" ... I use 6 here,
> assuming there's 6 lines in the file I opened. How can I know exactly
>
>
> #include <stdio.h>
>
> #define MAXLINES 200
> #define MAXLINELENGTH 30
>
> char (*ReadIPFile(int argc, char * argv))[MAXLINELENGTH];
>
> int main(int argc, char * argv[]){
> char (*IP)[MAXLINELENGTH];
> int i, j;
>
>
> //take out newline character
> for(i=0;i<6;i++){ //<------- problem here
> j = strlen(IP[i]);

You can use malloc( ) to allocate memory dynamically.

Method 1: Find out the number of lines in the target file, and allocate
memory for "char **line". It hold the char pointers for each line of the
file (a array containing many string address), then you can use for-loop to
read the file and allocate memory for each line everytime.

line[index] = malloc(line_length);

Method 2: Use linked list structure to hold the lines. It is efficient and
good for adding elements at runtime.

--
Jeff
-je6543 at yahoo.com