I/O (C++)
簡単なファイル I/O (C++版)
ここでは、C++ 言語を用いた簡単な入出力を紹介します。といっても、コンテストに必要となりそうな主な例を紹介するだけですが。 詳しい情報は、リファレンスマニュアルなどを見てください。
標準出力 (cout)
標準出力に文字列を表示するには、cout
と <<
operator を使えばよいでしょう。
簡単な例(printTest.cpp)を紹介しておきます。文字列,int, float/double
を扱った例です。 <<
operator が、型に応じた出力を行ってくれます。 string
クラスも利用可能です。
後半部は、浮動小数点に関する、やや細かい例です。興味がある人はどうぞ。
まずは実行させてください。Eclipse を使っているなら、例えば、新しい HelloWorld を作成して、下記の文面を copy & paste すればOKです。
#include <iostream>
using namespace std; /* std という namespace を使う */
int main(void) {
cout << "Hello World" << endl; /* 文字列 */
cout << 10 << " " << 1.0 << endl; /* 整数、浮動小数 */
char name[] = "Taro"; /* 文字列 */
cout << "Hello World, " << name << endl;
/* 浮動小数点などの込み入った表示例 */
double v =(((1.0)/1000)/1000)/1000;
cout << v << " ";
cout.setf(ios::fixed, ios::floatfield);
cout.precision(6);
cout << v << " ";
cout.precision(12);
cout << v << endl;
cout.unsetf(ios::fixed);
cout << 1.0/0.0 << " " << 0.0/0.0 << endl;
return 0;
}
フォーマット指定をしたい場合は、C 言語同様 printf
を使った方が楽かもしれませんね。#include <cstdio>
を行えば、利用可能です。
標準入力
標準入力から、データを取り込むには、cin
と >>
operator を使うと楽でしょう。
以下が簡単なサンプル(scanTest.cpp)です。long, double
などのフォーマットの変化に注意しましょう。
文字列にはstringクラスを利用し、一行読み込みに関しては、getline()
を使っています。
C 言語形式の文字配列が欲しければ、``char * cstr = str.c_str();` で取得可能です。
#include <iostream>
using namespace std;
int main(void) {
int x;
cin >> x; /* int への読み込み */
cout << "Input: " << x << endl;
int y[2];
cin >> y[0] >> y[1]; /* 配列要素への読み込み */
cout << "Input: " << y[0] << ", " << y[1] << endl;
float v;
cin >> v; /* float の読み込み */
cout << "Input: " << v << endl;
double v2;
cin >> v2; /* double の読み込み */
cout << "Input: " << v2 << endl;
string str;
cin >> str; /* 空白で区切られた文字の読み込み */
cout << "Input: " << str << endl;
string line;
getline(cin,line);
cout << "Input: " << line << endl;
getline(cin,line);
cout << "Input: " << line << endl;
return 0;
}
与えるデータはこちら。
1234567890
1 2
3
4
Who are you!
Who are you!
注意1: Eclipse で実行中は、入力は標準出力が表示されているコンソールから行ってください。
注意2: 文字列の取り込みの際は、char\[\]
にデータを格納することも可能です。ただし、バッファオーバフローへの対応などが必要です。コンテストの場合は、入力データサイズに制限があると思いますので、それにそってバッファサイズを決めることになるでしょう。
一文字単位の入力処理
char
型の変数を対象に >>
してもよいですし、get()
メソッドを使ってもらっても構いません。
ファイルの終わりは、eof()
メソッドで判定可能です。
以降は、ファイル入出力に取り組みたい人だけ見てくれればOKです。
ファイル出力 (ofstream)
ファイル名を指定して、ofstream
クラスのインスタンスを作成し、書き込みを行います。 使い方は cout
とほとんど一緒です。
こんな感じ(printTest2.cpp)。#include
の変化も注意。最後のclose()
も忘れずに。
#include <iostream>
#include <fstream>
using namespace std;
int main(void) {
char filename[] = "output.txt";
ofstream out(filename);
/* あるいは、
ofstream out;
out.open(filename); */
if(!out) {
cout << "Can't open file: " << filename << endl;
return 1;
}
out << "Hello World" << endl; /* 文字列 */
out << 10 << " " << 1.0 << endl; /* 整数、浮動小数 */
char name[] = "Taro"; /* 文字列 */
out << "Hello World, " << name << endl;
/* 浮動小数点などの込み入った表示例 */
double v =(((1.0)/1000)/1000)/1000;
out << v << " ";
out.setf(ios::fixed, ios::floatfield);
out.precision(6);
out << v << " ";
out.precision(12);
out << v << endl;
out.unsetf(ios::fixed);
out << 1.0/0.0 << " " << 0.0/0.0 << endl;
out.close();
return 0;
}
ファイル入力 (ifstream)
ファイル名を指定して、ifstream
クラスのインスタンスを作成し、書き込みを行います。 使い方は cin
とほとんど一緒です。
こんな感じ(scanTest2.cpp)。#include
の変化も注意。最後のclose()
も忘れずに。
#include <iostream>
#include <fstream>
using namespace std;
int main(void) {
char filename[] = "scanTest.txt";
ifstream in(filename);
/* あるいは、
ifstream in;
in.open(filename); */
if(!in) {
cout << "Can't open file: " << filename << endl;
return 1;
}
int x;
in >> x; /* int への読み込み */
cout << "Input: " << x << endl;
int y[2];
in >> y[0] >> y[1]; /* 配列要素への読み込み */
cout << "Input: " << y[0] << ", " << y[1] << endl;
float v;
in >> v; /* float の読み込み */
cout << "Input: " << v << endl;
double v2;
in >> v2; /* double の読み込み */
cout << "Input: " << v2 << endl;
string str;
in >> str; /* 空白で区切られた文字の読み込み */
cout << "Input: " << str << endl;
string line;
getline(in,line);
cout << "Input: " << line << endl;
getline(in,line);
cout << "Input: " << line << endl;
in.close();
return 0;
}