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

Discussion in 'C Programming' started by Abby, Sep 16, 2003.

  1. Abby

    Abby Guest

    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;

    }
    Abby, Sep 16, 2003
    #1
    1. Advertising

  2. Abby

    Al Bowers Guest

    Re: How can I know how many elements are there in multi-dimentionalarray?

    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/
    Al Bowers, Sep 16, 2003
    #2
    1. Advertising

  3. Abby

    Jeff Guest

    "Abby" <> wrote in message
    news:...
    > 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.


    --
    Jeff
    -je6543 at yahoo.com
    Jeff, Sep 16, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Willem Oosthuizen
    Replies:
    1
    Views:
    2,773
    Jonathan Bromley
    Jul 9, 2003
  2. Michael Neuling
    Replies:
    4
    Views:
    2,895
    Michael Neuling
    Nov 27, 2003
  3. lovecreatesbeauty
    Replies:
    13
    Views:
    578
    Joe Wright
    Jan 15, 2006
  4. NvrBst
    Replies:
    3
    Views:
    427
    NvrBst
    Nov 13, 2008
  5. Joan Gu

    multi-dimentional array

    Joan Gu, Mar 25, 2008, in forum: Ruby
    Replies:
    5
    Views:
    124
    Paul Mckibbin
    Mar 27, 2008
Loading...

Share This Page