C Programming question

GEC: Discuss gaming, computers and electronics and venture into the bizarre world of STGODs.

Moderator: Thanas

Post Reply
User avatar
defanatic
Jedi Knight
Posts: 627
Joined: 2005-09-05 03:26am

C Programming question

Post 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.
>>Your head hurts.

>>Quaff painkillers

>>Your head no longer hurts.
User avatar
phongn
Rebel Leader
Posts: 18487
Joined: 2002-07-03 11:11pm

Post by phongn »

This is operating-system dependent. Also, I hope you aren't asking us to do your homework for you.
User avatar
Ace Pace
Hardware Lover
Posts: 8456
Joined: 2002-07-07 03:04am
Location: Wasting time instead of money
Contact:

Post 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.
Brotherhood of the Bear | HAB | Mess | SDnet archivist |
User avatar
Chris OFarrell
Durandal's Bitch
Posts: 5724
Joined: 2002-08-02 07:57pm
Contact:

Re: C Programming question

Post 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.
Image
User avatar
Braedley
Jedi Council Member
Posts: 1716
Joined: 2005-03-22 03:28pm
Location: Ida Galaxy
Contact:

Post 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 (//).
Image
My brother and sister-in-law: "Do you know where milk comes from?"
My niece: "Yeah, from the fridge!"
User avatar
phongn
Rebel Leader
Posts: 18487
Joined: 2002-07-03 11:11pm

Re: C Programming question

Post 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!
User avatar
Ace Pace
Hardware Lover
Posts: 8456
Joined: 2002-07-07 03:04am
Location: Wasting time instead of money
Contact:

Re: C Programming question

Post 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?
Brotherhood of the Bear | HAB | Mess | SDnet archivist |
User avatar
Durandal
Bile-Driven Hate Machine
Posts: 17927
Joined: 2002-07-03 06:26pm
Location: Silicon Valley, CA
Contact:

Post by Durandal »

In *nix, look at the man page for dirent.
Damien Sorresso

"Ever see what them computa bitchez do to numbas? It ain't natural. Numbas ain't supposed to be code, they supposed to quantify shit."
- The Onion
Post Reply