#include <iostream>
#include <fstream>
#include <string>
using std::cout;
using std::ifstream;
using std::string;
int main()
{
int winwidth,winheight,wincolor; // Declare your variables
winwidth = winheight = wincolor = 0; // Set them all to 0
string path = "filename.txt"; // Storing your filename in a string
ifstream fin; // Declaring an input stream object
fin.open(path); // Open the file
if(fin.is_open()) // If it opened successfully
{
fin >> winwidth >> winheight >> wincolor; // Read the values and
// store them in these variables
fin.close(); // Close the file
}
cout << winwidth << '\n';
cout << winheight << '\n';
cout << wincolor << '\n';
return 0;
}
ifstream は、cin を使用するのとほぼ同じ方法で、抽出演算子 >> とともに使用できます。明らかに、ファイル I/O にはこれ以外にも多くのことがありますが、要求に応じて、これでシンプルに保たれています。