473,326 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How to know how many files are into a directory

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

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

-- Antonio
Nov 19 '06 #1
12 2041
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.
Nov 19 '06 #2
Antonio Maschio wrote:
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 <dirdo
...
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.

Nov 19 '06 #3
On Sun, 19 Nov 2006 14:50:57 +0100, in comp.lang.c , Antonio Maschio
<tb**@libero.itwrote:
>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
Nov 19 '06 #4
"santosh" <sa*********@gmail.comwrites:
[...]
Standard C doesn't even have the concept of files or directories,
[...]

Well, it does have the concept of files.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 19 '06 #5
Antonio Maschio wrote:
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 <dirdo
...
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.
--
Flash Gordon
Nov 19 '06 #6
Keith Thompson wrote:
"santosh" <sa*********@gmail.comwrites:
[...]
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.

Nov 19 '06 #7
"santosh" <sa*********@gmail.comwrites:
Keith Thompson wrote:
>"santosh" <sa*********@gmail.comwrites:
[...]
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.

--
Ben.
Nov 19 '06 #8
"santosh" <sa*********@gmail.comwrites:
Keith Thompson wrote:
>"santosh" <sa*********@gmail.comwrites:
[...]
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. 8-)}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 19 '06 #9
santosh said:
Keith Thompson wrote:
>"santosh" <sa*********@gmail.comwrites:
[...]
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.
Nov 19 '06 #10
Antonio Maschio wrote:
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 <dirdo
...
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
===================================

Nov 20 '06 #11

Becker wrote:
Antonio Maschio wrote:
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 <dirdo
...
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;
}

Nov 20 '06 #12
Thanks Bill! Your advice is really helpful to me:-)

regards,

Weichao Liu
Bill Pursell wrote:
Becker wrote:
Antonio Maschio wrote:
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 <dirdo
...
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;
}
Nov 20 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
1
by: jajoo | last post by:
Hi everyone, I am trying to send files with multipart/form-date. Everything is ok with the send. But when I am receiving the files I should specify a directory where the files to be saved. The...
0
by: Mart Rogers | last post by:
I have an asp.net project which includes some javascript files which live in a subdirectory to the project root directory. The include statements obviously expect the files to be in that...
1
by: tom lewton | last post by:
if the following article is to be believed: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/supported_file_systems.asp a directory can contain 66534 files. however,...
6
by: Peter Row | last post by:
Hi, Can someone give me a set of steps for an ASP.NET project (well actually its just a VB.NET class DLL that implements HttpHandler) that will work when moved to another developers machine? ...
1
by: Aaron via DotNetMonster.com | last post by:
I am testing for several file types in a directory by creating an array of masks and for each mask in the array, I execute a GetFiles. My issue is that the VB code: ...
10
by: Martin Ho | last post by:
I am running into one really big problem. I wrote a script in vb.net to make a copy of folders and subfolder to another destination: - in 'from.txt' I specify which folders to copy - in...
63
by: David Mathog | last post by:
There have been a series of questions about directory operations, all of which have been answered with "there is no portable way to do this". This raises the perfectly reasonable question, why,...
7
by: devnew | last post by:
hi i am trying to create a cache of digitized values of around 100 image files in a folder..In my program i would like to know from time to time if a new image has been added or removed from the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.