Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Traversing a directory tree in C++

Tags:

c++

This is been a curiosity of mine for a while: how do you traverse a directory tree without using boost or any third-party library? Just plain ol' C++ (examples in 98, 99, 01, 0x and 1x specs are okay.)? It was done back in the day before boost existed so there's got to be a way to do it.

like image 331
Casey Avatar asked Aug 04 '11 02:08

Casey


People also ask

What does it mean to traverse a directory?

Directory traversal (also known as file path traversal) is a web security vulnerability that allows an attacker to read arbitrary files on the server that is running an application. This might include application code and data, credentials for back-end systems, and sensitive operating system files.

How do I recurse a directory?

For example, when listing files in a Windows command prompt, you can use the dir /s command to recursively list all files in the current directory and any subdirectories.


2 Answers

Please take a look at http://en.wikipedia.org/wiki/Dirent.h

The reference also has a link to dirent.h implementation for Windows or you can use cygwin

If you want to just do it for Windows you can build upon this example

http://msdn.microsoft.com/en-us/library/aa365200%28VS.85%29.aspx

like image 109
parapura rajkumar Avatar answered Oct 02 '22 19:10

parapura rajkumar


There are no standard filesystem functions, so you won't get any answers that use "plain C++". For POSIX systems, opendir is used. For Windows, FindFirstFile. I'm not sure about other OSes.

There's a reason people recommend Boost Filesystem—it's portable and takes care of all these details for you.

like image 45
Cory Nelson Avatar answered Oct 02 '22 17:10

Cory Nelson