Skip to Content
C++の紹介 (Japanese Edition)
book

C++の紹介 (Japanese Edition)

by Frances Buontempo
March 2026
Intermediate
348 pages
4h 30m
Japanese
O'Reilly Media, Inc.
Content preview from C++の紹介 (Japanese Edition)

第8章 ファイルの操作

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

君はすでにC++についてかなり理解しているが、学ぶべきことはまだある。この章ではファイルの読み書きを行い、書き出しと読み込みを行う。第7章で書いたコードを使って模擬価格を生成し、保存し、それらを読み込む方法を学ぶ。そうすれば価格を取得する複数の方法が手に入る。

第1章ではストリームへの出力を、第2章ではストリームからの入力を書いた。C++ではファイルもストリームであるため、この章のプロジェクトに必要な知識はほぼ全て習得済みだ。ストリームを読み取る関数も既に書いたので、難しい部分は終わっている。この章では、std::cinではなくファイル を使ってget_prices関数を呼び出す方法を示す。

main関数から特定の値を返す方法(およびその理由)についても説明する。また、ビット演算子についても学ぶことになる。これはファイル制御にも同様の考え方が用いられるため、ここで関連性があるのだ。

ファイルへの書き込み

新しいmain.cppファイルを作成する。第7章 のget_prices関数を使って価格を生成するので、input.hをインクルードする。

main関数内に write_to_fileという関数を作成し、生成した価格をファイルに書き込む。以前std::coutで画面に出力していたが、C++でファイルに書き込むにはstd::ofstreamという出力ファイルストリームを使う。これは<FSTREAM>ヘッダで定義されている。ファイルストリームはcoutや cinと似ており、例えば出力には<<演算子を使う。

例8-1のコードを試す際、ビルドにinput.cppを追加することを忘れるな。

例 8-1. 株価をファイルに書き込む
#include <fstream> 1
#include <iostream>
#include <vector>

#include "input.h"

void write_to_file(const std::vector<double> & prices, 
		const std::string & filename)
{
    std::ofstream file{filename}; 2
    if(file) 3
    {
        for(auto price: prices) 4
        {
            file << price << '\n';  
        }
    }
}

int main()
{
    write_to_file ...
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

エージェントメッシュ (Japanese Edition)

エージェントメッシュ (Japanese Edition)

Eric Broda, Davis Broda

Publisher Resources

ISBN: 0642572352653