By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,220 Members | 1,723 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,220 IT Pros & Developers. It's quick & easy.

files and directories into an array of arrays

P: n/a
I wish to place all files and directories that are within a user defined
path (on a Linux x86 PC) into some type of array and then examine those
items for the existence of certain charaters such as "*?<>/\|\\" If one
of the chars are found, I'd like to replace it with a "-" and then
commit the change to the actual file system. I know that a scripting
language may be better suited for this, but I'm experimenting with C and
I'm trying to learn how to better use it. Here are my questions:

1. How would I collect all of the file and dir names from a specific path?
2. What type of array should I place them in?

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. 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.

Thanks!!!

Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
hokieghal99 wrote:
I wish to place all files and directories that are within a user defined
path (on a Linux x86 PC) into some type of array and then examine those
items for the existence of certain charaters such as "*?<>/\|\\" If one
of the chars are found, I'd like to replace it with a "-" and then
commit the change to the actual file system. I know that a scripting
language may be better suited for this, but I'm experimenting with C and
I'm trying to learn how to better use it. Here are my questions:

1. How would I collect all of the file and dir names from a specific path?
You would have to use platform specific functions. The _standard_ C++
language has no facilities for or requirements of directories. Ask the
experts in a newsgroup about your platform.

2. What type of array should I place them in? Text is often represented as a array of characters terminated by a
nul character ('\0'). Many of the string functions expect the
terminating null character. See strcpy, strstr, strcat. Also,
beware of the '\' backslash character, since it represents an
"escape" character. The common pitfall is to have a filename of
"any_directory\text.txt", where the '\t' represents a tab character
in C.

If you want multiple text objects, you may want an array of (array
of text), e.g.:
char many_texts[20][128];
char * * p_many_texts;


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. 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.

Thanks!!!

Read the C FAQ below.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Nov 13 '05 #2

P: n/a
"hokieghal99" <ho********@hotmail.com> wrote in message
news:bn**********@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 :-)
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.