Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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
how many elements in my array? Please help...thanks a lot.


#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;

IP = ReadIPFile(argc, argv[1]);

//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]{

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

IPaddr = fopen(argv, "r");

if(!IPaddr){
printf("Error opening file\n");
exit(1);
}

i = 0;

//store each line to IPTemp[i]
while (!feof(IPaddr)) {
fscanf(IPaddr, "%s", IPTemp[i]);
if (IPTemp[i] == NULL){
if(i != 0){
i--;
}
}
else {
i++;
}

}
fclose(IPaddr);

return IPTemp;

}
 
Reply With Quote
 
 
 
 
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
> how many elements in my array? Please help...thanks a lot.
>
>
> #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: (remove the x)
http://www.geocities.com/abowers822/

 
Reply With Quote
 
 
 
 
Jeff
Guest
Posts: n/a
 
      09-16-2003

"Abby" <> wrote in message
news: 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
> how many elements in my array? Please help...thanks a lot.
>
>
> #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;
>
> IP = ReadIPFile(argc, argv[1]);
>
> //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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How many elements are there in the array. hara Perl Misc 34 05-27-2006 08:11 PM
How do free() know how many elements should be freed in a dynamic array? lovecreatesbeauty C Programming 13 01-15-2006 03:58 PM
How does free() know how many elements should be freed in a dynamic array? lovecreatesbeauty C Programming 2 01-13-2006 04:59 PM
I know, I know, I don't know Andries Perl Misc 3 04-23-2004 02:17 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57