The UNIX and Linux Forums  


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 args 2 skooly5 UNIX for Dummies Questions & Answers 2 04-06-2008 10:36 PM
command line args skooly5 UNIX for Dummies Questions & Answers 2 04-06-2008 08:46 PM
alias with args how to ... umen UNIX for Dummies Questions & Answers 4 12-28-2005 07:47 AM
required command line args yoi2hot4ya UNIX for Dummies Questions & Answers 6 06-02-2005 07:50 PM
Args to Array Shaz Shell Programming and Scripting 2 06-30-2003 05:11 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 05-28-2007
enuenu enuenu is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 39
Command line args

My program usage takes the form for example;
$ theApp 2 "one or more words"

i.e. 3 command line arguments; application name, an integer, some text

My code includes the following 4 lines:

int anInteger;
char words[80];

sscanf(argv[1], "%d", &anInteger);
sscanf(argv[2], "%s", &message);


Based on th example execution shown above this code results in th following assignments:

anInterger = 2 /* correct and as I thought*/
message = one /* not as I thought or intended*/

I thought when I printed out the value of message I would get:
one or more words

but instead I get:
one

I have done a test and the use of the " " marks seems to make "one or more words" be seen as a single argument. If I don't use the " " marks it sees every word as another argument.

So even though the program sees "one or more words" as a single argument, sscanf is not reading it properly. How can I overcome this problem?
  #2 (permalink)  
Old 05-28-2007
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
That is how the scanf family of functions works. Try another test:

Code:
#include<stdio.h>

int main(int argc, char *argv[]){
        char message[80];

        fscanf(stdin,"%s",message);
        fprintf(stdout,"%s\n",message);
}

If you run this, this is what you get:
Code:
# ./a.out
this is a test
this

This is how it is implemented. Anyway, if you want to copy the second argument (argv[2]) into the message string, just use strcpy, like this:

Code:
#include<stdio.h>
#include<string.h>

int main(int argc, char *argv[]){
        int anInteger;
        char message[80];

        sscanf(argv[1], "%d", &anInteger);
        strcpy(message,argv[2]);
        fprintf(stdout,"anInteger: %d\nmessage: %s\n",anInteger,message);
}

This works as you want it to:

Code:
# ./a.out 2 "this is a test"
anInteger: 2
message: this is a test

  #3 (permalink)  
Old 05-29-2007
enuenu enuenu is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 39
Thanks very much, problem solved.
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 09:48 PM.


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