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!!! 2 6406
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
"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 :-) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Arjen |
last post by:
Hello,
I want to make a command line application that search for *.htm* files
inside directories and subdirectories.
1.
How can I save all the *.htm* files inside directories and...
|
by: RML |
last post by:
hey guys,
i am looking at this piece of code that lists numbers of files in a
directory. i want to convert it so it lists the files in th directory that
end with .doc. i cant seem to get it to...
|
by: David |
last post by:
Hi all,
I am fairly new to C#. so go easy on me :-)
Anyhow, I have a class file that I have set up properties and a method.
I am calling this class file directly from and aspx.cs file. So...
|
by: Frost |
last post by:
Hi All,
I am a newbie i have written a c program on unix for line by line
comparison for two files now could some one help on how i could do word
by word comparison in case both lines have the...
|
by: rn5a |
last post by:
I have a ListBox which should list all the files & directories that
exist in a particular directory. The problem is I can get the ListBox
to list either all the files or all the directories but not...
|
by: Stephen Howe |
last post by:
Hi
Just going over some grey areas in my knowledge in C++:
1) If I have
const int SomeConst = 1;
in a header file, it is global, and it is included in multiple translations
units, but it...
|
by: supriyamk |
last post by:
Hi,
I am trying to search a directory for subdirectories, recreate the subdirectories elsewhere and copy only certain files from current subdirectory to new subdirectory.
In other words i am...
|
by: aRTx |
last post by:
I have try a couple of time but does not work for me
My files everytime are sortet by NAME.
I want to Sort my files by Date-desc.
Can anyone help me to do it?
The Script
<?
/*
ORIGJINALI
|
by: Tammy |
last post by:
Hello all,
I am wondering what is the best way to declare a struct to be used in
other c and c++ files. Such as for a C API that will be used by
others.
1. Declaring the typedef and the...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |