Chapter 4. Building Directories and Contents

In This Chapter

  • Creating and deleting directories

  • Getting the contents of a directory

  • Copying and moving, and why they are related

  • Moving and renaming files and why they are similar

We're about to say something you might not believe: C++ provides no functions for creating directories and getting the contents of a directory.

Really! We know it's hard to believe, but we can say two points about this:

  • There really is a (more-or-less) good reason for this lack: C++ is a general-purpose language; issues that deal with directories are specific to individual operating systems. Thus, it doesn't make sense to include such features in C++. (Supposedly. So they say. We guess. Whatever.)

  • Some brave rebels have added some functions — and these functions exist in most C++ implementations. Whew! That's a good thing — otherwise you'd have to call into the operating system to create or modify a directory.

C++ has a holdover from the C programming language in the header file stdio.h that includes functions for renaming and removing files and directories. (Interesting.) Oh yes, and there's another one in there for creating a temporary file. (Even more interesting.)

Warning

In this chapter we present you with ways to manipulate directories and files. (We have tested these routines only for the GNU gcc compiler that comes with the CodeBlocks product. If you're working with a different compiler or operating system, try them out. They probably will work.)

Note

For the examples ...

Get C++ All-In-One For Dummies®, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.