Thread: HELP with READLINE.H

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    17

    Question HELP with READLINE.H

    Hello again

    Is possible to know how many args has been introduced by the user, without making any functions to do that, simple using my code:
    Code:
    char *s ;
    	int n = 0, q = 0 ;
    	int x,y;
    	char *c,*nome_f,e;
    
    	c = (char*) malloc(25 * sizeof(char)); 
    	nome_f = (char*) malloc(25 * sizeof(char));
    
    	read_history("historico"); 
    	using_history(); 
    	do {  
      		s = readline("Letrorium>");  
    		if ( *s != 'q' ) {
    /*AT THIS POINT I WANT KNOW HOW MANY ARGS HAS BEEN INTRODUCED (2, 3 ,4 MORE... SEPARATED BY SPACES);
    			add_history(s);
           			n++;
    Thanks in advanced

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    36

    Number of Arguments:

    You can find the number of command line arguments passed by the user using argc variable.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    I know i can use argc to know the number of args. But i already try to put like so many examples over internet and always count wrong args.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Show an example, and we'll see if we can't correct it. Hard to believe we couldn't get this right! This is a standard parameter, and used all the time, so it's better to use it, than to try and work out your own code for it.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    Here is an sample example.
    Code:
    #include<stdio.h>
    #include <readline/readline.h>
    #include <readline/history.h>
    int main(int argc,char *argv[])
    {
    char *s;
    s = readline("Letters>");
    printf("number of args: %d\n",argc);
    }

  6. #6
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Remember that the first element in argv is a pointer to the name of the program. This is also included in the parameter count variable argc. The count will always be 1 (for the name of the application) even if you don't specify parameters. If you specify one parameter argc will be 2, etc.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    Yes DeadPlanet i know that also.

    But my program stays always with the prompt of readline "Letters:>" and i need to count argc in this.

    If i put ./Letters testing, argc will be 2 and so on. After i put this my prompt changes to Letters and the user is able to put many comands inside to execute some jobs (this is my problem).

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So what you want to do is get a list of jobs from the user, for your program to then process?
    Last edited by Adak; 04-01-2010 at 06:53 AM.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    Yes Adak.

    When i run my program the prompt changes to Letters, because i'am using readline.h
    After the user should give me some comands that my program executes, but i want to check how many args as been inserted so if he puts more then my autorization sayz "INVALID NUMBER OF ARGS", if he puts 2 i do somethings, if he puts 4 i do another thing and so on.

    Thks

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So what they type doesn't go into argv/argc. What they type goes into s. If you want to know how many letters they typed, I suppose you can use strlen (although I have no idea whether readline gives you \n at the end of the string or not). If you want to know how many words they typed you'll have to parse that string, looking for spaces.

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    Thanks tabstop.

    That the way i already do the job.

    I just was looking for an better way. I don't really know if argv and argc can be used in readline.

    Thanks anyway

Popular pages Recent additions subscribe to a feed