Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

argv and sscanf problem

Status
Not open for further replies.

fortytwo

Technical User
Apr 18, 2000
206
GB
Hi,

I have a bit of a problem passing command line stuff to my program in Linux. Here is the program:

[tt]
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {
char string[100];

//here
sscanf(string, &quot;%s&quot;, argv[1]);
printf(&quot;Plaintext: %s\n&quot;, string);

printf(&quot;Plaintext: %s\n&quot;, argv[1]);
return 0;
}
[/tt]

If I comment out the two lines after the //here comment, the program runs fine, and prints out the second element of the arguments. If I uncomment the two lines after the //here comment I het junk printed out for *both* of the printf statements! Anyone any ideas why this might be?

Thanks
Will.

will@hellacool.co.uk
 
You're using sscanf wrong, your statement should be:

Code:
sscanf(argv[1],&quot;%s&quot;,string);

Good luck.
 
You should say :
Code:
sscanf(argv[1], &quot;%s&quot;, string);
or 
sprintf(string,&quot;%s&quot;,argv[1]);
-obislavu-
 
I think this is what you want for the next two lines after the //here:

printf(&quot;Enter something: &quot;);
scanf( &quot;%s&quot;, &string);
 
Thanks everyone, I had the arguments to sscanf wrong, this is correct:
[tt]
sscanf(argv[1],&quot;%s&quot;,string);
[/tt]
I was pulling my hair out ;) Not homework though, writing a simple program to help decrypt ceasar ciphers just for fun.

Will.

will@hellacool.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top