Re: files and directories into an array of arrays

From: Peter Pichler (pichlo6_at_pobox.sk)
Date: 10/31/03


Date: Fri, 31 Oct 2003 19:37:22 -0000


"hokieghal99" <hokiegal99@hotmail.com> wrote in message
news:bntu7d$5i2$1@solaris.cc.vt.edu...
> 1. How would I collect all of the file and dir names from a specific path?

There is no such thing in standard C. Standard C recognizes files, but knows
nothing about directories. To find out how to walk a directory, check the
programer's documentation for the target OS or ask in an OS specific
newsgroup.
This is off-topic on comp.lang.c, I'm afraid.

> 2. What type of array should I place them in?

Now this is topical. If you want a quick and dirty solution and you know the
maximum length of a filename (e.g. FILENAME_MAX, #defined in stdio.h), you
can use an array of arrays of char:

#define MAXIMUM_DODGY_NUMBER_OF_FILENAMES 42
char array_of_filenames[MAXIMUM_DODGY_NUMBER_OF_FILENAMES][FILENAME_MAX];

Please note that this method is:
a) Wsteful, because you always have to allocate space for the worst case,
   i.e. for the maximum possible number of filenames, each of them of the
   maximum length.
b) Ureliable, because you can never be sure what the maximum would be. You
   may easily waste space for 42 strings 256 characters long, only to find
   out that you in fact need 43 strings, each only 5 chars long.

A much better solution would be to use some dynamic structure, for example
a linked list:

struct filename
{
  char string[FILENAME_MAX];
  struct filename * next;
};

Then you would need to allocate a new struct filename for every filename
you find and link it with the already existing list through the next field.
Look up malloc on memory allocation and the whole chapter about pointers in
your C tutorial.

While doing so, you will probably learn enough to realize that having a
fixed
size array in struct filename is wasteful and that you can easily replace it
with something like:

struct filename
{
  char * string;
  struct filename * next;
};

The memory image of filenames in a list like this would look like this:

head -> filename1 +--> filename2 +--> ....
         | | | | | |
         v v | v v |
      string next--+ string next--+

The last element in the list will have its next field equal NULL. You also
need head of type struct filename *, pointing to the first element of the
list.

I am sure that your book about data structures (you got one, right?)
describes all this better and in more detail than I do, so I do not know
why I bother, oh I am so depressed...

> Here is why I ask the second question. I know that C has no built-in
> type for strings. I know that it uses a char array to make a string.

Excellent, you have done a part of your homework already :-)

> So,
> a filename would be a char array (string), right? This is where I do not
> understand how to create an array of char arrays (strings). Any
> pointers? I am new at C, so forgive my ignorance.

Let's declare a variable x of a certain type, e.g. char:

char x;

Now, to declare an array of such type, we need to specify how many elements
of such array we want to allocate. For that, we use brackets []:

char x[size];

See the pattern? By adding [size] we declare an array. Each element of the
array is char x. If we add another pair of brackets, like this:

char x[size][number];

what do we get? An array of char x[size], i.e. an array of number of arrays
of char. You can add more pairs of brackets (up to the limit of 12 in the
new standard, C99, I don't know what the limit was in C89).

I am sure that pedants in this group will beat me on my head for this kind
of explanation, but I believe (hope?) that it should be good enough for a
newbie :-)



Relevant Pages

  • Re: two dimensional arrays passed to functions
    ... > array and then send it down as a single dimmensional array. ... x is an array of char pointers. ... to a pointer to the first element of this array, ... need to copy the strings and not just assign pointers to them). ...
    (comp.lang.c)
  • Re: Help - JNI and JMS - data conversion problems
    ... DataOutputStream in a raw byte array. ... Note when you do this your Strings will be counted UTF-8 format. ... You could write them an char[] if you wanted the simplicity of 16-bit ...
    (comp.lang.java.programmer)
  • Re: a string, a string array and character array
    ... which defined its "char" to be in the range -128 to 127. ... with strings, like sorting. ... A character array is an array of character type; ...
    (comp.soft-sys.matlab)
  • Re: Returning an array of strings in C
    ... fucntion and return char** type. ... you can't return an array from a C function. ... Now for strings it gets a bit more ... take when allocating memory dynamically. ...
    (comp.lang.c)
  • Re: Window Management
    ... (* prompt is a string resource identifier specifying the string ... PROCEDURE YesNoCancel(prompt: ARRAY OF CHAR; ... VAR INOUT response: ARRAY OF CHAR): BOOLEAN; ...
    (comp.lang.ada)