作业13: 文件的输入与输出

作业13: 文件的输入与输出

/*************************** 第13章  14题  P458   ******************************

4.建立两个磁盘文件 f1.dat 和 f2.dat,编程序实现以下工作:
1)从键盘输入20个整数,分别存放在两个磁盘文件中(每个文件中放10个整数);
2)从f1.dat读入10个数,然后存放到f2.dat文件原有数据的后面;
3)从f2.dat中读入20个整数,将它们按从小到大的顺序存放到f2.dat(不保留原来的数据)

******************************************************************************/


// T4_RWFile.cpp 
/*************************** 第13章  14题  P458   ******************************
4.建立两个磁盘文件 f1.dat 和 f2.dat,编程序实现以下工作:
1)从键盘输入20个整数,分别存放在两个磁盘文件中(每个文件中放10个整数);
2)从f1.dat读入10个数,然后存放到f2.dat文件原有数据的后面;
3)从f2.dat中读入20个整数,将它们按从小到大的顺序存放到f2.dat(不保留原来的数据)
******************************************************************************/

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
 	int onceagain = 0;
 	do
 	{
// statement
   char* stat =
"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
"   该程序实现的功能:建立两个磁盘文件 f1.dat 和 f2.dat,\n"
"1)从键盘输入20个整数,分别存放在两个磁盘文件中(每个文件中放10个整数);\n"
"2)从f1.dat读入10个数,然后存放到f2.dat文件原有数据的后面;\n"
"3)从f2.dat中读入20个整数,将它们按从小到大的顺序存放到f2.dat(不保留原来的数据) \n"
"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
"     要开始咯    GO... GO... GO... \n\n";
   cerr << stat;

 	
// 从键盘输入20个整数

	int a[20];								// 用作储存数字
	int i;					  				// 用作循环条件
	cerr << "输入20个整数:(每输入一个按一下回车):" << endl;
	for(i = 1; i <= 20; ++i)
	{
		cout << "第 " << i << " 个:  ";
		cin >> a[i-1];
	}
  cout << endl << "输入完毕!" << endl << endl;
  
// 分别存放在两个磁盘文件中(每个文件中放10个整数)

  ofstream outfile;		    	// 定义 ofstream 类(输出文件流类)对象 outfile
  ifstream infile;				// 定义 ifstream 类(输入文件流类)对象 infile
  const char * f1 = "f1.dat";
  const char * f2 = "f2.dat";

	
	
	cerr << "开始写入数据到 " << f1 << " 文件..." << endl;
	outfile.open(f1, ios::out);         	//.open()  f1.dat
	for(i = 0; i < 10; ++i)
	{
		outfile << a[i] << endl;
		cerr << a[i] << endl;
	}
	outfile.close();						// .close()  f1.dat
	cerr << "写入成功!" << endl << endl;


	cerr << "开始写入数据到 " << f2 << " 文件..." << endl;
	outfile.open(f2, ios::out);          	//.open()    f2.dat
	for(i = 10; i < 20; ++i)
	{
		outfile << a[i] << endl;
		cerr << a[i] << endl;
	}
	outfile.close();					   // .close()  f2.dat
	cerr << "写入成功!" << endl << endl;	
	cerr << endl << "Yeah! 第一任务执行成功。" << endl << endl; 
	
	cerr << "请按回车键继续..." << endl; 
	cin.get();
	cin.get();
	
// 从f1.dat读入10个数,然后存放到f2.dat文件原有数据的后面;
// 读取  // 写入 ios::app 可加到末尾 ios::ate 文件指针指向末尾
    int t[20];
	infile.open(f1, ios::in);				//.open() f1.dat
	outfile.open(f2, ios::out | ios::app);	//.open() f2.dat
	cerr << "正在从 " << f1 
		 << " 读入10个数,然后存放到 " << f2 << " 文件原有数据的后面..." 
		 << endl;
	for(i = 0; i < 10; ++i)
	{
        cerr << "- -正在读取第 " << i+1 << " 个数: "; 
		infile >> t[i];
		cerr << t[i] << endl;
		cerr << " + 正在写入第 " << i+1 << " 个数: ";
		outfile << t[i] << endl;
		cerr << t[i] << endl;
	}
	infile.close();							//.close() f1.dat
	outfile.close();						//.close() f2.dat
	cerr << "Yeah! 第二任务执行成功。" << endl << endl; 
	
	cerr << "请按回车键继续..." << endl; 
	cin.get();
	

// 从f2.dat中读入20个整数,将它们按从小到大的顺序存放到f2.dat(不保留原来的数据)

// 读取 
    infile.open(f2, ios::in);
    for(i = 0; i < 20; ++i)
	{
	infile >> t[i];
	cerr << "成功读取第 " << i+1 << " 个数: " << t[i] << endl; 
	}
    infile.close();

// 排序 -- 选择法 
   int k;
   int j; 
   int min;
   for (i = 0; i < 20; ++i)
   {
   	   k = i;
   	   min = t[i];
   	 // 找出最小值 
   	   for (j = i; j < 20; ++j)
   	   	  if (t[j] < min)
   	   	   {
   	   	   	  min = t[j];
   	   	   	  k = j;
	  	  }
    // 换位 
 		 min = t[k];		// min此时只作temp临时比较作用 
 		 t[k] = t[i];
 		 t[i] = min;
    }


	cerr << endl;
// 输出 
    outfile.open(f2, ios::out);
    for(i = 0; i < 20; ++i)
	{
	outfile << t[i] << endl;
	cerr << "成功输出第 " << i+1 << " 个数: " << t[i] << endl; 
	}
	outfile.close();
	cerr << endl << "Yeah! 成功排序输出!第三任务执行成功!" << endl; 
	
	
	cerr << endl << "若想退出,请输入0,否则请输入其它数字:"; 
	cin >> onceagain;
	cout << endl << endl;
	}while(onceagain);
	
	return 0;
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值