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

A

Abby

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);
if (IP[j - 1] == '\n') {
IP[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
while (!feof(IPaddr)) {
fscanf(IPaddr, "%s", IPTemp);
if (IPTemp == NULL){
if(i != 0){
i--;
}
}
else {
i++;
}

}
fclose(IPaddr);

return IPTemp;

}
 
A

Al Bowers

Abby said:
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.
 
J

Jeff

Abby said:
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);


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top