473,568 Members | 2,986 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

List files in current directory

Hi there, quick question, how would I retrieve a list of files in ANSI C in
a purely platform independent way?

Any pointers would be great!

thanks
Kristan
Oct 6 '06 #1
7 2459

"Kristan" <kr*******@hotm ail.comwrote in message
news:45******** *************** @ptn-nntp-reader01.plus.n et...
Hi there, quick question, how would I retrieve a list of files in ANSI C
in
a purely platform independent way?

Any pointers would be great!
(I'm posting after reading on comp.lang.c. I don't know if it's suited to
the other NG's you posted to and you didn't set followups to the NG you are
reading from. Since, I don't know from which NG you are reading replies,
all NG's you posted to whether appropriate or not get this message.)

"in ANSI C"
- You don't. You could use POSIX C routines.
"in a purely platform independent way"
- You might use a platform specific version of Doug Gwyn's Public Domain
libndir package or create a multiplatform version from the various versions.
for BSD, libndir.tar.Z http://ftp.br.xemacs.org/pub/unix-c/languages/c/
for POSIX, libndir-posix.tar.Z
http://ftp.br.xemacs.org/pub/unix-c/languages/c/
for DOS, (several versions exist, I'm not looking them up unless you
_actually_ need them, i.e., beg)
- You might look at how multi-platform applications which store directory
structures work, like Info-ZIP, PDTar (Public Domain tar), GNU tar, etc...
Infozip http://www.info-zip.org/pub/infozip/
pdtar.tar.Z http://ftp.br.xemacs.org/pub/unix-c/tapes/

Programs like Info-ZIP and PDTar have to create their routines which perform
directory access identically on many platforms. However, those routines may
not be integrated into a single file...
Rod Pemberton
Oct 6 '06 #2
Hi there, well, POSIX sounds promising, as it seems to be supported on Linux
and Windows (2000 upwards), basically I'm using Windows for development and
the Debian Linux server is the platform.

Do you know whether POSIX is available with my Visual Studio 2005 C
compiler? Or would I have to obtain it separately?

thanks
Kristan
"Rod Pemberton" <do*********@bi tfoad.cmmwrote in message
news:eg******** **@main.corriga .net...
>
"Kristan" <kr*******@hotm ail.comwrote in message
news:45******** *************** @ptn-nntp-reader01.plus.n et...
>Hi there, quick question, how would I retrieve a list of files in ANSI C
in
>a purely platform independent way?

Any pointers would be great!
(I'm posting after reading on comp.lang.c. I don't know if it's suited to
the other NG's you posted to and you didn't set followups to the NG you
are
reading from. Since, I don't know from which NG you are reading replies,
all NG's you posted to whether appropriate or not get this message.)

"in ANSI C"
- You don't. You could use POSIX C routines.
"in a purely platform independent way"
- You might use a platform specific version of Doug Gwyn's Public Domain
libndir package or create a multiplatform version from the various
versions.
for BSD, libndir.tar.Z http://ftp.br.xemacs.org/pub/unix-c/languages/c/
for POSIX, libndir-posix.tar.Z
http://ftp.br.xemacs.org/pub/unix-c/languages/c/
for DOS, (several versions exist, I'm not looking them up unless you
_actually_ need them, i.e., beg)
- You might look at how multi-platform applications which store directory
structures work, like Info-ZIP, PDTar (Public Domain tar), GNU tar, etc...
Infozip http://www.info-zip.org/pub/infozip/
pdtar.tar.Z http://ftp.br.xemacs.org/pub/unix-c/tapes/

Programs like Info-ZIP and PDTar have to create their routines which
perform
directory access identically on many platforms. However, those routines
may
not be integrated into a single file...
Rod Pemberton


Oct 6 '06 #3
Kristan wrote:
Hi there, well, POSIX sounds promising, as it seems to be supported on Linux
and Windows (2000 upwards), basically I'm using Windows for development and
the Debian Linux server is the platform.

Do you know whether POSIX is available with my Visual Studio 2005 C
compiler? Or would I have to obtain it separately?
I don't think it's directly supported. There is a POSIX subsystem for
Windows that you can download, but it does not use the Visual Studio C
compiler.

My approach when dealing with platform-specifics is to abstract the
functionality into functions that are conditionally compiled on each system.

Below is a directory lister for Windows, Unix/POSIX and MS-DOS.

#include <stdio.h>

#ifdef _WIN32

/* Compiling for Windows */

#include <windows.h>

int main(void)
{
WIN32_FIND_DATA f;
HANDLE h = FindFirstFile(" ./*", &f);
if(h != INVALID_HANDLE_ VALUE)
{
do
{
puts(f.cFileNam e);
} while(FindNextF ile(h, &f));
}
else
{
fprintf(stderr, "Error opening directory\n");
}
return 0;
}

#else
#ifdef __unix__

/* Compiling for UNIX / POSIX */

#include <sys/types.h>
#include <dirent.h>

int main(void)
{
DIR *dir = opendir(".");
if(dir)
{
struct dirent *ent;
while((ent = readdir(dir)) != NULL)
{
puts(ent->d_name);
}
}
else
{
fprintf(stderr, "Error opening directory\n");
}
return 0;
}

#else
#ifdef __TURBOC__

/* Compiling for MS-DOS */

#include <dir.h>

int main(void)
{
struct ffblk ffblk;
if(findfirst("* .*", &ffblk, 0) == 0)
{
do
{
puts(ffblk.ff_n ame);
} while(findnext( &ffblk) == 0);
}
else
{
fprintf(stderr, "Error opening directory\n");
}
return 0;
}

#else
#error Unsupported Implementation
#endif
#endif
#endif
Oct 6 '06 #4
>Hi there, quick question, how would I retrieve a list of files in ANSI C in
>a purely platform independent way?
1. Prompt the user for the file names, one at a time.
2. Get the list of file names from argv[].
3. Open a file containing a list of file names and read it, one line at a time.
Oct 6 '06 #5
Kristan wrote:
Hi there, quick question, how would I retrieve a list of files in ANSI C in
a purely platform independent way?

Any pointers would be great!

thanks
Kristan

Windows:
system("dir /b file.lst");
Linux:
system("ls file.lst");

might do it.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Oct 7 '06 #6
Joe Wright <jo********@com cast.netwrites:
Kristan wrote:
>Hi there, quick question, how would I retrieve a list of files in
ANSI C in a purely platform independent way?
Any pointers would be great!
thanks
Kristan
Windows:
system("dir /b file.lst");
Linux:
system("ls file.lst");

might do it.
Or it might not.

<OT>
In both cases, the command applies only to the current directory,
whatever that happens to be. The Unix command skips any files whose
names start with '.'. The order in which the files are listed may or
may not depend on the current locale. Either command will fail if you
don't have write permission in the current directory. If "file.lst"
already exists, the command will either clobber it or fail; if it
doesn't exist, you've just added a new file that may or may not show
up in the listing.
</OT>

Since you've provided different solutions for Windows and Linux, it's
obviously not "purely platform independent", which is what the OP was
asking for. The fact that you're using the standard function system()
doesn't make the code platform independent; it merely delays any
failure until execution time.

There is no purely platform independent solution.

Both Windows and Linux provide system-specific mechanisms for
retrieving a list of files (the Linux solution should work on any
Unix-like system). These mechanisms are far more flexible, and they
don't depend on creating and reading a temporary file in the very
directory you're trying to examine

This is one of those cases where trying to write portable code is a
waste of time; the non-portable solutions work better, and the
seemingly portable solution isn't portable at all.

--
Keith Thompson (The_Other_Keit h) 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.
Oct 7 '06 #7
"Kristan" <kr*******@hotm ail.comwrote in message
news:45******** *************** @ptn-nntp-reader04.plus.n et...
Hi there, well, POSIX sounds promising, as it seems to be supported on
Linux and Windows (2000 upwards), basically I'm using Windows for
development and the Debian Linux server is the platform.

Do you know whether POSIX is available with my Visual Studio 2005 C
compiler? Or would I have to obtain it separately?
For your specific need, look at:
http://www.two-sdg.demon.co.uk/curbr...nt/dirent.html
>
thanks
Kristan
"Rod Pemberton" <do*********@bi tfoad.cmmwrote in message
news:eg******** **@main.corriga .net...
>>
"Kristan" <kr*******@hotm ail.comwrote in message
news:45******* *************** *@ptn-nntp-reader01.plus.n et...
>>Hi there, quick question, how would I retrieve a list of files in ANSI C
in
>>a purely platform independent way?

Any pointers would be great!
(I'm posting after reading on comp.lang.c. I don't know if it's suited
to
the other NG's you posted to and you didn't set followups to the NG you
are
reading from. Since, I don't know from which NG you are reading replies,
all NG's you posted to whether appropriate or not get this message.)

"in ANSI C"
- You don't. You could use POSIX C routines.
"in a purely platform independent way"
- You might use a platform specific version of Doug Gwyn's Public Domain
libndir package or create a multiplatform version from the various
versions.
for BSD, libndir.tar.Z http://ftp.br.xemacs.org/pub/unix-c/languages/c/
for POSIX, libndir-posix.tar.Z
http://ftp.br.xemacs.org/pub/unix-c/languages/c/
for DOS, (several versions exist, I'm not looking them up unless you
_actually_ need them, i.e., beg)
- You might look at how multi-platform applications which store directory
structures work, like Info-ZIP, PDTar (Public Domain tar), GNU tar,
etc...
Infozip http://www.info-zip.org/pub/infozip/
pdtar.tar.Z http://ftp.br.xemacs.org/pub/unix-c/tapes/

Programs like Info-ZIP and PDTar have to create their routines which
perform
directory access identically on many platforms. However, those routines
may
not be integrated into a single file...
Rod Pemberton



Oct 10 '06 #8

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

Similar topics

5
2751
by: Ken | last post by:
I currently have a set of documents in a directory that i need to list in a html table. Is there any way to generate the table with the documents listed instead of having to update the table manually everytime a new document is added to the list. Thanks Ken
6
2750
by: Gonnasi | last post by:
With >glob.glob("*") or >os.listdir(cwd) I can get a combined file list with directory list, but I just wanna a bare file list, no directory list. How to get it? Tons of thanks in advance!
7
2831
by: hank | last post by:
Hi All In the Circular Logging when the Primary Log file fill up, the database manager will creat a secondary log files for the transaction; when this transaction finished, the secondary log files still allocated in log directory; when all application disconnect from database or database reactive or database restart the secondary log files...
2
1589
by: mhk | last post by:
Hi , i am writing a c language program in Unix to see the executable files in a directory and it works if the directory is current but if i change to another directory than current directory then it show all the files even if not executable. here is the main code while (p = readdir(dp))
3
3751
by: Nick | last post by:
Is it possible to read a list of files from a specified directory using VB.net We have company intranet and I have created a page that displays photos from different events. I have coded a page that display images from a particular directory and named the files Pic1.jpg, Pic2.jpg etc. My code then cycles through the images and displays...
2
2109
by: Rob_S | last post by:
I have a program which saves time stamped files into time stamped directories. When I want to read these files, I get the current date and check for the existence of the directory using.... while (!(_chdir(dirname)) == 0) {reduce the directory name (time) and check again} This method quickly finds the last directory but I don't know...
3
1775
by: jpabich | last post by:
I want to display a list of filenames that exist in a certain directory. How do I go about loading this list?
7
32870
by: Kristan | last post by:
Hi there, quick question, how would I retrieve a list of files in ANSI C in a purely platform independent way? Any pointers would be great! thanks Kristan
6
4146
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would try my own small program. The problem is, I seem to be having trouble with memory, i.e. sometimes my program will work and display the correct output,...
1
2942
by: Trevor17 | last post by:
Hello All, I am currently in a Perl programming class and our assignment was: Create a filehandle with the open function that uses a pipe to list all the files in your current directory and will print only those files that are readable text files. Use the die function to quit if the open fails. After several hours, i have completed a...
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.