[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# SSCANF

Posted on 2011-04-26
Medium Priority
315 Views
What does this piece of code do?
It is a condition in a For Loop.
What does ==1 mean and inversely what would a 0 mean?

"sscanf(route_parameter+net_offset, "%s %n", &wpt_name[x][0], &offset) == 1"
0
Question by:dpalyca755

LVL 53

Accepted Solution

Infinity08 earned 2000 total points
ID: 35469557
The sscanf function parses the given string (first argument) according to the given format string (second argument), and stores the extracted values at the given memory addresses (other arguments).
It returns the number of succesfully read tokens.

So, if it returns 1, it means that the first token (the %s token in this case) was read succesfully.
If it returns 0, it means that none of the tokens were read succesfully.

For more information on how sscanf works, I refer you to this reference page :

http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/
0

LVL 17

Expert Comment

ID: 35472144
It has a string route_parameter that contains a series of "string number" combinations.

net_offset is obviously a numeric value that gets updated within the loop to point to the next combination.

After each successful loop (ie. the result of sscanf == 1) the wpt_name array entry at position x in the array contains the string value.
The offset variable will contain the number value from the combination.  What it is will be relative to the application.

0

LVL 8

Expert Comment

ID: 35474552
> sscanf(route_parameter+net_offset, "%s %n", &wpt_name[x][0], &offset) == 1
%s will read the string (that is stored at route_parameter+net_offset) upto first space.
And this will be stored in wpt_name[x][0].
%n will keep the count of number of characters eaten by %s and that will get stored in offset.

So, if the string stroed in route_parameter is "string 1234",
%s will read 's', 't', 'r', 'i', 'n', 'g' and after this there is a space. So, including space, the number of characters read is 7. So, the offset will get the value 7.

> What does ==1 mean and inversely what would a 0 mean?
And sscanf() returns the number of input items successfully matched.
Now, in the sscanf() that you have, there is only one %s and that is the only one to be matched,
If the string at route_parameter+net_offset is a null string, then %s will not read anything and sscanf will return 0. If there is a string, then it will return 1.
0

LVL 40

Expert Comment

ID: 37419092
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
###### Suggested Courses
Course of the Month18 days, 12 hours left to enroll