C++ PrimerPlus第六版第十七章输入、输出和文件编程练习答案

1.

#include <iostream>
int main()
{
   
	char input[255];
	std::cin.get(input, 255, '$');
	std::cout << "字符数: " << strlen(input) << std::endl;

	std::cout << "下一个字符: "<< (char)std::cin.get() << "\n";
	return 0;
}

2.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

int main(int argc, char * argv[])
{
   
	using namespace std;
	ofstream fout;
	for (int file = 1; file < argc; file++)
	{
   
		fout.open(argv[file]);
		if (!fout.is_open())
		{
   
			cerr << "Can't open " << argv[file] << endl;
			system("pause");
			exit(EXIT_FAILURE);
		}
		cout << "Please enter your input (enter a blank line to quit): \n";
		string input;
		while (getline(cin, input) && input.size() > 0)
			fout << input << endl;
		cout << "Input over.\nBye.\n";
		fout.close();
	}

	return 0;
}

3.

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
   
	char ch;
	ifstream fin;
	ofstream fout;
	if (argc < 3)
	{
   
		cerr << "参数不够"  << endl;
		exit(EXIT_FAILURE);
	}
	fin.open(argv[1]);
	fout.open(argv[2]);
	if (!fin.is_open())
	{
   
		cerr << "Could not open " << argv[1] << endl;
		exit(EXIT_FAILURE);
	}
	if (!fout.is_open())
	{
   
		cerr << "Could not open " << argv[2] << endl;
		exit(EXIT_FAILURE);
	}
	cout << "open successfully.\n";
	while (fin.get(ch))
		fout << ch;
	cout << "Copy and paste successfully.\nBye.\n";
	fin.close();
	fout.close();
	return 0;
}

4.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
   
	char ch;
	ifstream fin, fin1;
	ofstream fout;
	char file1[] = "helloworld.txt";
	char file2[] = "areyouok.txt";
	char file3[] = "hello.txt";
	fin.open(file1);
	fin1.open(file2);
	fout.open(file3);
	if (!fin.is_open())
	{
   
		cerr << "Could not open :" << file1  << endl;
		exit(EXIT_FAILURE);
	}
	if (!fin1.is_open())
	{
   
		cerr << "Could not open :" << file2  << endl;
		exit(EXIT_FAILURE);
	}
	if (!fout.is_open())
	{
   
		cerr << "Could not open " <<   file3 << endl;
		exit(EXIT_FAILURE);
	}
	cout << "open successfully.\n";
	string temp;
	string temp1;
	int a = 0;
	int b = 0;
	while (getline(fin, temp))
		++a;
	while (getline(fin1, temp1))
		++b;
	fin.clear();
	fin1.clear();
	fin.close();
	fin1.close();
	fin.open(file1);
	fin1.open(file2);
	if (a >= b)
	{
   
		for (int i = 0; i < a; ++i)
		{
   
			getline(fin, temp);
			fout << temp << " ";
			if (i < b)
			{
   
				getline(fin1, temp1);
				fout << temp1 << endl;
			}
		}
	}
	else
	{
   
		for (int i = 0; i < b; ++i)
		{
   
			getline(fin1, temp1);
			fout << temp1 << " ";
			if (i < a)
			{
   
				getline(fin, temp);
				fout << temp << endl;
			}
		}
	}

	cout << "Copy and paste successfully.\nBye.\n";
	fin.close();
	fout.close();
	return 0;
}

5.

#include <iostream>
#include <fstream>
#include <cstdlib>
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值