more argv and argc

This is a discussion on more argv and argc within the C Programming forums, part of the General Programming Boards category; Hi all, I am just trying to write a program that will echo the command line minus the program name... ...

  1. #1
    Unregistered
    Guest

    more argv and argc

    Hi all,
    I am just trying to write a program that will echo the command line minus the program name... here is my code, any idea why it doesn't work? Thanks!

    Code:
    #include <stdio.h>
    
    int main(int argc, char * argv [])
    {
      argv = argv+1;
      printf(" \n");
    
    while(**argv != NULL)
       {
         while (**argv != NULL)
         {
           putchar(**argv);
           ++(*argv);
         }
    
            argv = argv + 1;
    
       }
    
    	return 0;
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Good question. The first argument in main (int argc) is a counter of the number of different statements that are in the command line. Here's an example of what i'm trying to say:
    c:\>my_program open whatever.txt
    In this example argc will be 2 (3 arguments starting at 0). Looking for char **argv to be null can have errors when ran. So instead count argc down to 1 (0 is the path).

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    22
    Hi,
    Why you are complicating things try this one.


    By
    A.Sebasti...


    #include<stdio.h>
    int main(int argc,char * argv[])
    {
    int i=1;
    printf("<%d>\n",argc);
    while(argc!=1)
    {
    printf("<%s>\n",argv[i]);
    i++;
    argc--;
    }
    }

  4. #4
    Clarinetster
    Guest

    thanks

    I didn't even think about using argc! Thanks for the help.

    Clarinetster

  5. #5
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Code:
    int main(int argc, char * argv [])
    {
    	int i;
    	for(i=1;i<argc;i++) printf("%s",argv[i]);
    	return 0;
    }
    I compile code with:
    Visual Studio.NET beta2

  6. #6
    Unregistered
    Guest
    Code:
    #include <stdio.h>
    
    int main (int argc, char * argv [])
    {
     printf ("\n");
     argv++;
     while (*argv != NULL)
     {
      //printf ("%s ", *argv);
      //argv++;
      // reformatted so it looks like yours...
      while (**argv != "")
      {
       putchar (**argv);
       (*argv)++;
      }
      argv++; 
     }
     return 0;
    }
    The problem with your first code wat that is tested whether **argv was NULL, which **argv is a char, so it doesn't make sense.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    We had a question at UNI for this ages and ages ago. We also had to print them in reverse order and test if they used it wrong. I did the part your after like Witch_King. It is easier to understand and looks neater

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help getting a grib on argc and argv
    By elwad in forum C Programming
    Replies: 10
    Last Post: 05-01-2009, 11:13 AM
  2. Using argc and argv data
    By Rad_Turnip in forum C Programming
    Replies: 4
    Last Post: 03-31-2006, 06:09 AM
  3. Converting a argc / argv construct into a va_list
    By chambece in forum C Programming
    Replies: 6
    Last Post: 07-03-2005, 05:47 PM
  4. argc & argv
    By miryellis in forum C Programming
    Replies: 11
    Last Post: 09-20-2004, 12:59 AM
  5. how do i? re: argc - argv
    By luigi40 in forum C Programming
    Replies: 2
    Last Post: 06-11-2004, 11:17 AM

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