473,779 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to use sscanf?

is it similar to scanf?
when i use scanf it can read the words in the screen automatically one
after another.i use a char array to store the string,then use sscanf to
read the words,but it just only reat out the first word in the string
array every time. so if i want to read the words in the string one by
one, just like scanf, what should i do?

thanks!
Nov 30 '05 #1
7 27016
nick wrote:
is it similar to scanf?
when i use scanf it can read the words in the screen automatically one
after another.i use a char array to store the string,then use sscanf to
read the words,but it just only reat out the first word in the string
array every time. so if i want to read the words in the string one by
one, just like scanf, what should i do?


If all you're going to read is words (strings), you can increment the
buffer pointer passed to sscanf by the length of the string previously
read.

Assuming your complete string is pointed to by char *str and you want
to read each word from it into another already allocated character
array char *word. Then...

/*Read first word*/
sscanf(str,"%s" , word);
/*Read next word*/
sscanf(str+strl en(word), "%s", word);

You can repeat this to read consecutive strings.

sscanf is typically used though when you know exactly the number and
type of fields you want to extract from the parent string. You will
then use sscanf just once and get all the fields in one shot instead of
having to manipulate the buffer pointer for each field you read.

Nov 30 '05 #2
More basically,

The scanf() prototype is:
/*
---------------------------------------------------------------------------------------------
*/
int scanf ( const char * format [ , argument , ...] );

The sscanf() prototype is:
/*
---------------------------------------------------------------------------------------------
*/
int sscanf ( char * buffer, const char * format [ , argument , ...] );
I guess you must be familiar with the function scanf(), which reads
data from the standard input(stdin) and stores it to the locations
specified by the argument(s) passed to it(scanf()).
sscanf() reads data from the specified -buffer- and stores it into the
locations by the argument(s).

Just as what Saif said:
sscanf is typically used though when you know exactly the number and
type of fields you want to extract from the parent string. You will
then use sscanf just once and get all the fields in one shot instead of
having to manipulate the buffer pointer for each field you read.


/* BEGING */
#include <stdio.h>

int main (void)
{
char specifiedBuffer[] = "C is a good programming language!";
char str1[10], str2[10];
int i;

sscanf (specifiedBuffe r, "%s %*s %*s %s", str1, str2);
printf ("%s => %s\n", str1, str2);

return 0;
}

/* END */

Output:
C => good

/*
Becker
*/

Nov 30 '05 #3
More basically,

The scanf() prototype is:
/* ------------------------------------------------*------------------
*/
int scanf ( const char * format [ , argument , ...] );

The sscanf() prototype is:
/* ------------------------------------------------*------------------
*/
int sscanf ( char * buffer, const char * format [ , argument , ...] );

I guess you must be familiar with the function scanf(), which reads
data from the standard input(stdin) and stores it to the locations
specified by the argument(s) passed to it(scanf()).
sscanf() reads data from the specified -buffer- and stores it into the
locations by the argument(s).

Just as what Saif said:
sscanf is typically used though when you know exactly the number and
type of fields you want to extract from the parent string. You will
then use sscanf just once and get all the fields in one shot instead of
having to manipulate the buffer pointer for each field you read.

/* BEGING */
#include <stdio.h>

int main (void)
{
char specifiedBuffer[] = "C is a good programming language!";
char str1[10], str2[10];

sscanf (specifiedBuffe r, "%s %*s %*s %s", str1, str2);
printf ("%s => %s\n", str1, str2);

return 0;
}

/* END */

Output:
C => good
/*
Becker
*/

Nov 30 '05 #4
Becker wrote:
More basically,

The scanf() prototype is:
/* ------------------------------------------------*------------------
*/
int scanf ( const char * format [ , argument , ...] );
I have:
7.19.6.4 The scanf function
Synopsis
1 #include <stdio.h>
int scanf(const char * restrict format, ...);
The sscanf() prototype is:
/* ------------------------------------------------*------------------
*/
int sscanf ( char * buffer, const char * format [ , argument , ...] );
I have:
7.19.6.7 The sscanf function
Synopsis
1 #include <stdio.h>
int sscanf(const char * restrict s,
const char * restrict format, ...);
/* BEGING */
:)
#include <stdio.h>

int main (void)
{
char specifiedBuffer[] = "C is a good programming language!"; And what happens when there's a typo like:
char unspecifiedBuff er[] = "pooooorprogram ming language!"; char str1[10], str2[10];

sscanf (specifiedBuffe r, "%s %*s %*s %s", str1, str2);

sscanf (unspecifiedBuf fer, "%s %*s %*s %s", str1, str2);

Ought to check return values of *scanf() and friends...

[snip]

Nov 30 '05 #5
thanks yours reply, but i have another question, the question is, how to
check where is the end of the string?

thanks!
Nov 30 '05 #6
On 2005-11-30, nick <i1********@yah oo.com> wrote:
thanks yours reply, but i have another question, the question is, how to
check where is the end of the string?


strlen(str) returns an index, strchr(str,0) a pointer; pick your poison.
Nov 30 '05 #7
On 30 Nov 2005 03:03:18 -0800, "Saif" <sa****@gmail.c om> wrote:
nick wrote:
<snip> so if i want to read the words in the string one by
one, just like scanf, what should i do?


If all you're going to read is words (strings), you can increment the
buffer pointer passed to sscanf by the length of the string previously
read.

Assuming your complete string is pointed to by char *str and you want
to read each word from it into another already allocated character
array char *word. Then...

/*Read first word*/
sscanf(str,"%s" , word);
/*Read next word*/
sscanf(str+strl en(word), "%s", word);

You can repeat this to read consecutive strings.

%s _skips whitespace_ and then reads a string of non-whitespace, i.e.
a "word". Or fails due to hitting the end of the input, which you
should catch by checking the return value, as you should for all
*scanf variants. strlen(word) doesn't allow for the whitespace. If
there is leading whitespace in the input string, this will (first)
fail for second word; otherwise for the third.

Also, %s (and %[) in *scanf should always be given with a length
limit, to prevent either accidentally or maliciously exceeding the
actual object (buffer) size and causing Undefined Behavior, which in
the latter (malicious) case is likely to be destroying your data
and/or stealing your money. Unless you are absolutely 100% sure the
input is valid, which in practice is only if was generated by a valid
sprintf call or similar in the line immediately preceding the sscanf
call, in which case you already have the data and don't need to scan.

Try:
char * ptr = str; /* if not already a pointer you can spare */
int used;
if( sscanf (ptr, "%Ns%n", word, &used) < 1 ) /* error */
ptr += used;
if( sscan (ptr, ...

- David.Thompson1 at worldnet.att.ne t
Dec 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
473
by: Allan Bruce | last post by:
If I have sscanf("FL:%s:%d:%s\n", lGuid, &lID, lFileName); and the last string contains spaces, e.g. my complete string "FL:1234ABCD:3:FileName With Spaces.txt\n" does sscanf just make lFileName the string up to the whitespace? even though I tell it the end of string as at the \n ? Thanks
4
4259
by: smshahriar | last post by:
Hi, I want to scan from the following string all the hex numbers and populate an array of integers: 0x27 0x00 0x30 0x00 0x33 0x00 0x36 0x00
10
5473
by: baumann | last post by:
hi, 1) first test program code #include <stdio.h> int main(void) { char * file = "aaa 23 32 m 2.23 ammasd"; int i2,i3;
4
2030
by: baumann | last post by:
hi all there has 2 program 1) the first test program code #include <stdio.h> int main(void) {
5
6405
by: jchludzinski | last post by:
I'm using strtok() to parse thru a line and read different numbers: float value; char *token; token = strtok( line, " " ); .... sscanf( token, "%f", &value ); These results are less precise than I had expected:
22
2964
by: Superfox il Volpone | last post by:
Hello I have some problem with sscanf, I tryed this code but it doesn't works : char* stringa = "18/2005" char mese; char anno; int i_letture; i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno);
8
2389
by: Artemio | last post by:
Dear folks, I need some help with using the sscanf() function. I need to parse a string which has several parameters given in a "A=... B=... C=..." way, and each has a different type (one is a text string, another is a decimal, next one is float, etc.). I have GCC 4.0.1 on Mac OS X Tiger. Here is an example of what I am trying to do.
20
21444
by: AMP | last post by:
Hello, Anybody know if anything exists like sscanf in c. I found a few things OL but most were pretty old. Maybe something has come along since 2004? Thanks Mike
5
3506
by: Alex Mathieu | last post by:
Hi, using sscanf, I'm trying to retrieve something, but nothing seems to work. Here's the pattern: SS%*sþ0þ%6s Heres the data: SS000000395000000000DC-þ0þ799829þ1174503725þ Actually, I would like to retrieve the "799829" from the data, but it always failed. I thought that the "%*sþ0þ" would work as if I was
7
11630
by: gio | last post by:
suppose I have: .... char str1; char str2; int ret; fgets(str1, LEN, stdin); //str1 can contain just '\n' and '\0' ret=sscanf(str1, "%s", str2); ....
0
9633
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10305
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9928
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8959
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7483
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.