#### Archived

This topic is now archived and is closed to further replies.

# Some help with Files and Reading from Directories

This topic is 6425 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Can anyone point me to a good tutorial on having C++ open up a directory and then go through each file one by one printing on the screen all the files that are in the directory. Thanks a lot. Hope someone has a link to a good reference on it. Neo_Matrix

##### Share on other sites
Yeah, its called "dir"

Seriously though, you are going to have to give more details on what you are planning to do - windows? unix?

If windows then you can use the common dialog boxes for file access with MFC... its also possible with pure win32 api but its a bit more of a challenge. If you are programming games then MFC generally isnt recommended.

But back to my dir "joke", you could use the system command:

  system("dir");//or in unix...system("ls");

Note that this will splurt all the output to the standard output.. ie the command line interfacy thing... and remember to #include process.h.

Yeah, i know you asked for tutorials but i dont know any

Edited by - jumble on July 19, 2000 1:58:14 PM

##### Share on other sites
I dont know a tutorial, but i think this is what you are looking for.
It is possible to do what you want - but its platform specific.
If you are writing on Windows:

  #include #include #include long fh;struct _finddata_t findData;fh = _findfirst( "basepath\\*.*", &findData);if ( !fh){ // cannot open path}else{ do { printf( "file/dir = %s\n", fileData.name); } while ( _findnext( fh, &findData));}you need to remember to add on "\\*.*" onto whatever directory you want to look in.but if you are writing under unix would want something likethis:#include #include #include DIR *dp;struct dirent *DEntry;dp = opendir( "basepath");if ( !dp){ // cannot open path}else{ while( DEntry = readdir( dp) != 0) { printf( "file/dir = %s\n", DEntry->d_name); }}

If you want to know whether each entry is a file or another directory, you will need to use stat

As you can see, the Unix solution is a little nicer. But then i am biased.

Hope that helps

Edited by - shamen on July 19, 2000 2:41:31 PM

##### Share on other sites
That is exactly what I wanted. I will mess with the code and see what I can come up with. Do you know where I can find information on setting up FTP in C++? I think Unix easier but doing it for NT, damn. Thanks again.

Neo_Matrix