《C++ Primer Plus(第六版)》(41)(第十七章 输入、输出和文件 编程练习和答案1)...

17.8 编程练习

1.编写一个程序计算输入流中第一个$之前的字符数目,并将$留在输入流中。

//  
//  main.cpp  
//  HelloWorld  
//  
//  Created by feiyin001 on 16/12/30.  
//  Copyright (c) 2016年 Fable. All rights reserved.  
//  
#include <iostream>  
#include <iomanip>   
using namespace std;

int main()
{
	char ch;
	int counter = 0; 
	while ( cin.peek() != '$' && cin.get(ch) )
	{ 
		++counter;
	} 
	return 0;
}

2.编写一个程序,将键盘输入(直到模拟的文件尾)复制到通过命令行指定的文件中。

//  
//  main.cpp  
//  HelloWorld  
//  
//  Created by feiyin001 on 16/12/30.  
//  Copyright (c) 2016年 Fable. All rights reserved.  
//  
#include <iostream>  
#include <iomanip>
#include <fstream>
using namespace std;

int main(int argc, char* argv[])
{
	if (argc <= 1)
	{
		cout << "need file name" << endl;
		exit(0);
	}
	string fileName = argv[1];
	ofstream fo(fileName.c_str());
 
	char ch; 
	while ( cin && cin.get(ch) )
	{ 
		fo << ch;
	} 
	fo.close();
	return 0;
}

3.编写一个程序,将一个文件复制到另一个文件。让程序通过命令行获取文件名。如果文件无法打开,程序将支出这一点。

//  
//  main.cpp  
//  HelloWorld  
//  
//  Created by feiyin001 on 16/12/30.  
//  Copyright (c) 2016年 Fable. All rights reserved.  
//  
#include <iostream>  
#include <iomanip>
#include <fstream>
using namespace std;

int main(int argc, char* argv[])
{
	if (argc <= 2)
	{
		cout << "format: xx.exe sourceName targetName" << endl;
		exit(0);
	}
	ifstream io(argv[1]);
	if (!io.is_open())
	{
		cout << "can not find file: " << argv[1] << endl;
		exit(0);
	}
	ofstream fo(argv[2]);
 
	char ch; 
	while ( io && io.get(ch) )
	{ 
		fo << ch;
	} 
	fo.close();
	io.close();
	return 0;
}

4.编写一个程序,它打开两个文本文件进行输入,打开一个文本文件进行输出。该程序将两个输入文件中对应的行并接起来,并用空格分隔,然后将结果写入到输出文件中。如果一个文件比另一个文件短,则将较长的文件中余下的几行直接复制到输出文件中。例如,假设第一个输入文件的内容如下:

eggs kites donuts

balloons hammers

stones

而第二个输入文件的内容如下:

zero lassitude

finance drama

则得到文件的内容将如下:

eggs kites donuts zero lassitude

balloons hammers finance drama

stones

//  
//  main.cpp  
//  HelloWorld  
//  
//  Created by feiyin001 on 16/12/30.  
//  Copyright (c) 2016年 Fable. All rights reserved.  
//  
#include <iostream>  
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
	if (argc <= 3)
	{
		cout << "format: xx.exe sourceName targetName" << endl;
		exit(0);
	}
	ifstream fi1(argv[1]);
	if (!fi1.is_open())
	{
		cout << "can not find file: " << argv[1] << endl;
		exit(0);
	}
	ifstream fi2(argv[2]);
	if (!fi2.is_open())
	{
		cout << "can not find file: " << argv[2] << endl;
		exit(0);
	}

	ofstream fo(argv[3]);
 
	string line1;
	string line2;
	while (true)
	{ 
		if (fi1)
		{
			getline(fi1, line1);
		}
		if (fi2)
		{
			getline(fi2, line2);
		}
		if (!fi1 && !fi2)
		{
			break;
		}
		else if (fi1 && fi2)
		{
			fo << line1 << " " << line2 << endl;
		}
		else if (!fi1 && fi2)
		{
			fo << line2 << endl;
		}
		else if (fi1 && !fi2)
		{
			fo << line1 << endl;
		}
	} 
	fo.close();
	fi1.close();
	fi2.close();
	return 0;
}

5.Mat和Pat想邀请他们的朋友来参加派对,就像第16章中的编程练习8那样,但现在他们希望程序使用文件。他们请您编写一个完整的下属任务的程序。

·从文本文件mat.dat中读取Mat朋友的姓名清单,其中每行为一个朋友。姓名将被存储在容器,然后按顺序显示出来。

·从文本文件pat.dat中读取Pat朋友的姓名清单,其中每行为一个朋友。姓名将被存储在容器中,然后按顺序显示出来。

·合并两个清单,删除重复的条目,并将结果保存在文件matnpat.dat中,其中每行为要给朋友。

//  
//  main.cpp  
//  HelloWorld  
//  
//  Created by feiyin001 on 17/01/04.  
//  Copyright (c) 2016年 Fable. All rights reserved.  
//  
#include <iostream>  
#include <iomanip>
#include <fstream>
#include <string>
#include <set>
using namespace std;

int main(int argc, char* argv[])
{
	if (argc <= 3)
	{
		cout << "format: xx.exe sourceName targetName" << endl;
		exit(0);
	}
	ifstream fi1(argv[1]);
	if (!fi1.is_open())
	{
		cout << "can not find file: " << argv[1] << endl;
		exit(0);
	}
	ifstream fi2(argv[2]);
	if (!fi2.is_open())
	{
		cout << "can not find file: " << argv[2] << endl;
		exit(0);
	}

	ofstream fo(argv[3]);
 
	set<string> friends1;
	set<string> friends2;
	string str; 
	while (fi1 && getline(fi1, str))
	{ 
		friends1.insert(str);
	} 
	while (fi2 && getline(fi2, str))
	{ 
		friends2.insert(str);
	}
	set<string> friend3;
	friend3.insert(friends1.begin(), friends1.end());
	friend3.insert(friends2.begin(), friends2.end());
	cout << "Mat's friends: ";
	for (string s : friends1)
	{
		cout << s << " ";
	}
	cout << endl;
	cout << "Pat's friends: ";
	for (string s : friends2)
	{
		cout << s << " ";
	}
	cout << endl; 
	for (string s : friend3)
	{
		fo << s << endl;
	} 
	fo.close();
	fi1.close();
	fi2.close();
	return 0;
}







转载于:https://www.cnblogs.com/fablegame/p/6430223.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值