sprintf的问题...... [英] problems with sprintf...

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

问题描述

大家好....


我有一点问题,这让我疯了。我似乎无法做出任何感觉。我有这个小型网络服务器,当找到替换字符串时,它会替换来自

页面的一些数据。当我写数字时,我一切正常,甚至是sprintf,

。但是,如果我想写一个字符串,那么这一切都是错误的。

我正在尝试打印over在指针键上看起来像这样的函数(这只是真实的一个,它更大)。

当我打印浮点数或整数时即使我用一些像/ bm这样的几个字符写一个小的

字符串,也没有问题。工作良好。但是当我到达

的情况''c''它显示一个5位数字,总是相同,然后是我的字符串。

unsigned char * Key;

unsigned char NewKey [25];

unsigned int i;


if(TCPTxDataCount< 4)return;


Key = TCP_TX_BUF;


for(i = 0; i<(TCPTxDataCount - 2); i ++)

{

if(* Key ==''?'')

{

if(*(Key + 1)=='' ?'')

{

开关(*(Key + 2))

{

case'' a'':

{

sprintf(NewKey,"%4.2f",Func_returning_a_float());

memcpy(Key,NewKey ,5);

休息;

}

案例''b'':

{

if(Get_conditional_function()== 0xAFAF)

{

sprintf(NewKey,"%4.3f",Float_number_1());

memcpy(Key,NewKey,4);

休息;

}

其他

{

sp rintf(NewKey,"%4.3f%s",float_num_2()," / bm");

memcpy(Key,NewKey,7);

break ;

}

/ *直到这里一切顺利。 * /

case''c'':

{

sprintf(NewKey,"%s"," over");

memcpy(Key,NewKey,4);

休息;

}


/ *如果我重置要从4复制到Key的数据量,让我们说,

20,我得到一个7位数,总是一样的,然后是我的字符串....

类似于:" 20.7570over"

想知道发生了什么?我以为我正在使用sprintf,但是它好像b $ b似乎它不想和角色一起工作....

干杯!

Yodai


Hi all....

I have a little problem that''s driving me nuts. I can''t seem to make any
sense of it. I have this small webserver that substitutes some data from a
page when finds a substitution string. I all works fine, even the sprintf,
when I write numbers. But if I want to write a character string, it all goes
wrong.
I am trying to print "over" on the pointer Key on a function that looks
like this (this is only an extract of the real one, which is much larger).
While I print floats or integers there''s no problem, even if I write a small
string with a couple characters like "/bm" works fine. But when I get to the
case ''c'' it displays a 5 digit number, always the same, and then my string.
unsigned char *Key;
unsigned char NewKey[25];
unsigned int i;

if (TCPTxDataCount < 4) return;

Key = TCP_TX_BUF;

for (i = 0; i < (TCPTxDataCount - 2); i++)
{
if (*Key == ''?'')
{
if (*(Key + 1) == ''?'')
{
switch (*(Key + 2))
{
case ''a'' :
{
sprintf(NewKey, "%4.2f", Func_returning_a_float());
memcpy(Key, NewKey, 5);
break;
}
case ''b'' :
{
if (Get_conditional_function() == 0xAFAF)
{
sprintf(NewKey, "%4.3f", Float_number_1());
memcpy(Key, NewKey, 4);
break;
}
else
{
sprintf(NewKey, "%4.3f%s", float_num_2(), "/bm");
memcpy(Key, NewKey, 7);
break;
}
/* up until here it all works smoothly. */
case ''c'' :
{
sprintf(NewKey, "%s", "over");
memcpy(Key, NewKey, 4);
break;
}

/* if I reset the amount of data to be copied to Key from 4 to, lets say,
20, I get a 7 digit number, always the same, and then my string....
something like: "20.7570over"
Any idea of what''s going on? I thought I was using sprintf correctly, but it
seems it doesn''t want to work with characters....
Cheers!

Yodai



推荐答案

如果有人看到问题并感觉像回答,感觉自由。但是我确定了改变交换机字符的问题
。我还是不知道是什么

失败了,但我有一次幸运的罢工

我不知道 - 我怎么做了 - 它只是工作....


Yodai

" Yodai" <哟*** @ spamnot.mail.vu> escribióenel mensaje

news:sw *********************** @ telenews.teleline.e s ...
If someone sees the problem and feels like answering, feel free. But I
arranged the problem changing the switch caracters. I still don''t know what
was failing, but I had one of those lucky strikes
I-don''t-know-how-I-did-it-but-it-works....

Yodai
"Yodai" <yo***@spamnot.mail.vu> escribió en el mensaje
news:sw***********************@telenews.teleline.e s...
大家好......

我有一个小问题,这让我疯了。我似乎无法理解它。我有这个小型网络服务器,当找到替换字符串时,它会替换来自
页面的一些数据。当我写数字时,我一切都很好,甚至是sprintf,
。但如果我想写一个字符串,那么所有
都会出错。
我正在尝试打印over在指针上的键看起来像这样的函数(这只是真实的一个,它更大)。
当我打印浮点数或整数时,没有问题,甚至如果我用一个像/ bm这样的几个字符写一个
的小字符串工作良好。但当我得到
的情况''c''它显示一个5位数字,总是相同,然后我的
字符串。

unsigned char * Key;
unsigned char NewKey [25];
unsigned int i;

if(TCPTxDataCount< 4)return;

Key = TCP_TX_BUF;

for(i = 0; i<(TCPTxDataCount - 2); i ++)
{
if(* Key ==''?'')
{
if(*(Key + 1)==''?'')
{
开关(*(Key + 2))
{
案例''a' ':
{sprintf(NewKey,"%4.2f",Func_returning_a_float());
memcpy(Key,NewKey,5);
休息;
}
case''b'':
{
if(Get_conditional_function()== 0xAFAF)
{sprintf(NewKey,"%4.3f", Float_number_1());
memcpy(Key,NewKey,4); break;
}
其他
{sprintf(NewKey,"%4.3f%s",float_num_2()," / bm");
memcpy(Key,NewKey,7);
休息;
}
/ *直到这里一切顺利。 * /
案例''c'':
{sprintf(NewKey,%s,over);
memcpy(Key,NewKey,4) ;
休息;
}
/ *如果我重置要从4复制到Key的数据量,让我们说,
20,我得到了7位数字,总是一样的,然后我的字符串....
类似于:20.7570over

任何关于发生了什么的想法?我以为我正在使用sprintf,但是
似乎它不想和角色一起工作....

干杯!

Yodai

Hi all....

I have a little problem that''s driving me nuts. I can''t seem to make any
sense of it. I have this small webserver that substitutes some data from a
page when finds a substitution string. I all works fine, even the sprintf,
when I write numbers. But if I want to write a character string, it all goes wrong.
I am trying to print "over" on the pointer Key on a function that looks
like this (this is only an extract of the real one, which is much larger).
While I print floats or integers there''s no problem, even if I write a small string with a couple characters like "/bm" works fine. But when I get to the case ''c'' it displays a 5 digit number, always the same, and then my string.

unsigned char *Key;
unsigned char NewKey[25];
unsigned int i;

if (TCPTxDataCount < 4) return;

Key = TCP_TX_BUF;

for (i = 0; i < (TCPTxDataCount - 2); i++)
{
if (*Key == ''?'')
{
if (*(Key + 1) == ''?'')
{
switch (*(Key + 2))
{
case ''a'' :
{
sprintf(NewKey, "%4.2f", Func_returning_a_float());
memcpy(Key, NewKey, 5);
break;
}
case ''b'' :
{
if (Get_conditional_function() == 0xAFAF)
{
sprintf(NewKey, "%4.3f", Float_number_1());
memcpy(Key, NewKey, 4);
break;
}
else
{
sprintf(NewKey, "%4.3f%s", float_num_2(), "/bm");
memcpy(Key, NewKey, 7);
break;
}
/* up until here it all works smoothly. */
case ''c'' :
{
sprintf(NewKey, "%s", "over");
memcpy(Key, NewKey, 4);
break;
}

/* if I reset the amount of data to be copied to Key from 4 to, lets say,
20, I get a 7 digit number, always the same, and then my string....
something like: "20.7570over"
Any idea of what''s going on? I thought I was using sprintf correctly, but it seems it doesn''t want to work with characters....
Cheers!

Yodai



2004年1月13日星期二,格林尼治标准时间10:12:40,
Yodai< yo ***@spamnot.mail.vu>在Msg写了

。 < sw *********************** @ telenews.teleline.es>
On Tue, 13 Jan 2004 10:12:40 GMT,
Yodai <yo***@spamnot.mail.vu> wrote
in Msg. <sw***********************@telenews.teleline.es>
大家好......

我有一个小问题'让我疯了。我似乎无法理解它。我有这个小型网络服务器,当找到替换字符串时,它会替换来自
页面的一些数据。当我写数字时,我一切都很好,甚至是sprintf,
。但如果我想写一个字符串,那一切都是错误的。


虽然我不能立刻看到是什么原因导致你的代码表现得像b $ b那样,但这里有一些提示:


1)当使用sprintf()时,你冒着缓冲区溢出的风险,除非你确定没有可能的参数值可以导致

溢出。如果你有它,最好使用snprintf()。


2)字符串文字over需要5个字节,而不是4个字节(但这不是
似乎是你的问题)。相关说明:用strcpy替换对memcpy

的调用,它始终处理尾随零。并且确保

在任何情况下都可以使用Key来保存数据*。

带有几个字符的字符串,例如/ bm工作良好。但当我到达
情况''c''时,它显示一个5位数字,总是相同,然后是我的字符串。


''显示'是什么意思?您的代码段中没有代码

显示任何内容。你发布的代码似乎把正确的东西放到了

NewKey中,但NewKey可能会在其他地方被破坏。


你正在经历一个典型的案例未定义的行为。跟踪错误的难度越大,通常情况下就越尴尬

,当你终于找到它时;-)

case' 'c'':
{sprintf(NewKey,%s,over);
memcpy(Key,NewKey,4);
break;
}
知道发生了什么事吗?我以为我正在使用sprintf,但是它好像不想和角色一起工作....
Hi all....

I have a little problem that''s driving me nuts. I can''t seem to make any
sense of it. I have this small webserver that substitutes some data from a
page when finds a substitution string. I all works fine, even the sprintf,
when I write numbers. But if I want to write a character string, it all goes
wrong.
While I can''t see right away what causes your code to behave the way it
does, here a few tips:

1) When using sprintf() you run the risk of buffer overflow unless you
can make absolutely sure that no possible argument value can cause an
overflow. It''s better to use snprintf() if you have it.

2) The string literal "over" requires 5 bytes, not 4 (but that doesn''t
seem to be your problem here). Related note: Replace the calls to memcpy
with strcpy which always takes care of the trailing zero. And make sure
that Key is big enouch to hold the data *in all cases*.
string with a couple characters like "/bm" works fine. But when I get to the
case ''c'' it displays a 5 digit number, always the same, and then my string.
What does ''it displays'' mean? There is no code in your snippet that
displays anything. The code you posted seems to put the right thing into
NewKey, but NewKey might get corrupted someplace else.

You''re experiencing a classic case of Undefined Behaviour. The more
difficult it is to track the mistake, the more embarrasing it usually is
when you finally find it ;-)
case ''c'' :
{
sprintf(NewKey, "%s", "over");
memcpy(Key, NewKey, 4);
break;
}
Any idea of what''s going on? I thought I was using sprintf correctly, but it
seems it doesn''t want to work with characters....




只是为了咯咯笑......你有没有试过strcpy(Key,over)?应该完成

完全相同的事情。但是,是的,你正确使用sprintf。


- 丹尼尔


-

"跟我没关系!和你在一起? (来自坡道)



Just for giggles... Have you tried strcpy(Key, "over")? Should accomplish
the exact same thing. But yes, you''re using sprintf correctly.

--Daniel

--
"With me is nothing wrong! And with you?" (from r.a.m.p)


2004年1月13日星期二格林尼治标准时间10:33:47,

Yodai< yo *** @ spamnot。 mail.vu>在Msg写了

。 < fQ *********************** @ telenews.teleline.es>
On Tue, 13 Jan 2004 10:33:47 GMT,
Yodai <yo***@spamnot.mail.vu> wrote
in Msg. <fQ***********************@telenews.teleline.es>
如果有人看到问题并感觉像回答,随意。但是我安排了改变交换机字符的问题。我仍然不知道什么是失败的,但我有一次幸运的罢工
我不知道 - 我是怎么做的 - 但是 - 它起作用。 ...
If someone sees the problem and feels like answering, feel free. But I
arranged the problem changing the switch caracters. I still don''t know what
was failing, but I had one of those lucky strikes
I-don''t-know-how-I-did-it-but-it-works....




我很想你还在拥有UB,只是现在才发生这样的事情。

表现得很好你期待的方式...一段时间......直到你改变一些东西

某个地方,一切都变得混乱了。


已经多次发生在我身上(对于这个人来说,我是另一个人。

daresay)。


- 丹尼尔


-

和我一起没有错!和你在一起? (来自r.a.m.p)



I''m tempted to think that you''re still having UB, only now it happens to
behave the way you expect... for a while... until you change something
someplace and everything goes haywire again.

Has happened many times to me (and to everybody else on this ng, I
daresay).

--Daniel

--
"With me is nothing wrong! And with you?" (from r.a.m.p)


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

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