![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Dec 2004
Posts: 50
Rep Power: 4
![]() |
How to get file listing of a dir
Is there any way to get a list of the files in a dir with win api or c++ standard libs?
|
|
|
|
|
|
#2 |
|
Newbie
Join Date: Mar 2005
Posts: 7
Rep Power: 0
![]() |
are you familiar with the "dir" and "cd" commands and the command prompt?
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2005
Posts: 7
Rep Power: 0
![]() |
If so then, use the "system" command in C++ to change to the directory you want and view a list of files. Here the code:
#include <cstdlib> system ("command"); system ("cd\\"); // if you wanted to switch to the windows directory for instance. system ("cd windows"); system ("dir"); //Displays a list of the files in that directory. // or use this is there are a lot of files in that directory and they can't all be fit // on the command prompt window: system ("dir /p"); |
|
|
|
|
|
#4 | |
|
Programmer
Join Date: Dec 2004
Posts: 50
Rep Power: 4
![]() |
Quote:
![]() is there any way to do this with windows api? |
|
|
|
|
|
|
#5 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
search for FindFirstFile and FindNextFile for windows api implementations
|
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4
![]() |
Ah, I don't want to burst your bubble, but system() forks a new shell so the present working directory should only be changed as long as that shell is running; e.g.
pwd is /home/mackenga
code does: system("cd ..");
pwd is /home/mackengaIn this scenario, I do go into the /home directory, but only until the system() function returns. Another problem with your method is: what if you want your program to know a list of the files, rather than just display said list to the user without actually getting any information into your application about what's there? The usual way to read directories in C, Perl, etc., is using opendir() then readdir(). Well, I'm /sure/ this applies to Perl, and suspect C too since most things do though I have to blush and confess I've never read a dir from C as far as I can remember (terrible, isn't it?). |
|
|
|
|
|
#7 |
|
Programming Guru
![]() ![]() ![]() |
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Feb 2005
Posts: 18
Rep Power: 0
![]() |
I'm having this same problem. I need the "right" (non-system()) way to do it. I run a webserver of my own creation, and I implemented this for auto-file indexing. I manipulated it to output the dir command to a file (system("dir > temp.txt");) and read it back in, but if two people try to request a directory (same or not) at once, the server crashes.
Thank you for that link. <3 Last edited by Floppie; Apr 2nd, 2005 at 2:55 PM. |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Dec 2004
Posts: 50
Rep Power: 4
![]() |
FindFirstFile() and FindNextFile() work perfectly thanks
![]() |
|
|
|
|
|
#10 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4
![]() |
File locking
To avoid problems with multiple simultaneous requests crashing your server or producing undesirable behaviour, you'll really need to look into file locking. You can use portable advisory file locking (flock) from a variety of languages and it works under most circumstances (not great over an NFS share though).
An alternative for your problem might be to use unique filenames for the requests; to do this properly, include the process ID and the current system time in your tempfile names and the chances of a clash are very slim (two processes would have to get the same pid in the same second, which is highly unlikely). |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|