How to know how many files are into a directory

A

Antonio Maschio

Hi, I need help. I want to read text files contained into a directory,
but my program is unaware of how many files are contained into.

In bash there's something similar to (figure out)

for i in <dir> do
....
done

but I don't know how to do the same in C. Can you show me snippets of code?

-- Antonio
 
R

Richard Heathfield

Antonio Maschio said:
Hi, I need help. I want to read text files contained into a directory,
but my program is unaware of how many files are contained into.

http://c-faq.com/ - see question 19.20.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
S

santosh

Antonio said:
Hi, I need help. I want to read text files contained into a directory,
but my program is unaware of how many files are contained into.

In bash there's something similar to (figure out)

for i in <dir> do
...
done

but I don't know how to do the same in C. Can you show me snippets of code?

Standard C doesn't even have the concept of files or directories, so
the best manner to do what you're asking is to use the file management
functions offered by your C library or, failing that, your OS.
Functions defined by POSIX are widely implemented under most modern
OSes. You can use these routines to read the files in a directory by
name and then try to access each one.

The exact method often varies according to your platform. Ask for more
detail in an appropriate group. This group deals only with standard C.
 
M

Mark McIntyre

Hi, I need help. I want to read text files contained into a directory,
but my program is unaware of how many files are contained into.

C doesn't have any general functions for handling directories. Of
course most platforms do have system-specific routines for doing this,
so you need to ask in a group specialising in your platform.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
F

Flash Gordon

Antonio said:
Hi, I need help. I want to read text files contained into a directory,
but my program is unaware of how many files are contained into.

In bash there's something similar to (figure out)

for i in <dir> do
...
done

but I don't know how to do the same in C. Can you show me snippets of code?

You can't do it in standard C. I'm guessing you are probably using a
Unix like system, probably Linux, so I suggest asking in
comp.unix.programmer, but read their FAQ first and search the archives
since this has almost certainly been asked before.

<OT>"man opendir" might point you in the right direction</OT>

Before you ask, OT means Off Topic, i.e. please don't discuss it further
here, instead take it where it is topical such as comp.unix.programmer
in this case.
 
S

santosh

Keith said:
santosh said:
Standard C doesn't even have the concept of files or directories,
[...]

Well, it does have the concept of files.

Not files as most people would likely think of them, (i.e. disk files).
It uses the terms 'stream' and 'FILE' to refer to a channel of data
exchange with an I/O device in the former case, and in the latter case,
the name of the principal metadata structure used to manage such
streams.

Please do correct this if there is a mistake. Right now, I'm too lazy
to look it up in the standard.
 
B

Ben Bacarisse

santosh said:
Keith said:
santosh said:
Standard C doesn't even have the concept of files or directories,
[...]

Well, it does have the concept of files.

Not files as most people would likely think of them, (i.e. disk files).
It uses the terms 'stream' and 'FILE' to refer to a channel of data
exchange with an I/O device in the former case, and in the latter case,
the name of the principal metadata structure used to manage such
streams.

Please do correct this if there is a mistake. Right now, I'm too lazy
to look it up in the standard.

It (ISO C) has a little more than that in that files have names (by
which they can be opened, of course) and can be renamed. They can
also be removed. New, unique, names can also be generated.
 
K

Keith Thompson

santosh said:
Keith said:
santosh said:
Standard C doesn't even have the concept of files or directories,
[...]

Well, it does have the concept of files.

Not files as most people would likely think of them, (i.e. disk files).
It uses the terms 'stream' and 'FILE' to refer to a channel of data
exchange with an I/O device in the former case, and in the latter case,
the name of the principal metadata structure used to manage such
streams.

Please do correct this if there is a mistake. Right now, I'm too lazy
to look it up in the standard.

C99 7.19.2 discusses streams. C99 7.19.3 discusses files. Look it up
when you're less lazy. :cool:}
 
R

Richard Heathfield

santosh said:
Keith said:
santosh said:
Standard C doesn't even have the concept of files or directories,
[...]

Well, it does have the concept of files.

Not files as most people would likely think of them, (i.e. disk files).

Ahem! :) I don't see what disks (or discs, in the case of CDs) have to do
with it. I've used many a tape file in my time. And my C programs, bless
them, didn't care - a file is a file, and who cares what medium is used to
store it?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
B

Becker

Antonio said:
Hi, I need help. I want to read text files contained into a directory,
but my program is unaware of how many files are contained into.

In bash there's something similar to (figure out)

for i in <dir> do
...
done

but I don't know how to do the same in C. Can you show me snippets of code?

-- Antonio

Hi, there. I guess it's not neccessary for me to explain too much. The
code example shows how to get to know the contents of the directory.

/*==================BEGIN=================*/
/*
* Name:
* list_v01.c - version 0.1 of list
* Function:
* List directory.
*/
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h> // for Directory Operations

int
main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;

if (argc == 1) // if there is only 1 argument
dp = opendir (".");
else if (argc == 2) { // if there are 2 arguments
if ((dp = opendir(argv[1])) == NULL) {
printf("Can't open %s\n", argv[1]);
return EXIT_FAILURE;
}
}
else { // if there are more than 2 arguments
printf("Usage: %s [DIR]\n", argv[0]);
return EXIT_FAILURE;
}


while ((dirp = readdir(dp)) != NULL)
printf ("%s\n", dirp -> d_name);

if (closedir(dp) == -1 ) {
printf("Error in closing directory.");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*===================END===================*/

You can do some directory operations with "dirent.h" header file. The
dirent struct inlcudes three attributes of the files contained in the
specified directory. You can get to know more about that struct in
"bit/dirent.h".

After you know how to read the directory, I guess you can do some
further operations to implement your goal.

I hope this will help a little.


Weichao Liu
===================================
Students' Robotics Lab, CS Dept.
Huai Hai Institute of Technology
===================================
 
B

Bill Pursell

Becker said:
Antonio said:
Hi, I need help. I want to read text files contained into a directory,
but my program is unaware of how many files are contained into.

In bash there's something similar to (figure out)

for i in <dir> do
...
done

but I don't know how to do the same in C. Can you show me snippets of code?

-- Antonio

Hi, there. I guess it's not neccessary for me to explain too much. The
code example shows how to get to know the contents of the directory.

/*==================BEGIN=================*/
/*
* Name:
* list_v01.c - version 0.1 of list
* Function:
* List directory.
*/
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h> // for Directory Operations

int
main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;

if (argc == 1) // if there is only 1 argument
dp = opendir (".");
else if (argc == 2) { // if there are 2 arguments
if ((dp = opendir(argv[1])) == NULL) {
printf("Can't open %s\n", argv[1]);
return EXIT_FAILURE;
}
}
else { // if there are more than 2 arguments
printf("Usage: %s [DIR]\n", argv[0]);
return EXIT_FAILURE;
}


while ((dirp = readdir(dp)) != NULL)
printf ("%s\n", dirp -> d_name);

if (closedir(dp) == -1 ) {
printf("Error in closing directory.");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*===================END===================*/

"Can't open foo." is not the most informative message.
Consider:
$ ./a.out foo
foo: No such file or directory
$ touch foo
$ ./a.out foo
foo: Not a directory
$ rm foo; mkdir foo; chmod 000 foo
$ ./a.out foo
foo: Permission denied

The above is the output of the following minor modifications
to your code:

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h> // for Directory Operations

int
main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
char *dirname;

if (argc == 1)
dirname = ".";
else if (argc == 2)
dirname = argv[1];
else {
printf("Usage: %s [DIR]\n", argv[0]);
return EXIT_FAILURE;
}

if ((dp = opendir(dirname)) == NULL) {
perror(dirname);
return EXIT_FAILURE;
}

while ((dirp = readdir(dp)) != NULL)
printf ("%s\n", dirp -> d_name);

if (closedir(dp) == -1 ) {
perror(dirname);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
 
B

Becker

Thanks Bill! Your advice is really helpful to me:)

regards,

Weichao Liu


Bill said:
Becker said:
Antonio said:
Hi, I need help. I want to read text files contained into a directory,
but my program is unaware of how many files are contained into.

In bash there's something similar to (figure out)

for i in <dir> do
...
done

but I don't know how to do the same in C. Can you show me snippets of code?

-- Antonio

Hi, there. I guess it's not neccessary for me to explain too much. The
code example shows how to get to know the contents of the directory.

/*==================BEGIN=================*/
/*
* Name:
* list_v01.c - version 0.1 of list
* Function:
* List directory.
*/
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h> // for Directory Operations

int
main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;

if (argc == 1) // if there is only 1 argument
dp = opendir (".");
else if (argc == 2) { // if there are 2 arguments
if ((dp = opendir(argv[1])) == NULL) {
printf("Can't open %s\n", argv[1]);
return EXIT_FAILURE;
}
}
else { // if there are more than 2 arguments
printf("Usage: %s [DIR]\n", argv[0]);
return EXIT_FAILURE;
}


while ((dirp = readdir(dp)) != NULL)
printf ("%s\n", dirp -> d_name);

if (closedir(dp) == -1 ) {
printf("Error in closing directory.");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*===================END===================*/

"Can't open foo." is not the most informative message.
Consider:
$ ./a.out foo
foo: No such file or directory
$ touch foo
$ ./a.out foo
foo: Not a directory
$ rm foo; mkdir foo; chmod 000 foo
$ ./a.out foo
foo: Permission denied

The above is the output of the following minor modifications
to your code:

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h> // for Directory Operations

int
main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
char *dirname;

if (argc == 1)
dirname = ".";
else if (argc == 2)
dirname = argv[1];
else {
printf("Usage: %s [DIR]\n", argv[0]);
return EXIT_FAILURE;
}

if ((dp = opendir(dirname)) == NULL) {
perror(dirname);
return EXIT_FAILURE;
}

while ((dirp = readdir(dp)) != NULL)
printf ("%s\n", dirp -> d_name);

if (closedir(dp) == -1 ) {
perror(dirname);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top