// page226.cpp : 定义控制台应用程序的入口点。
//C++ 面向对象程序设计
//从用户指定的文件读取3个数,求和,并将结果写入用户指定的另一个文件
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <fstream>
int main()
{
using namespace std ;
int a ;
char in_file_name[16], out_file_name[16];
ifstream in_stream ;
ofstream out_stream;
cout << " I will sum three numbers taken from an input\n"
<< " file and write the sum to output file .\n";
cout << " Enter the input file name (maximum of 15 characters):\n";
cin >> in_file_name ;
cout << " Enter the input file name (maximum of 15 characters):\n";
cin >> out_file_name;
cout << " I will read numbers from the filev "
<< in_file_name << " and\n"
<< " place the sum in file "
<< out_file_name << endl ;
in_stream.open(in_file_name);
if (in_stream.fail())
{
cout << " input file opening failed.\n";
exit(1);
}
out_stream.open(out_file_name);
if(out_stream.fail())
{
cout << " ouput file opening failed .\n";
exit(1);
}
int frist , second ,third;
in_stream >> frist >> second >> third ;
out_stream << " The sum of first 3\n"
<< " numbers in " << in_file_name << endl
<< " is " << (frist + second + third)
<< endl ;
in_stream.close();
out_stream.close();
cout << " End of Program.\n";
cin >> a ;
return 0;
}
//C++ 面向对象程序设计
//从用户指定的文件读取3个数,求和,并将结果写入用户指定的另一个文件
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <fstream>
int main()
{
using namespace std ;
int a ;
char in_file_name[16], out_file_name[16];
ifstream in_stream ;
ofstream out_stream;
cout << " I will sum three numbers taken from an input\n"
<< " file and write the sum to output file .\n";
cout << " Enter the input file name (maximum of 15 characters):\n";
cin >> in_file_name ;
cout << " Enter the input file name (maximum of 15 characters):\n";
cin >> out_file_name;
cout << " I will read numbers from the filev "
<< in_file_name << " and\n"
<< " place the sum in file "
<< out_file_name << endl ;
in_stream.open(in_file_name);
if (in_stream.fail())
{
cout << " input file opening failed.\n";
exit(1);
}
out_stream.open(out_file_name);
if(out_stream.fail())
{
cout << " ouput file opening failed .\n";
exit(1);
}
int frist , second ,third;
in_stream >> frist >> second >> third ;
out_stream << " The sum of first 3\n"
<< " numbers in " << in_file_name << endl
<< " is " << (frist + second + third)
<< endl ;
in_stream.close();
out_stream.close();
cout << " End of Program.\n";
cin >> a ;
return 0;
}