sscanf的 [英] SSCANF

查看:51
本文介绍了sscanf的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


我对sscanf有一些问题,我尝试了这段代码,但它不起作用



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'';


值随机而且我不'了解动机

我试过这个变种:


char * porka_vakka = strchr(stringa,''/'');

* porka_vakka =''\'''; //但是''''e''\ n''

i_letture = sscanf(stringa,"%s",& mese);


但这两种方法都不行......


有人能帮助我吗?


再见

- Atari


ps新年快乐:)

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 :)

推荐答案



Superfox il Volpone写道:

Superfox il Volpone wrote:
你好

我对sscanf有一些问题,我尝试了这段代码,但它不起作用


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] =''\''';


你有[3]和anno [5]。当你用sscanf阅读时,你告诉它

将字符数组存储在& mese和& anno中。 mese =& mese [0]和

anno =& anno [0]。

它应该是:i_letture = sscanf(stringa,"%2s /% 4s,mese,anno),否则

你试图在持有mese和anno

地址的位置写下这些值,而不是将它们写入地址

mese和anno point。

如果你真的想使用&然后写:

i_letture = sscanf(stringa,"%2s /%4s",& mese [0],& anno [0]);

值随机而且我不明白动机
我尝试了这个变体:

char * porka_vakka = strchr(stringa,''/'');
* porka_vakka =''\'''; //但是''''e''\ n''
i_letture = sscanf(stringa,"%s",& mese);
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=sscanf(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=sscanf(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" <在*** @ email.it>在消息中写道

news:11 ********************** @ g14g2000cwa.googlegr oups.com ...
"Superfox il Volpone" <at***@email.it> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
你好

我对sscanf有一些问题,我尝试了这段代码,但它不起作用


char * stringa =" 2005分之18"


" 18/2005"是一个字符串文字。任何修改

任何字符的尝试都会产生不确定的行为。

char mese [3]; char anno [5];
int i_letture;

i_letture = sscanf(stringa,"%2s /%4s",& mese,& anno);


这给出了未定义的行为。你试图修改

a字符串文字。


另外,因为''mese''和''anno''是数组,所以它们的名字<在这种情况下,
将重新评估指向他们的第一个元素

的指针。所以例如使用''mese'',而不是''& mese''。

%s必须与''char *''类型相匹配。表达式& mese

没有那种类型。它的类型是(*)[3](指向

三个char的数组)。不正确的类型。

mese [2] = anno [4] =''\''';


''sscanf()''已经为你应用了字符串终止符

(在适当的位置)


另外(暂时不考虑字符串文字问题)

请注意,如果'sscanf()''存储的数据大小为

小于数组,你的任意位置

''\'''作为最后一个数组元素将不会正确终止字符串

(会有''垃圾''数据和

终止符之间。

值随机,我不明白动机


你我不明白'sscanf()'是如何工作的,或者数组是如何工作的。

我试过这个变种:

char * porka_vakka = strchr(stringa,''/'');
* porka_vakka =''\'''; //但是要么''''''\\'n''


这两行可能有两种结果,都是

未定义的行为:


1)字符串中找不到字符'/''(

导致''strchr()''返回NULL),在这种情况下你要求
尝试取消引用NULL指针。未定义的行为。


2)找到字符'/''(''strchr()''返回其

地址。然后尝试修改它,但它是

a字符串文字的一部分。未定义的行为。

i_letture = sscanf(stringa,"%s",& mese);


更多未定义的行为.Attemt修改字符串文字。

错误的数据类型与''%s''一起使用。


最后,即使你确实有'sscanf()的可写存储'

请注意你没有防止数据溢出

你的数组。查找sscanf()格式的''width''标志

说明符。

但这两种方法都不行......

任何人都可以提供帮助我?
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 ?




我认为我能给出的最好的建议是建议你拿一些好的教科书。
http://www.accu.org/bookreviews/publ...ginner_s_c.htm

-Mike



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写道:
Mike Wahler wrote:
" Superfox il Volpone" <在*** @ email.it>在消息中写道
新闻:11 ********************** @ g14g2000cwa.googlegr oups.com ......
"Superfox il Volpone" <at***@email.it> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
你好

我对sscanf有一些问题,我尝试了这段代码,但它不起作用


char * stringa =" 18/2005"
Hello

I have some problem with sscanf, I tryed this code but it doesn''t works
:

char* stringa = "18/2005"



" 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);
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.




再看一下。

sscanf()的第一个参数是类型为const char *

(在C99 IIRC中受限制)。


我猜你看到了sscanf()并认为是sprintf()...


< snip>

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



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.


这篇关于sscanf的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆