Modern C++ Programming Cookbook
Marius Bancila
- 590 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
Modern C++ Programming Cookbook
Marius Bancila
About This Book
Over 100 recipes to help you overcome your difficulties with C++ programming and gain a deeper understanding of the working of modern C++About This Bookā¢ Explore the most important language and library features of C++17, including containers, algorithms, regular expressions, threads, and more, ā¢ Get going with unit testing frameworks Boost.Test, Google Test and Catch, ā¢ Extend your C++ knowledge and take your development skills to new heights by making your applications fast, robust, and scalable.Who This Book Is ForIf you want to overcome difficult phases of development with C++ and leverage its features using modern programming practices, then this book is for you. The book is designed for both experienced C++ programmers as well as people with strong knowledge of OOP concepts.What You Will Learnā¢ Get to know about the new core language features and the problems they were intended to solveā¢ Understand the standard support for threading and concurrency and know how to put them on work for daily basic tasksā¢ Leverage C++'s features to get increased robustness and performanceā¢ Explore the widely-used testing frameworks for C++ and implement various useful patterns and idiomsā¢ Work with various types of strings and look at the various aspects of compilationā¢ Explore functions and callable objects with a focus on modern featuresā¢ Leverage the standard library and work with containers, algorithms, and iteratorsā¢ Use regular expressions for find and replace string operationsā¢ Take advantage of the new filesystem library to work with files and directoriesā¢ Use the new utility additions to the standard library to solve common problems developers encounter including string_view, any, optional and variant typesIn DetailC++ is one of the most widely used programming languages. Fast, efficient, and flexible, it is used to solve many problems. The latest versions of C++ have seen programmers change the way they code, giving up on the old-fashioned C-style programming and adopting modern C++ instead.Beginning with the modern language features, each recipe addresses a specific problem, with a discussion that explains the solution and offers insight into how it works. You will learn major concepts about the core programming language as well as common tasks faced while building a wide variety of software. You will learn about concepts such as concurrency, performance, meta-programming, lambda expressions, regular expressions, testing, and many more in the form of recipes. These recipes will ensure you can make your applications robust and fast.By the end of the book, you will understand the newer aspects of C++11/14/17 and will be able to overcome tasks that are time-consuming or would break your stride while developing.Style and approachThis book follows a recipe-based approach, with examples that will empower you to implement the core programming language features and explore the newer aspects of C++.
Frequently asked questions
Information
Working with Files and Streams
- Reading and writing raw data from/to binary files
- Reading and writing objects from/to binary files
- Using localized settings for streams
- Using I/O manipulators to control the output of a stream
- Using monetary I/O manipulators
- Using time I/O manipulators
- Working with filesystem paths
- Creating, copying, and deleting files and directories
- Removing content from a file
- Checking the properties of an existing file or directory
- Enumerating the content of a directory
- Finding a file
Introduction
Reading and writing raw data from/to binary files
Getting ready
std::vector<unsigned char> output {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
How to do it...
- Open a file stream for writing in binary mode by creating an instance of the std::ofstream class:
std::ofstream ofile("sample.bin", std::ios::binary);
- Ensure that the file is actually open before writing data to the file:
if(ofile.is_open())
{
// streamed file operations
}
- Write the data to the file by providing a pointer to the array of characters and the number of characters to write:
ofile.write(reinterpret_cast<char*>(output.data()),
output.size());
- Flush the content of the stream buffer to the actual disk file; this is automatically done when you close the stream:
ofile.close();
- Open a file stream to read from a file in the binary mode by creating an instance of the std::ifstream class:
std::ifstream ifile("sample.bin", std::ios::binary);
- Ensure that the file is actually opened before reading data from it:
if(ifile.is_open())
{
// streamed file operations
}
- Determine the length of the file by positioning the input position indicator to the end of the file, read its value, and then move the indicator to the beginning:
ifile.seekg(0, std::ios_base::end);
auto length = ifile.tellg();
ifile.seekg(0, std::ios_base::beg);
- Allocate memory to read the content of the file:
std::vector<unsigned char> input;
input.resize(static_cast<size_t>(length));
- Read the content of the file to the allocated buffer by providing a pointer to the array of characters for receiving the data and the number of characters to read:
ifile.read(reinterpret_cast<char*>(input.data()), length);
- Check that the read operation is completed successfully:
auto success = !ifile.fail() && length == ifile.gcount();
- Finally, close the file stream:
ifile.close();
How it works...
- basic_filebuf implements the input/output operations for a raw file and is similar in semantics with a C FILE stream.
- basic_ifstream implements the high-level file stream input operations defined by the basic_istream stream interface, internally using a basic_filebuf object.
- basic_ofstream implements the high-level file stream output operations defined by the basic_ostream stream interface, internally using a basic_filebuf object.
- basic_fstream implements the high-level file stream input and output operations defined by the basic_iostream stream interface, internally using a basic_filebuf object.
typedef basic_ifstream<char> ifstream;
typedef basic_ifstream<wchar_t> wifstream;
typedef basic_ofstream<char> ofstream;
typedef basic_ofstream<wchar_t> wofstream;
typedef basic_fstream<char> fstream;
typedef basic_fstream<wchar_t> wfstream;
- To write data to a file, we instantiated an object of the type std::ofstream. In the constructor, we passed the name of the file to be opened and the stream open mode, for which we specified std::ios::binary to indicate binary mode. Opening the file like this discards the previous file content. If you want to append content to an existing file, you should also use the flag std::ios::app (that is, std::ios::app | std::ios::binary). This constructor internally calls open() on its underlying raw file object, that is, a basic_filebuf object. If this operation fails, a fail bit is set. To check whether the stream has been successfully associated with a file device, we used is_open() (that internally calls the method with the same name from the underlying basic_filebuf). Writing data to the file stream is done with the write() meth...
Table of contents
- Title Page
- Copyright
- Credits
- About the Author
- About the Reviewer
- www.PacktPub.com
- Customer Feedback
- Preface
- Learning Modern Core Language Features
- Working with Numbers and Strings
- Exploring Functions
- Preprocessor and Compilation
- Standard Library Containers, Algorithms, and Iterators
- General Purpose Utilities
- Working with Files and Streams
- Leveraging Threading and Concurrency
- Robustness and Performance
- Implementing Patterns and Idioms
- Exploring Testing Frameworks
- Bibliography