473,544 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sscanf

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
Allan
Nov 14 '05 #1
11 3170

"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote in message
news:c5******** **@news.freedom 2surf.net...
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
Allan


also, is there a way to set ':' as a terminator for the string? I jsut
realised that my lGuid will continue until the next whitespace or \0
Thanks
Allan
Nov 14 '05 #2
"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote:

"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote:
If I have
sscanf("FL:%s:% d:%s\n", lGuid, &lID, lFileName); ^
You forgot to specify the string to parse
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 ?


also, is there a way to set ':' as a terminator for the string? I jsut
realised that my lGuid will continue until the next whitespace or \0


Warning, untested:

sscanf( yourstring, "FL:%[^:]:%d:%[^\n]\n", lGuid, &lID, lFileName);

If the ':'s might be surrounded by whitespace that should be ignored,
you'll have to match and discard that, too.

HTH
Regards
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #3
>
Warning, untested:

sscanf( yourstring, "FL:%[^:]:%d:%[^\n]\n", lGuid, &lID, lFileName);

If the ':'s might be surrounded by whitespace that should be ignored,
you'll have to match and discard that, too.

HTH
Regards


I tried:
sscanf(xiBuffer , "FL:%d:%[^:]s:%d:%[^\n]s\n", &lID, lGUID, &lFileLength ,
lFileName);

the lGUID gets read fine, but nothing after this gets read. Where can I
find information about these scan types?
Thanks
Allan
Nov 14 '05 #4
"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote:

Warning, untested:

sscanf( yourstring, "FL:%[^:]:%d:%[^\n]\n", lGuid, &lID, lFileName);

If the ':'s might be surrounded by whitespace that should be ignored,
you'll have to match and discard that, too.

HTH
Regards
I tried:
sscanf(xiBuffe r, "FL:%d:%[^:]s:%d:%[^\n]s\n", [....]

^ ^
You've got two spurious s characters at the indicated positions.
the lGUID gets read fine, but nothing after this gets read. Where can I
find information about these scan types?


Hm, RTFM? ;-)
Seriously, your implementation should provide a documentation of the
standard library functions. Alternatively, buy the C standard, or,
if you're short on money, retrieve a copy of the final public draft
for free at: http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/ .
As a starter, here's an excerpt from the *scanf function description:

ISO/IEC 9899:1999
7.19.6.2 The fscanf function
[...]

[ Matches a nonempty sequence of characters from a set of expected
characters (the scanset). If no l length modifier is present, the
corresponding argument shall be a pointer to the initial element of
a character array large enough to accept the sequence and a
terminating null character, which will be added automatically.
[...] The conversion specifier includes all subsequent characters
in the format string, up to and including the matching right
bracket (]). The characters between the brackets (the scanlist)
compose the scanset, unless the character after the left bracket is
a circumflex (^), in which case the scanset contains all characters
that do not appear in the scanlist between the circumflex and the
right bracket. [...]

HTH
Regards
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #5
I tried:
sscanf(xiBuffe r, "FL:%d:%[^:]s:%d:%[^\n]s\n", [....]

^ ^
You've got two spurious s characters at the indicated positions.
the lGUID gets read fine, but nothing after this gets read. Where can I
find information about these scan types?


Hm, RTFM? ;-)
Seriously, your implementation should provide a documentation of the
standard library functions. Alternatively, buy the C standard, or,
if you're short on money, retrieve a copy of the final public draft
for free at: http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/ .


Thanks, I have it working now.
Allan
Nov 14 '05 #6
In <c5**********@n ews.freedom2sur f.net> "Allan Bruce" <al*****@TAKEAW AYf2s.com> writes:
If I have
sscanf("FL:%s: %d:%s\n", lGuid, &lID, lFileName);
You're invoking undefined behaviour, because the input string is missing
from your sscanf call.
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?
Yup.
even though I tell it the end of string as at the \n ?


Nope, you're not telling it anything like that. You're merely telling
it to eat *any* whitespace it finds after the string (which is a noop in
your case, since you're using sscanf and not [f]scanf). There is NO way
to tell %s that the first white space encountered is not ending the input
string. You have to use a more sophisticated conversion descriptor for
this purpose:

#define INPUT "FL:1234ABCD:3: FileName With Spaces.txt\n"

int rc = sscanf(INPUT, "FL:%s:%d:%[^\n]", lGuid, &lID, lFileName);

Things to remember:

1. If lFileName is not large enough to hold the whole first argument of
the sscanf call, use an explicit maximum input field specification; no
need to rediscover the joys of gets. This also applies to %s.

2. Unlike most conversion specifications, including %s, %[ doesn't skip
any preceding white space (just like %c). If needed, use an explicit
white space in the format string, for this purpose (it is harmless if
there is no white space at all at that point in the input string).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7
In <c5***********@ sunnews.cern.ch > I wrote:
In <c5**********@n ews.freedom2sur f.net> "Allan Bruce" <al*****@TAKEAW AYf2s.com> writes:
If I have
sscanf("FL:%s :%d:%s\n", lGuid, &lID, lFileName);


You're invoking undefined behaviour, because the input string is missing
from your sscanf call.
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?


Yup.
even though I tell it the end of string as at the \n ?


Nope, you're not telling it anything like that. You're merely telling
it to eat *any* whitespace it finds after the string (which is a noop in
your case, since you're using sscanf and not [f]scanf). There is NO way
to tell %s that the first white space encountered is not ending the input
string. You have to use a more sophisticated conversion descriptor for
this purpose:

#define INPUT "FL:1234ABCD:3: FileName With Spaces.txt\n"

int rc = sscanf(INPUT, "FL:%s:%d:%[^\n]", lGuid, &lID, lFileName);


Which is plagued by the problem already mentioned downthread by Allan:
%s will eat too much, stopping after FileName. So, %s is not the right
thing for lGuid, either:

int rc = sscanf(INPUT, "FL:%[^:]:%d:%[^\n]", lGuid, &lID, lFileName);

NEVER omit to check rc, to see if all the fields have been properly
converted.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #8
If I have
sscanf("FL:%s:% d:%s\n", lGuid, &lID, lFileName);

^

That's a C++ reference operator isn't it? I don't think that has
anything to do with a pointer. It is a COM Interface ID isn't it?

Bill
Nov 14 '05 #9
> the lGUID gets read fine, but nothing after this gets read. Where can I
find information about these scan types?
Thanks
Allan

sscanf reads a string instead of input from a keyboard.
http://www-ccs.ucsd.edu/c/

Bill
Nov 14 '05 #10

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
4226
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
5421
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
2018
by: baumann | last post by:
hi all there has 2 program 1) the first test program code #include <stdio.h> int main(void) {
5
6395
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
2937
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
2376
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
21402
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
3487
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
11613
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
7420
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...
0
7362
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...
0
7602
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. ...
0
7764
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5904
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...
1
5291
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...
0
4911
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...
0
657
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.