i need to write a program which accepts a folder as input and browses through that folder and says whether there is a file or a subfolder present in it and generate an xml file that gives the hierarchy(subfolder names & file names present) in that folder.
BUT IT SHOULD NOT USE ANY WINDOWS API. I'M WRITING IT FOR LINUX/UNIX.

Can anyone kindly shed light on this one ASAP.
All suggestions & help will be deeply appreciated.

thank you.

Dani AI

Generated

The task is straightforward: walk a directory on Linux/Unix and emit a hierarchical XML of folders and files (no Windows API). asked for that; suggested Boost.Filesystem and pointed to the POSIX tree-walk idea. Below are practical, current choices and a short workflow to build a robust solution.

Pick a traversal API

  • Modern C++: prefer std::filesystem (C++17+) — it provides convenient iterators and portable semantics. See the reference: std::filesystem.
  • Older C++ or portability to pre-C++17: Boost.Filesystem maps closely to std::filesystem (migrates easily). See Boost docs: Boost.Filesystem.
  • C / POSIX: use opendir/readdir + lstat or the callback-style nftw for convenience (good for C programs). See the man page: nftw(3).

Practical workflow and cautions

  • Canonicalize the input path and decide whether to follow symlinks. To avoid cycles, either do not follow symlinks or track visited (device+inode) pairs.
  • For each entry determine type (directory, regular file, symlink) and read metadata with stat/lstat or std::filesystem::status.
  • Prefer a streaming XML writer for large trees to avoid building the whole DOM in memory. Good libraries: pugixml, , tinyxml2. Always escape & < > in names or use proper element/attribute APIs from the XML library.
  • Handle permissions/errors gracefully (log and continue), respect UTF-8 filenames (or correctly handle locale), and enforce a recursion/depth limit if needed.

Minimal traversal sketch (C++17)

// iterate with std::filesystem::recursive_directory_iterator,
// check is_directory/is_regular_file, and stream XML output

This approach keeps the code portable, avoids Windows-only APIs, and separates traversal from XML emission so the output format can be changed easily.

Recommended Answers

All 3 Replies

Look into boost filesystem perhaps? I think it would satisfy those requirements (besides the xml file)

boost file system ?? can u pls elaborate on what to do exactly.

NAME
       ftw, nftw - file tree walk

SYNOPSIS
       #include <ftw.h>

       int  ftw(const  char  *dir,  int (*fn)(const char *file, const struct stat *sb, int
       flag), int nopenfd);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.