Hello
I have some problem with sscanf, I tryed this code but it doesn't works
:
char* stringa = "18/2005"
char mese[3]; char anno[5];
int i_letture;
i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno);
mese[2] = anno[4] = '\0';
The values ar random and I don't understand the motive
I tryed either this variant :
char* porka_vakka = strchr(stringa, '/');
*porka_vakka = '\0'; // but either ' ' e '\n'
i_letture = sscanf(stringa, "%s", &mese);
But neither this works...
Could anyone help me ?
Bye
- Atari
p.s. happy new year :) 22 2962
Superfox il Volpone wrote: Hello
I have some problem with sscanf, I tryed this code but it doesn't works :
char* stringa = "18/2005" char mese[3]; char anno[5]; int i_letture;
i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno); mese[2] = anno[4] = '\0';
You have mese[3] and anno[5]. When you read with sscanf you tell it to
store the character arrays in &mese and &anno. mese=&mese[0] and
anno=&anno[0].
It should be: i_letture=sscan f(stringa,"%2s/%4s",mese,anno) , otherwise
you are trying to write those values at locations that hold the
addresses of mese and anno instead of writing them to the addresses
where mese and anno point.
If you really want to use & then write:
i_letture=sscan f(stringa,"%2s/%4s",&mese[0],&anno[0]); The values ar random and I don't understand the motive I tryed either this variant :
char* porka_vakka = strchr(stringa, '/'); *porka_vakka = '\0'; // but either ' ' e '\n' i_letture = sscanf(stringa, "%s", &mese);
Same problem as above. Reading into the wrong location.
"Superfox il Volpone" <at***@email.it > wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. . Hello
I have some problem with sscanf, I tryed this code but it doesn't works :
char* stringa = "18/2005"
"18/2005" is a string literal. Any attempts to modify
any of its characters produces undefined behavior.
char mese[3]; char anno[5]; int i_letture;
i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno);
This gives undefined behavior. You're trying to modify
a string literal.
Also, since 'mese' and 'anno' are arrays, their names
in this context will evalutate to pointers to their
first element. So e.g. use 'mese', not '&mese'.
%s must match with type 'char*'. The expression &mese
does not have that type. Its type is (*)[3] (pointer to
array of three char). Not the correct type.
mese[2] = anno[4] = '\0';
'sscanf()' already applies the string terminator for you
(in the proper location)
Also (disregarding for now the string literal problem)
note that if the size of data stored by 'sscanf()' is
less than the size of the array, your arbitrary placement of
'\0' as the last array element will not terminate the string
properly (there will be 'garbage' between the data and the
terminator). The values ar random and I don't understand the motive
You don't understand how 'sscanf()' works, or how arrays
and pointers work.
I tryed either this variant :
char* porka_vakka = strchr(stringa, '/'); *porka_vakka = '\0'; // but either ' ' e '\n'
There are two possible results of these two lines, both of
which are undefined behavior:
1) The character '/' is not found in the string (which
causes 'strchr()' to return NULL), in which case you
try to dereference a NULL pointer. Undefined behavior.
2) The character '/' is found (and 'strchr()' returns its
address. You then try to modify it, but it's part of
a string literal. Undefined behavior.
i_letture = sscanf(stringa, "%s", &mese);
More undefined behavior. Attemt to modify string literal.
Wrong data type used with '%s'.
Finally, even if you do have writable storage for 'sscanf()'
note that you have no protection against the data overflowing
your array. Look up the 'width' flag for sscanf() format
specifiers. But neither this works...
Could anyone help me ?
I think the best advice I can give is to recommend you get
some good textbooks. http://www.accu.org/bookreviews/publ...ginner_s_c.htm
-Mike
Mike Wahler wrote: "Superfox il Volpone" <at***@email.it > wrote in message news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Hello
I have some problem with sscanf, I tryed this code but it doesn't works :
char* stringa = "18/2005"
"18/2005" is a string literal. Any attempts to modify any of its characters produces undefined behavior.
char mese[3]; char anno[5]; int i_letture;
i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno);
This gives undefined behavior. You're trying to modify a string literal.
Look again.
The first parameter of sscanf() is of type const char *
(qualified by restrict in C99 IIRC).
I guess you saw sscanf() and thought sprintf()...
<snip>
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mike Wahler <mk******@mkwah ler.net> wrote: i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno);
This gives undefined behavior. You're trying to modify a string literal.
I don't know that OP is "trying" to modify the string literal. In
practice, is sscanf() really likely to modify its first argument?
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Christopher Benson-Manica <at***@nospam.c yberspace.org> writes: Mike Wahler <mk******@mkwah ler.net> wrote:
> i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno);
This gives undefined behavior. You're trying to modify a string literal.
I don't know that OP is "trying" to modify the string literal. In practice, is sscanf() really likely to modify its first argument?
Doesn't the prototype of sscanf() promise to *not* modify its first argument?
--
Chris.
Superfox il Volpone a écrit : I have some problem with sscanf, I tryed this code but it doesn't works :
char* stringa = "18/2005" char mese[3]; char anno[5];
These are arrays of char
int i_letture;
i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno);
why & ? %s expects exactly what the name of an array of char is : the
address of its first element.
The separator must be read too :
#include <stdio.h>
int main(void)
{
char* stringa = "18/2005";
char mese[3];
char anno[5];
char c;
int i_letture = sscanf(stringa, "%2s%c%4s", mese, &c, anno);
if (i_letture == 3)
{
printf ("'%s' '%s'\n", mese, anno);
}
else
{
printf ("sscanf() error\n");
}
return 0;
}
mese[2] = anno[4] = '\0';
No need for that.
--
A+
Emmanuel Delahaye
"Michael Mair" <Mi**********@i nvalid.invalid> wrote in message
news:42******** *****@individua l.net... Mike Wahler wrote: "Superfox il Volpone" <at***@email.it > wrote in message news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Hello
I have some problem with sscanf, I tryed this code but it doesn't works :
char* stringa = "18/2005"
"18/2005" is a string literal. Any attempts to modify any of its characters produces undefined behavior.
char mese[3]; char anno[5]; int i_letture;
i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno);
This gives undefined behavior. You're trying to modify a string literal.
Look again. The first parameter of sscanf() is of type const char * (qualified by restrict in C99 IIRC).
I guess you saw sscanf() and thought sprintf()...
Um, yes. Oops. Blush. Sorry. Er, Happy New Year and all that. :-)
-Mike
Mike Wahler a écrit : char* stringa = "18/2005"
"18/2005" is a string literal. Any attempts to modify any of its characters produces undefined behavior.
How would it be modified ? I'm curious. Isn't the first parameter of
sscanf() a char const * ?
--
A+
Emmanuel Delahaye
Superfox il Volpone wrote: Hello
I have some problem with sscanf, I tryed this code but it doesn't works :
char* stringa = "18/2005"
Missing ';' termination char mese[3]; char anno[5]; int i_letture;
i_letture = sscanf(stringa, "%2s/%4s", &mese, &anno); mese[2] = anno[4] = '\0';
Unnecessary here: sscanf NUL-terminates the results The values ar random and I don't understand the motive
Well, it works for me! What makes you think the result is wrong?
Robert This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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
|
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;
|
by: baumann |
last post by:
hi all
there has 2 program
1) the first test program code
#include <stdio.h>
int main(void)
{
|
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:
| |
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.
|
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
|
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
|
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);
....
|
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...
|
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...
| |
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |