Thread: help getting a grib on argc and argv

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    help getting a grib on argc and argv

    hello people i m srry i kinda dont understand the need of argc and argv like i dunno what they do like for example this code here it doesnt even run or anything
    Code:
    #include < stdio.h>
    int main(int argc, char** argv)
    {
        int i;
        printf("argc = %d\n", argc);
        for (i = 0; i < argc; i++)
    	    printf("argv[%d] = \"%s\"\n", i, argv[i]);
    	return 0;
    }
    it would be be cool if someone like give me the idea on how they work

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    To get a grip on command line arguments read the tutorial.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    oh i understand it now but will it only work in cmd ? like can i also use command line arguments to like produce a programmer that opens a txt ? without cmd like in a window ? or argc and argv used only with cmd ?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Please post clear questions? From your last post it is impossible to figure out what you're tryin' to say.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    like i got a txt file on the same folder i m in is it possible to make a program that opens it without needing to add do it from cmd like from the program itself?

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    like when you like have a text file like in the same like folder u like just add its path to like the cmd line of the program in like cmd like so you like can get like the filename from like argv and u like not have to like hardcode it like in the source code itself like that

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    like ROTFLOL

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    Quote Originally Posted by MWAAAHAAA View Post
    like when you like have a text file like in the same like folder u like just add its path to like the cmd line of the program in like cmd like so you like can get like the filename from like argv and u like not have to like hardcode it like in the source code itself like that
    english isnt my main language so if it makes you feel better to make fun of my talking then go ahead

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by elwad View Post
    english isnt my main language so if it makes you feel better to make fun of my talking then go ahead
    There's no need to butcher simple works like "sorry."

    And your language is so idiosyncratic I have a hard time believing you're not a native English speaker, but like, whatever and stuff.

    (Idiosyncratic doesn't mean "idiotic")
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    To answer a question that the OP may have been asking: it is possible to associate your program with .txt files (either by clicking on it normally or having a separate item in the right-click menu) so that when you choose to open a file with your program, the full path to the file you opened is passed as a command-line parameter to your program. I'm not going to say more than that in case I'm on the wrong track here . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    here is a sample assigment i did from the k&r book which uses the commandline arguments . i think some gui libraries uses commandline parameters internally to keep track of some things but i am not that far into my studies of c that i can say if that is true or not.
    Code:
    #include <stdio.h>
    #define IN 1
    #define OUT 0
    
    int main(int argc,char *argv[]){
    	
    	int c;
    	int nl,nw,nc,state;
    	    nl=nw=nc=state=0;
    	FILE *fp;
    	
    	if(argc != 2)
    	  {
    	  	fprintf(stderr," %s: too few arguments for format\n",argv[0]);
    	  	return 1;
    	  }
    	  
    	  if((fp=fopen(argv[1],"r")) != NULL)
    	  {
    		/* we opened a file to read it and count it*/
            while((c=getc(fp))!= EOF)
    	 {
    	     	   nc++;
    	     	if(c=='\n')
    	     	  nl++;
    	        if(c==' '||c=='\n'||c=='\t')
    	           state=OUT;
    	          else if(state == OUT)
    	          {
    	          	state = IN;
    	          	++nw;
    		  }
    	}
    		fprintf(stdout,"%d %d %d\n",nl,nw,nc);
    	}
            else
            {
    		fprintf(stderr,"error in opening file %s\n",argv[1]);
    		return 1;
    	}
    		return 0;
    }
    i am not sure if you can read it and i waiting for a eye operation so i read and write really bad at the moment but i hope it was helpful -(and i posted it in t he right context if not then ignore post please. )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using argc and argv data
    By Rad_Turnip in forum C Programming
    Replies: 4
    Last Post: 03-31-2006, 06:09 AM
  2. Converting a argc / argv construct into a va_list
    By chambece in forum C Programming
    Replies: 6
    Last Post: 07-03-2005, 04:47 PM
  3. argc & argv
    By miryellis in forum C Programming
    Replies: 11
    Last Post: 09-19-2004, 11:59 PM
  4. how do i? re: argc - argv
    By luigi40 in forum C Programming
    Replies: 2
    Last Post: 06-11-2004, 10:17 AM
  5. more argv and argc
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-08-2001, 11:04 PM