c++ int main(int argc, char *argv[]) 与 cmd中输入问题

来源:百度知道 编辑:UC知道 时间:2024/06/17 14:59:15
两个文件中分别有一个2元数组, 程序要求是 把2个文件中的数组 相加或 相乘 在cmd.exe 中 输入 123.exe -a a.txt b.txt c.txt
a.txt和 b.txt 为输入文件 c.txt 为输出文件。

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

int main(int argc, char *argv[])
{
ifstream inStream1, inStream2;
ofstream outStream;

int arr1[100][100];
int arr2[100][100];
int row, col;
int m, n;
int a, b;

inStream1 >> col >> row;
for(m = 0; m < row; m++ )
for(n = 0; n < col; n++)
inStream1 >> arr1[m][n];
///////////// take the numbers in input1.txt to arr1[][];/////////////

inStream2 >> b >> a;
for(m = 0; m < row; m++ )
for(n = 0; n < col; n++)
inStream2 >> arr2[m][n];
///////////// take the numbers in input2.txt to arr2[][];/////////////

switch( *argv[0] )
{
case '-

switch( *argv[0] )这条不正确,argv[0]是程序名称,看你的程序,argv[1]里才是选项。

另外,switch参数可以使数值或字符,不能使字符串,所以case '-b'这样的格式是不行的,如果你输入的格式是123.exe -x 1.txt 2.txt 3.txt,那么可以用
switch( argv[1][1] ) {
case 'b'
……
如果你输入的格式是123.exe x 1.txt 2.txt 3.txt,那么可以用
switch( argv[1][0] ) {
case 'b'
……