示例1:
#include <iostream>
#include <fstream>
using namespace std;
int main(void)
{
ifstream file;
file.open("test.txt");
char ch;
int ch_count = 0;
while (file >> ch)
{
ch_count++;
}
file.close();
cout << "There are " << ch_count << " charactors in the file." << endl;
return 0;
}
示例2:
/*
* ex6-9.cpp
*
* Created on: Nov 11, 2016
* Author: Jamienstar
*/
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct contributor
{
string name;
double contribution;
};
int main(void)
{
ifstream file;
file.open("contributors.txt");
int contributor_num;
file >> contributor_num;
file.get();
contributor *contibutors = new contributor[contributor_num];
int i = 0;
int grand_count = 0;
while (i < contributor_num)
{
getline(file, contibutors[i].name