c++怎么从命令行获取参数及参数的合法性检测?习题为C++primer第五版中的练习题

本文介绍如何在C++中从命令行获取参数,并以C++ Primer第五版的习题为例,讲解参数合法性检测。通过创建Debug程序,然后在DOS环境下运行cmd,进入程序目录并输入主程序名及参数来执行程序。
摘要由CSDN通过智能技术生成

#include <iostream>
#include "Sales_data.h"
#include <fstream>

using namespace std;
int main(int argc,char *argv[])
{
	if (argc!=3)
	{
		cerr<<"请给出文件名"<<endl;
		return -1;
	}
	ifstream input(argv[1]);
	if (!input)
	{
		cerr<<"无法打开输入文件"<<endl;
		return -1;
	}
	ofstream output(argv[2]);
	if (!output)
	{
		cerr<<"无法打开输出文件"<<endl;
	}

// 	ifstream input("Salesin.text");
// 	ofstream output("Salesout.text");

	Sales_data total;
	if (read(input,total))
	{
		Sales_data trans;
		while(read(input,trans))
		{
			if (total.isbn()==trans.isbn())
			{
				total.combine(trans);
			}
			else
			{
				print(output,total)<<endl;
				total=trans;
			}
		}
		print(output,total)<<endl;
	}
	else
	{
		cerr<<"NO data?!"<<endl;
	}
	return 0;
}

注:头文件#include "Sales_da

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值