Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
C++ System Programming Cookbook

You're reading from   C++ System Programming Cookbook Practical recipes for Linux system-level programming using the latest C++ features

Arrow left icon
Product type Paperback
Published in Feb 2020
Publisher Packt
ISBN-13 9781838646554
Length 292 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Onorato Vaticone Onorato Vaticone
Author Profile Icon Onorato Vaticone
Onorato Vaticone
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting Started with System Programming 2. Revisiting C++ FREE CHAPTER 3. Dealing with Processes and Threads 4. Deep Dive into Memory Management 5. Using Mutexes, Semaphores, and Condition Variables 6. Pipes, First-In First-Out (FIFO), Message Queues, and Shared Memory 7. Network Programming 8. Dealing with Console I/O and Files 9. Dealing with Time Interfaces 10. Managing Signals 11. Scheduling 12. Other Books You May Enjoy

Understanding the filesystem

C++17 marks another huge milestone in terms of new features. The filesystem library provides a simpler way of interacting with the filesystem. It was inspired by Boost.Filesystem (available since 2003). This recipe will show its basics features.

How to do it...

In this section, we'll show two examples of the filesystem library by using directory_iterator and create_directories. Although there is definitely more under this namespace, the goal of these two snippets is to highlight their simplicity:

  1. std::filesystem::directory_iterator: Let's write the following code:
#include <iostream>
#include <filesystem>
int main()
{
for(auto& p: std::filesystem::directory_iterator("/"))
std::cout << p << std::endl;
}
  1. Now, compile it with g++ filesystem_01.cpp -std=c++17 -lstdc++fs, where -std=c++17 tells the compiler to use the C++17 standard and -lstdc++fs tells the compiler to use the filesystem library.

The second example is about creating a directory and a file:

  1. std::filesystem::create_directories: Write the following code:
#include <iostream>
#include <filesystem>
#include <fstream>
int main()
{
std::filesystem::create_directories("test/src/config");
std::ofstream("test/src/file.txt") << "This is an example!"
<< std::endl;
}
  1. The compilation is as the same as the previous example: g++ filesystem_02.cpp -std=c++17 -lstdc++fs.

With just two lines of code, we've created a folder structure, a file, and have also written on it! It's as simple (and portable) as that.

How it works...

The filesystem library is located in the <filesystem> header under the std::filesystem namespace. These two tests, although pretty simple, were needed to show how powerful the filesystem library is. The output of the first program is as follows:

A complete list of std::filesystem methods can be found here: https://en.cppreference.com/w/cpp/header/filesystem.

std::filesystem::create_directories create a directory (recursively, if test/src does not exist) in the current folder, in this case. Of course, an absolute path is managed too and the current line would be perfectly valid, that is, std::filesystem::create_directories("/usr/local/test/config");.

The second line of the source code uses ofstream to create an output file stream named test/src/file.txt and appends << to the string: This is an example!.

There's more...

The filesystem library is heavily inspired by Boost.Filesystem, which has been available since 2003. If you want to experiment and debug a little, just add the -g option (add the debug symbols to the binary) to the compiler: g++ -g fs.cpp -std=c++17 -lstdc++fs.

See also

The books Effective Modern C++ by Scott Meyers and The C++ Programming Language by Bjarne Stroustrup cover these topics in great detail.

You have been reading a chapter from
C++ System Programming Cookbook
Published in: Feb 2020
Publisher: Packt
ISBN-13: 9781838646554
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime