The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Command line arguments. Varghese UNIX for Dummies Questions & Answers 1 02-17-2009 06:33 AM
command line arguments skooly5 Shell Programming and Scripting 1 04-07-2008 04:49 AM
arguments in command line rrs UNIX for Dummies Questions & Answers 6 07-28-2006 07:44 AM
command line arguments bankpro High Level Programming 3 02-02-2006 11:12 AM
Command Line Arguments roba909 UNIX for Dummies Questions & Answers 7 01-19-2006 01:46 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-09-2009
hansel13 hansel13 is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 11
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 (permalink)  
Old 04-09-2009
nobody4 nobody4 is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 18
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 (permalink)  
Old 04-09-2009
hansel13 hansel13 is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 11
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 (permalink)  
Old 04-10-2009
hansel13 hansel13 is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 11
delete this

Last edited by hansel13; 04-10-2009 at 09:31 PM..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:39 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0