Skip to Content
Introducing C++
book

Introducing C++

by Frances Buontempo
March 2026
Beginner
348 pages
7h 27m
English
O'Reilly Media, Inc.
Content preview from Introducing C++

Chapter 8. Working with Files

You now know quite a lot of C++, but there is more to learn. In this chapter, you will work with files, writing them out and reading them back. You’ll generate simulated prices, using the code you wrote in Chapter 7, save them, and learn how to read them back in. You will then have several ways to get prices.

In Chapter 1 you wrote output to a stream, and in Chapter 2 you read input from a stream. Files are also streams in C++, so you know almost everything you need for the project in this chapter. You have also written a function to read a stream, so you’ve done the hard work already. This chapter will show you how to call your get_prices function with a file, rather than with std::cin.

I’ll also discuss how (and why) to return specific values from main, and you will learn about bitwise operators, which are relevant here because a similar idea is used to control files.

Writing to a File

Create a new main.cpp file. You will use the get_prices function from Chapter 7 to generate prices, so include input.h.

Write a function in main called write_to_file to write the generated prices to a file. You previously used std::cout to stream values to the screen. To write to a file in C++, you use an output file stream, called std::ofstream. This is defined in the <fstream> header. File streams are similar to cout and cin. For example, you use operator << for output.

Try the code in Example 8-1, remembering to add input.cpp to your build.

Example 8-1. Writing ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Effective Modern C++

Effective Modern C++

Scott Meyers
C++ Software Design

C++ Software Design

Klaus Iglberger
Learn C++ by Example

Learn C++ by Example

Frances Buontempo

Publisher Resources

ISBN: 9781098178130Errata Page