Combining multiple command line arguments


 
Thread Tools Search this Thread
Top Forums Programming Combining multiple command line arguments
# 1  
Old 04-09-2009
Combining multiple command line arguments

suppose the user enters:
Code:
./Myfile blah1 blah2 blah3

I want to be able to read that in as:
Code:
blah1blah2blah3

IN ONE VARIABLE

Obviously I can print it out in one line excluding the white space, but I'm having trouble combining argv[1], argv[2], ....., argv[i] together!

This is what I tried, but I clearly have no clue what I'm doing, because it didn't work:

Code:
int main(int argc, char* argv[])
{
    i = 1;

    while(argv[i] != NULL)
    {
        if(i > 1)
        {
            argv[1] = strncat(argv[i],argv[1],1000);
        }
        //printf("STRING LENGTH IS: %d", strlen(argv));
        printf("%s\n\n", argv[1]);
        i++;
    }

}


Can someone help me out? I'm stuck on probably the easiest part of this assignment, because I don't know what to use to combine each argument! I've been trying to figure this out for hours...

Last edited by Yogesh Sawant; 04-09-2009 at 07:27 AM.. Reason: added code tags
# 2  
Old 04-09-2009
Use the man pages, Luke.

char *strncat(char *restrict s1, const char *restrict s2, size_t n);

strcat(), strncat(), strlcat()
The strcat() function appends a copy of string s2, including
the terminating null character, to the end of string s1. The
strncat() function appends at most n characters. Each
returns a pointer to the null-terminated result. The initial
character of s2 overrides the null character at the end of s1.

You have your s1 and s2 interchanged, and the way it is written now,
you'll need to print argv[1] to get the final result.

As written, it also leaks memory, but that might not be an issue for you at this point.
[as each is copied, it allocates a new one and abandons the previous copy]
# 3  
Old 04-09-2009
Thank you, that clears it up. Everything works now...

Code:
int main(int argc, char* argv[])
{
    int i = 1;

    for(i = 1; i < argc; i++)
    {
        if(i > 1)
        {
            argv[i] = strncat(argv[i - 1],argv[i],1000);
        }
        printf("%s\n\n", argv[i]);
    }
    printf("Printing string: %s\n", argv[i - 1]);
    printf("The sentence entered is %u characters long.\n",strlen(argv[i - 1]));
    return 0;
}

# 4  
Old 04-10-2009
delete this

Last edited by hansel13; 04-10-2009 at 09:31 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combining multiple block of lines in one comma separated line

Hi Everyone, On my Linux box I have a text file having block of few lines and this block lines separated by one blank line. I would like to format and print these lines in such a way that this entire block of lines will come as single comma separated line & again next block of lines in next... (7 Replies)
Discussion started by: gr8_usk
7 Replies

2. Shell Programming and Scripting

Processing Multiple Arguments in Command Line Options

Hi All, I am new to scripting. Could you please assist me . Here is my requirement. I have written a script that has 2 option flags defined. -l) calls some function with the arguments passed in front of -l -r) calls second function with the arguments passed in front of -r *) calls the... (7 Replies)
Discussion started by: Jay Deshpande
7 Replies

3. Shell Programming and Scripting

Command line arguments with multiple values

how can I pass multiple values from command line arguments example script.sh -arg1 op1 -arg2 op1 op2 op3 (2 Replies)
Discussion started by: nsk
2 Replies

4. Programming

PERL:Combining multiple lines to single line

Hi All I need a small help for the below format in making a small script in Perl or Shell. I have a file in which a single line entries are broken into three line entries. Eg: I have a pen and notebook. All i want is to capture in a single line in a separate file. eg: I have a pen and... (4 Replies)
Discussion started by: Kalaiela
4 Replies

5. Shell Programming and Scripting

command line arguments

hi,,,, I want to create a command prompt, for example "prompt>", so my prompt need to handle commands, for example "prompt>cmd", so i want to know how to get arguments for my own commands cmd, i.e. default argc should contain arguments count and argv should point to the argument vector i.e, for... (2 Replies)
Discussion started by: vins_89
2 Replies

6. UNIX for Dummies Questions & Answers

command line arguments

hi, can someone how to accept command line arguments as a variable using in script? like: ./scriptname arguments by accept arguments, I can use it in my script? thx! (1 Reply)
Discussion started by: ikeQ
1 Replies

7. UNIX for Dummies Questions & Answers

Command line arguments.

I am working on a script wherein i need the user to enter the Build ID for eg:the command line will show enter the build ID Now on entering the build ID it should be assigned to @ARGV. How can this be done.? (1 Reply)
Discussion started by: Varghese
1 Replies

8. Shell Programming and Scripting

command line arguments

-------------------------------------------------------------------------------- I have this while loop and at the end I am trying to get it to tell me the last argument I entered. And with it like this all I get is the sentence with no value for $1. Now I tried moving done after the sentence... (1 Reply)
Discussion started by: skooly5
1 Replies

9. UNIX for Dummies Questions & Answers

arguments in command line

Hi all, How many arguments can we pass while testing a prgm at command line.. I encountered an issue while passing 10 arguments. For $10 its taking argument passed for $1 followed by 'zero'. can we pass more than 9 arguments /Is there any other way. Thanks, rrs (6 Replies)
Discussion started by: rrs
6 Replies

10. Programming

command line arguments

Hi How to pass multi line text as a command line argument to a program. (i.e) ./a.out hi this is sample 0 file1 where hi this is sample should be stored in argv 0 in argv and so on... (3 Replies)
Discussion started by: bankpro
3 Replies
Login or Register to Ask a Question