Page 1 of 1

C Programming question

Posted: 2006-10-24 11:36am
by defanatic
I am doing C course, and was wondering how to write a program that would print out all files in a given directory.

Posted: 2006-10-24 01:24pm
by phongn
This is operating-system dependent. Also, I hope you aren't asking us to do your homework for you.

Posted: 2006-10-24 01:34pm
by Ace Pace
Since I don't know C, but you can easily find out the following in C from looking up reference documents.

Step is getting the file list.

Work file by file(in a loop), and print it out using the built in commands.

This is simple enough that it shouldn't give anything away.

Re: C Programming question

Posted: 2006-10-24 07:21pm
by Chris OFarrell
defanatic wrote:I am doing C course, and was wondering how to write a program that would print out all files in a given directory.
In unix / linux enviroments there are a whole heep of functions that deal with the operating system and manipulating the file system, I think they are in <unistd.h>, <stdio.h> and some others. Visual C++ has a crapload of stuff about manipulating windows ACL's and so on, but I can't remember exactly what.

Posted: 2006-10-24 07:51pm
by Braedley
Do yourself a favour and go find yourself a library. Unless your assignment is to come up with the solution. Then that's just cheating.

I do know that in IDL it's as easy as

Code: Select all

directory='c:\'   ; or some other directory
print, transpose(file_search(directory, '*'))
;or
files=directory+'*'
print, transpose(file_search(files))
The transpose simply formats the array to print as a column. The first one does a recursive search (which will probably yield a list that is tens of thousands of elements long, mine is 22677). The second will just list the folders and files in that specific directory. This is all useless to you, however, as you're using C, not IDL.

EDIT: I'd also like to point out that in IDL, the comment character is the semi-colon (;), not slash-slash (//).

Re: C Programming question

Posted: 2006-10-24 10:21pm
by phongn
Chris OFarrell wrote:In unix / linux enviroments there are a whole heep of functions that deal with the operating system and manipulating the file system, I think they are in <unistd.h>, <stdio.h> and some others. Visual C++ has a crapload of stuff about manipulating windows ACL's and so on, but I can't remember exactly what.
Ah, but he has to do it in C, which means directly accessing the Win32 API. No MFC or .NET Framework for him!

Re: C Programming question

Posted: 2006-10-25 01:20am
by Ace Pace
phongn wrote:
Chris OFarrell wrote:In unix / linux enviroments there are a whole heep of functions that deal with the operating system and manipulating the file system, I think they are in <unistd.h>, <stdio.h> and some others. Visual C++ has a crapload of stuff about manipulating windows ACL's and so on, but I can't remember exactly what.
Ah, but he has to do it in C, which means directly accessing the Win32 API. No MFC or .NET Framework for him!
Isn't the Win32 API quite good enough for this sort of work?

Posted: 2006-10-25 02:35am
by Durandal
In *nix, look at the man page for dirent.