sscanf() weired behaviour


 
Thread Tools Search this Thread
Top Forums Programming sscanf() weired behaviour
# 1  
Old 02-14-2012
sscanf() weired behaviour

Hi with the following code

int a, b;
while ((n = readline (connfd, buf, sizeof(buf)-1)) > 0)
{

buf[n] = '\0';

if (sscanf(buf,"%d %d",&a,&b) != 2)
snprintf (buf, sizeof(buf), "data error\r\n");

else
{
printf("\nRecvd %d and %d",a,b);
snprintf (buf, sizeof(buf), "%ld\r\n", a+b);}

when i check the values "a" gets "0" whereas b gets the correct value. i tried this code outside it works fine out side in independent prog... I am using cygwin.
dataformat in buf = intSPACEint\r\n

---------- Post updated at 02:46 PM ---------- Previous update was at 02:39 PM ----------

ooh its working fine now with the above code.. i was using unsigned short a,b; instead of int ... and %u to get unsigned values....
However it is strnage it doenot use unsigned short ...
thanx for having a look
# 2  
Old 02-15-2012
It expects integer size, and just giving it shorts doesn't warn it that you're giving it shorts. With &, you're giving it an address remember, so it can't know the size. So it crams 4 bytes of data into an address where you've got a 2-byte variable, overwriting 2 bytes of random data after it -- possibly your other variable there, producing the zero.

There are size qualifiers to the expressions if you want to give it anything other than an integer, see man 3 printf.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Format specifier for sscanf() in C

Hello, I have formatted lines delimited by colon ":", and I need to parse the line into two parts with sscanf() with format specifiers. infile.txt: Sample Name: sample1 SNPs : 91 MNPs : 1 Insertions : 5 Deletions ... (13 Replies)
Discussion started by: yifangt
13 Replies

2. Programming

different behaviour in fg and bg

fg = foreground bg = background I have a cobol program that I start with a very simple script. The script is not at fault as it has not changed and the program worked in fg and bg before. I have altered the logging in the program and moved my cursor declare to working storage. The program runs... (6 Replies)
Discussion started by: Bruble
6 Replies

3. Programming

using sscanf

How can I separetely extract the string and int after "dribble" ? (sscanf must limit TEXT to 9 chars to avoid buffer overflows.) How come this code does not work with "dribbletext08" but does with "dribbletext05" ? int main(void) { char TEXT = ""; int NUMBER = 0; ... (2 Replies)
Discussion started by: cyler
2 Replies

4. Programming

Help with sscanf

sscanf does not stop at the first "&". How can I extract "doe" ? char A = "name=john&last=doe&job=vacant&"; char B = "last"; char C = ""; char *POINTER = strstr(A, B); sscanf(POINTER + strlen(B), "=%s%*", C); printf("%s\n", C); // doe&job=vacant& (2 Replies)
Discussion started by: limmer
2 Replies

5. Programming

help with sscanf

I need to match a float inside a very long string (about 5000 chars) with sscanf. (I trimmed the string in this example.) I can't seem to match all the chars that come before and after the float. int main(void) { char A = ""; strcat(A, " hello world! WORD' name='5.3498' hello world! ... (1 Reply)
Discussion started by: limmer
1 Replies

6. Programming

help with sscanf()!

Hi everybody, i need help with this function, i'm programming in CGI with C and i can't make this work. QUERY_STRING is something like: user=MYUSER&pass=MYPASS So, what i want is to store the strings containing the username and the password into str1 and str2 respetively, here's the... (4 Replies)
Discussion started by: Zykl0n-B
4 Replies

7. Shell Programming and Scripting

cp -R behaviour

i 've noticed the following difference between freebsd cp and gnu cp from the freebsd cp man page: -R ... If the source_file ends in a /, the contents of the directory are copied rather than the directory itself. ... on gnu cp from the man pagewhile on gnu cp manpage: ‘-r'... (2 Replies)
Discussion started by: aegis
2 Replies

8. Programming

sscanf !!

I have a string Form this string, I want to extract I am unable to do that with sscanf because of the space between the words. What else can I use? #include <stdio.h> char buf_2; int main() { char *buf_1 = "\\\\?\\whats going on"; sscanf(buf_1,... (4 Replies)
Discussion started by: the_learner
4 Replies

9. Shell Programming and Scripting

Little bit weired : Find files in UNIX w/o using find or where command

Yes , I have to find a file in unix without using any find or where commands.Any pointers for the same would be very helpful as i am beginner in shell scritping and need a solution for the same. Thanks in advance. Regards Jatin Jain (10 Replies)
Discussion started by: jatin.jain
10 Replies

10. Programming

sscanf function is failing

Please delete this thread. (0 Replies)
Discussion started by: jxh461
0 Replies
Login or Register to Ask a Question