1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need help with option parsing arguments!!

Discussion in 'C' started by euler, Mar 13, 2011.

  1. euler

    euler New Member

    Joined:
    Mar 13, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hey!!

    I have a project for my university to be done.I have almost complete the whole program but i have little problem(i think...)

    the exercise tells me that the call of the program must be something like this:
    progname -c C1 C2 -f file1 file2 -o fileoutput

    C1,C2 are integers numbers
    can anyone plz help me..

    thanks.!
     
  2. eriyer

    eriyer New Member

    Joined:
    Jan 22, 2011
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    /* something on the foll lines */
    
    void main(int argc, char* argv[])
    {
    	int i, c1,  c2, opt = 0, error = 0;
    	char *file1, *file2, *fileoutput;
    	
    	for( i = 1; i < argc && opt <= 'o' ; i++ )
    	{
    		if( ! strcmp(argv[ i ], "-c" )
    		{
    			if( opt == 0 )
    			{
    				opt = 'c';
    				continue;
    			}
    			else
    			{
    				error = 1;
    			}
    				
    		}
    		else if( opt == 0 )  /* expecting -c */
    			error = 1;
    		
    		if( ! strcmp(argv[ i ], "-f" )
    		{
    			if( opt == 'e' )
    			{
    				opt = 'f';
    				continue;
    			}
    			else
    			{
    				error = 1;
    			}
    		}
    		else if( opt == 'e' )  /* expecting -f */
    			error = 1;
    		
    		if( ! strcmp(argv[ i ], "-o" )
    		{
    			if( opt == 'h' )
    			{
    				opt = 'o';
    				continue;
    			}
    			else
    			{
    				error = 1;
    			}
    		}
    		else if( opt == 'h' ) /* expecting -o */
    			error = 1;
    		
    		if( error )
    		{
    			printf("parameters to program incorrect\n");
    			exit(1);
    		}
    		
    		if( opt == 'c' )
    		{
    			c1 = atoi(argv[ i ]);
    			opt = 'd';   /* next param for 2nd integer */
    		}
    			
    		if( opt == 'd' )  /* 2nd integer */
    		{
    			c2 = atoi(argv[ i ]);
    			opt = 'e';   /* prep to read file names */
    		}
    		
    		if( opt == 'f' )
    		{
    			f1 = argv[ i ];
    			opt = 'g'; 
    		}
    		
    		if( opt == 'g' )  /* 2nd filename */
    		{
    			f2 = argv[ i ];
    			opt = 'h'; 
    		}
    		
    		if( opt == 'o' )
    		{
    			fileoutput = argv[ i ];
    			opt = 'q';
    		}
    	}
    	
    	if( opt != 'q' || i < argc )
    	{
    		printf("parameters to program incorrect\n");
    		exit(1);
    	}
    		
    	/* rest of your program below */
    }
     
  3. mthushara

    mthushara New Member

    Joined:
    Aug 4, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    void main(int argc, char* argv[])
    {
    	int c1 = 0,  c2 = 0;
    	char *file1 = NULL, *file2 = NULL, *fileoutput = NULL;
    	bool bNoError = false;
    
    	if(argc != 9)
    	{
    		printf("invalid number of arguments\n");
    		printf("usage : progname -c C1 C2 -f file1 file2 -o fileoutput\n");
    		exit(1);
    	}
    
    	//////////////////////////////////////////////////////////////////////////
    
    	int i = 1;
    
    	if (strcmp(argv[i],"-c") == 0)
    	{	
    		i++;
    		c1 = atoi(argv[i]);
    
    		i++;
    		c2 = atoi(argv[i]);
    
    		bNoError = true;
    	}
    
    	i++;
    	if (strcmp(argv[i],"-f") == 0)
    	{	
    		i++;
    		file1 = argv[i];
    
    		i++;
    		file2 = argv[i];
    
    		bNoError &= true;
    	}
    
    	i++;
    	if (strcmp(argv[i],"-o") == 0)
    	{	
    		i++;
    		fileoutput = argv[i];
    
    		bNoError &= true;
    	}
    
    	//////////////////////////////////////////////////////////////////////////
    	
    
    	if(bNoError == false)
    	{
    		printf("parameters to program incorrect\n");
    		exit(1);
    	}
    	else
    	{
    		printf("argv[1] : %s\n",argv[1]);
    		printf("argv[2] : %d\n",c1);
    		printf("argv[3] : %d\n",c2);
    		printf("argv[4] : %s\n",argv[4]);
    		printf("argv[5] : %s\n",file1);
    		printf("argv[6] : %s\n",file2);
    		printf("argv[7] : %s\n",argv[7]);
    		printf("argv[8] : %s\n",fileoutput);
    	}
    }
     

Share This Page