473,326 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

sscanf problem

Consider this program:
// ScanTest.c
#include <stdio.h>
int main(int argc, char* argv[])
{
int x1, x2, x3;
char s[16] = "38 02 14";
char t[16] = "38 08 14";
sscanf( t, "%i %i %i", &x1, &x2, &x3 );
printf( "x1 = %i, x2 = %i, x3 = %i\n", x1, x2, x3 );

sscanf( s, "%i %i %i", &x1, &x2, &x3 );
printf( "x1 = %i, x2 = %i, x3 = %i\n", x1, x2, x3 );
return 0;
}

The output using lcc-win32 sscanf is:
x1 = 38, x2 = 8, x3 = 14
x1 = 38, x2 = 2, x3 = 14

The output with gcc (under linux) or with Microsoft VC6++ is:
x1 = 38, x2 = 0, x3 = 8
x1 = 38, x2 = 2, x3 = 14

I can see why this should be. Can anyone here explain?

Thanks for your time

P.S. I have verified that there is no uppercase "O" that replaces
a zero. The "08" is well zero and then eight.

Why does it stop???


Nov 14 '05 #1
6 4631
Forget it. I found the problem...

The string 08 should be interpreted as an octal number since it begins with
zero

Nov 14 '05 #2
On Fri, 9 Jul 2004, jacob navia wrote:
The output using lcc-win32 sscanf is:
x1 = 38, x2 = 8, x3 = 14
x1 = 38, x2 = 2, x3 = 14

The output with gcc (under linux) or with Microsoft VC6++ is:
x1 = 38, x2 = 0, x3 = 8
x1 = 38, x2 = 2, x3 = 14


My Linux man page for sscanf says that %i "matches an optionally signed
integer; the next pointer must be a pointer to int. The integer is read
in base 16 if it begins with `0x' or `0X', in base 8 if it begins with
`0', and in base 10 otherwise. Only characters that correspond to the
base are used."

02 = octal 2 = 2
08 = octal 8??? = makes no sense
--
Michael Hoffman
Nov 14 '05 #3
In 'comp.lang.c', "jacob navia" <ja***@jacob.remcomp.fr> wrote:
Consider this program:
// ScanTest.c
#include <stdio.h>
int main(int argc, char* argv[])
{
int x1, x2, x3;
char s[16] = "38 02 14";
char t[16] = "38 08 14";
sscanf( t, "%i %i %i", &x1, &x2, &x3 );


"08" is an invalid octal representation. You should use "%d" and instead of
"%i" if it the string is supposed to be a decimal representation of the value
8.

Keep in mind that %i with scanf() acts like strtol() with base 0. It tries to
perform an automatic conversion assumed on the value of the first characters:

0x[0-9,a-f,A-F[*]] -> hexacecimal
0[0-7[*]] -> octal
[+-]1-9[0-9[*]] -> decimal

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #4
In <cc**********@news-reader4.wanadoo.fr> "jacob navia" <ja***@jacob.remcomp.fr> writes:
Forget it. I found the problem...

The string 08 should be interpreted as an octal number since it begins with
zero


Which means that the conversion stops when the 8 is encountered.

I've never understood the rationale for having %i. It's far more
confusing than useful and it was not mentioned in K&R1. Did it already
exist in the pre-ANSI days or is it a committee invention?

Anyway, if you don't use it, it's as good as if it didn't exist. Unless
you're an implementor: you still have to get it right, of course.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5
jacob navia wrote:
Consider this program:
// ScanTest.c
#include <stdio.h>
int main(int argc, char* argv[])
{
int x1, x2, x3;
char s[16] = "38 02 14";
char t[16] = "38 08 14";
sscanf( t, "%i %i %i", &x1, &x2, &x3 );
printf( "x1 = %i, x2 = %i, x3 = %i\n", x1, x2, x3 );

sscanf( s, "%i %i %i", &x1, &x2, &x3 );
printf( "x1 = %i, x2 = %i, x3 = %i\n", x1, x2, x3 );
return 0;
}

The output using lcc-win32 sscanf is:
x1 = 38, x2 = 8, x3 = 14
x1 = 38, x2 = 2, x3 = 14

The output with gcc (under linux) or with Microsoft VC6++ is:
x1 = 38, x2 = 0, x3 = 8
x1 = 38, x2 = 2, x3 = 14

I can see why this should be. Can anyone here explain?


Because lcc's library function scanf is broken. '%i' specifies that the
argument be interpreted as by strtol with a value of 0 for the base.
'08' is interpreted as octal '0' followed by decimal '8'.
Nov 14 '05 #6
Because lcc's library function scanf is broken.


WAS broken.

It is fixed now. What a pain this subtle differences between %i and %d

thanks for your time
Nov 14 '05 #7

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

Similar topics

4
by: baumann | last post by:
hi all there has 2 program 1) the first test program code #include <stdio.h> int main(void) {
5
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...
22
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",...
10
by: broeisi | last post by:
What advantages does sscanf offer over scanf? I had the following code: #include <stdio.h> #include <string.h> int main(void) { int start, finish, values;
4
by: lynx_ace | last post by:
Hi everyone. I need a little bit help here...I have an assignment and it is working fine until the last part which I can't solve. So here's the code in simple form #define maxlength 200 ...
9
by: GrispernMix | last post by:
bool variant_t::Convert( fieldtype_t newType ) { if ( newType == fieldType ) { return true; } // // Converting to a null value is easy. //
8
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...
5
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...
5
by: a | last post by:
After reading FAQ comp.lang.c section 12 and googling again, still there is no threads talking about reading a series of numbers. The input files, somehow structured, is exemplified below: ...
4
by: utab | last post by:
Dear all, I have to interface some C code in C++, but I had a problem with sscanf function, it has been some time I have not used C and I could not figure out my problem. Simple code is below, I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.