TCLUG Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [TCLUG:10354] c question



On Mon, Nov 22, 1999 at 02:19:25PM -0600, bliss wrote:
> i have a file server.list that contains server names
> server1
> server2
> and i need to initialize an array to those strings
> array[0] = "server1";
> array[1] = "server2";
> but i want to do this dynamicly, ie i want this program to read the file and
> assign each string to the array subscript
> array[0] --> server1 while reading the file
> how do i get a line and assign a string to an array
> then move forward in the array and assign the next string to the next space
> in the array?
> 
> any ideas ???

Here's a function that does it:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

char **readList(const char *filename, size_t *numread)
{
    char **listptr = NULL;
    size_t numitems = 0;
    size_t numused = 0;
    char linebuf[512];
    FILE *input;
    int failed = 0;

    input = fopen(filename, "r");
    if (input == NULL)
    {
        failed = 1;
    }
    if (!failed)
    {
        numitems = 32;
        listptr = malloc(numitems * sizeof(char *));
        if (listptr == NULL)
        {
            failed = 1;
        }
    }
    while (!failed && fgets(linebuf, sizeof(linebuf), input) != NULL)
    {
        if ((numused + 1) >= numitems)
        {
            char **tempptr = NULL;

            numitems += (numitems / 2);
            tempptr = realloc(listptr, numitems * sizeof(char *));
            if (tempptr == NULL)
            {
                failed = 1;
                continue;
            }
            else
            {
                numitems = numitems + (numitems / 2);
                listptr = tempptr;
            }
        }
        if (!failed)
        {
            listptr[numused] = strdup(linebuf);
            if (listptr[numused] == NULL)
            {
                failed = 1;
                continue;
            }
        }
        if (!failed)
        {
            ++numused;
        }
    }
    if (!failed)
    {
        assert(numused < numitems);
        listptr[numused] = NULL;
        *numread = numused;
        if ((numused + 1) < numitems)
        {
            listptr = realloc(listptr,
                              (numused + 1) * numitems * sizeof(char *));
        }
        fclose(input);
        return(listptr);
    }
    else
    {
        if (listptr != NULL)
        {
            size_t i = 0;
            for (i = 0; i < (numitems - 1); ++i)
            {
                free(listptr[i]);
            }
            free(listptr);
            listptr = NULL;
        }
        if (input != NULL)
        {
            fclose(input);
        }
        return(NULL);
    }
}

	It does a lot of ammlocing, and may not be exactly what you
want, but you should get the basic idea.

Have fun (if at all possible),
-- 
Its name is Public Opinion.  It is held in reverence. It settles everything.
Some think it is the voice of God.  Loyalty to petrified opinion never yet
broke a chain or freed a human soul.     ---Mark Twain
-- Eric Hopper (hopper@omnifarious.mn.org
                http://ehopper-host105.dsl.visi.com/~hopper) --

PGP signature