![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Combining multiple command line arguments
suppose the user enters:
Code:
./Myfile blah1 blah2 blah3 Code:
blah1blah2blah3 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 |
|
||||
|
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] |
|
||||
|
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;
}
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|