reading & handling command line args

Hi im i student working on a assignment and need some help. we have to do is read in arguments from command line (-i inputTxt.txt -o outputTxt.txt) and then assign the inputTxt.txt/outputTxt.txt to string values.Cant seem to get my method working.Smashing my head against the wall to get it working.any ideas would be much appreiated thnk in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//-------------------------------------------
//
void arraySort::runProgram(int argc,char *argv[])
{
    string inputFile = "";
    string outputFile = "";   
     int row = 1;
				
    if(argc > 5)
    {
        cerr << "Error invalid number of command line arguments";
        exit(1);
    }
    while(argc > row)
    {
	if(argv[row] == "-i")
	{	
	    row++;
	    if(argc > row)
	    {		
	         tmp1 = argv[row];
	        inputFile = tmp1;
	    }	 
	}	
	if(argv[row] == "-o")
	{	
	        row++;
	        if(argc > row)
	        {		
		tmp2 = argv[row];
		outputFile = tmp2;
	        }
    	}
    	row++;
	}
	if(inputFile == outputFile)
	{
	    cerr << "Error fileNames are the same";
	    exit(1);
	}
}
// inputFile and outputFile are private string class variables. 
Last edited on
Is it not compiling? Or is it just not producing the expected output?
not producing the expected output.its nots reading the both files from the command line into the string variable in the method. what we have to do is read in strings from a input txt file then put those string into array of non dupicates then we sort it and then we write it out to the outputfile. The problem is for some reason its not assiging the files from the argv[row] to the appriate string variables that in this method(inputFile,outputFile) i alway get the error FileNames are the same) the -i and the -o are flags what ever is behind -i is the inputfile and the -o is the outputfile. just cant seem to figure it out eh.
Last edited on
Can u print the inputFle and outputFile and see what its reading?
try that nothing printing out.
Last edited on
Just updated the code to reflect changes, errors still remain, cannot open inputFile...
will now check if values are being stored.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//-------------------------------------------
//
void arraySort::runProgram(int argc,char *argv[])
{
    string inputFile = "";
    string outputFile = "";   
     string com= argv[row];
     int row = 1;
				
    if(argc > 5)
    {
        cerr << "Error invalid number of command line arguments";
        exit(1);
    }
    while(argc > row)
    {
	if(com== "-i")
	{	
	    row++;
	    if(argc > row)
	    {		
	         tmp1 = argv[row];
	        inputFile = tmp1;
	    }	 
	}	
	if(com == "-o")
	{	
	        row++;
	        if(argc > row)
	        {		
		tmp2 = argv[row];
		outputFile = tmp2;
	        }
    	}
    	row++;
	}
	if(inputFile == outputFile)
	{
	    cerr << "Error fileNames are the same";
	    exit(1);
	}
}
// inputFile and outputFile are private string class variables. 

solved u cannot compare char and string directly have to make a separted variable for comparision.
You can't use 'row' on line 7 before you declare it on line 8
Topic archived. No new replies allowed.