trouble with arguments in main method

This is a discussion on trouble with arguments in main method within the C Programming forums, part of the General Programming Boards category; Hi, I'm having some trouble with my C assignment. My assignment is based on an unix program that performs translations, ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    53

    trouble with arguments in main method

    Hi,

    I'm having some trouble with my C assignment. My assignment is based on an unix program that performs translations, deletions, uppercase, lowercase, and squeezing.

    My basic problem right now is my main method. I want have an output like this to access the various options listed above.
    a -u<test.txt> output.txt

    -a is used to run the program in gvim

    but I cannot get this output. I get a message saying: tr: invalid option --u

    Here's my code for the main method:
    Code:
    int main(int argc, char *argv[])
    {
    	int i;
    	FILE *fp;
    	char varchar;
    	char s[BUFSIZE];
    
    	if(argc > 2)  /*two arguments are allowed*/
    	{
    		fprintf(stderr, "usage:%s file....\n", argv[0]);
    		return 1;
    	}
    	if((fp = fopen(argv[0], "w"))==0)
    	{
    		perror("fopen");
    		return 1;
    	}
    	for(i=0; i<argc; i++)
    	{
    		if(argv[i][0] == '-')
    		{
    			if(argv[i][1] == 'u')
    				upper(s);
    			if(argv[i][1] == 'l')
    				lower(s);
    			if(varchar == 'h')
    				display(s);
    			else
    				fprintf(stderr, "invalid input\n");
    			    fprintf(stderr, "Please try 'tr -h' for help\n");
    		}
    		else
    			fprintf(stderr, "first argument problem\n");
    	}
    	fclose(fp);
    	return 0;
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    I do not understand your explanaition
    do you plan to run the program as

    a -u<test.txt> output.txt

    in this case
    argc = 3
    argv[0] = "a"
    argv[1]="-u<test.txt>"
    argv[2]="output.txt"

    But your code shows something different

    ...
    Code:
    if(varchar == 'h')
    varchar is not initialized. This line is executed always (should it be?) with unpredictable results

    fprintf(stderr, "Please try 'tr -h' for help\n");
    this line is always executed also inside the codition branch if(argv[i][0] == '-')
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    53
    Quote Originally Posted by vart View Post
    I do not understand your explanaition
    do you plan to run the program as

    a -u<test.txt> output.txt

    in this case
    argc = 3
    argv[0] = "a"
    argv[1]="-u<test.txt>"
    argv[2]="output.txt"
    "a" - is what I use to run the program, I used a compiler from gvim

    argv[0]="-"
    argv[1]="u<test.txt>"
    argv[2]="output.txt"

    this is what I'm trying to do right now

    I actually have moved onto a new design in my code, so far its a bit successful
    Code:
    int main(int argc, char *argv[])
    {
    	int i;
    	char s[BUFSIZE];
    	FILE *fp;
    
    	if(argc != 2 && argc != 3)
    	{
    		fprintf(stderr, "usage:&#37;s file....\n", argv[0]);
    		return 1;
    	}
    	
    	for(i=1; i<argc; i++)
    	{
    		if(argc == 2)
    		{
    
    			if(strcmp(argv[1], "-l") == 0)
    			{
    				lower(s);
    			}
    			else if(strcmp(argv[1], "-u") == 0)
    			{
    				upper(fp);
    			}
    			else
    			{
    				 fprintf(stderr, "ok\n");
    			 }
    		 }
    		 else
    			 fprintf(stderr, "ok\n");
    	}
           return 0;
    }

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    argv[0] is a program name (with or without location depending on the environment)
    "-" to be reported as full argument should be delimited with space from the next part...

    have you tested your settings using simple program:
    Code:
    int main(int argc, char* argv[])
    {
       int i;
       printf("Number of args &#37;d\n", argc);
       for(i=0; i<argc;i++)
          printf("%d: <%s>\n", i,argv[i]);
    }
    ???
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. arguments in main
    By cdonlan in forum Linux Programming
    Replies: 3
    Last Post: 01-23-2005, 11:59 PM
  4. main function arguments
    By myjay in forum C++ Programming
    Replies: 2
    Last Post: 09-29-2003, 08:33 PM
  5. void or int for main?:)
    By bigtamscot in forum C Programming
    Replies: 13
    Last Post: 09-27-2001, 03:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21