使用printf打印字符串时遇到问题? [英] Having problem on printing a string using printf?

查看:120
本文介绍了使用printf打印字符串时遇到问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码,它没有printf。但是我通过使用for

语句并使用putchar到显示器,它在那里... ...


我如何才能使用printf?如果问题最后需要一个null

我该怎么办呢?

我在上一次记忆之后试过pos =''\'''也不起作用。

=================================== =============== ===========

void ERR(char * err_msg){


char tmp [1024],* pos;

unsigned int len,i,s;


memset(tmp,' '\0'',1024);


len = strlen(err_msg);

s = ntohl(len);

pos = tmp;

memcpy(pos,& s,sizeof(s));

pos + = sizeof(s);

memcpy(pos,err_msg,len);


printf("%s \ n",tmp);


}

解决方案

jt写道:

这是我的代码如下,它不打印。但是我通过使用for
语句并使用putchar到显示器,它在那里... ...

如何让它使用printf?如果问题最后需要一个空的
我怎么能这样做呢?
我在上一次memcpy之后尝试了pos =''\''',这也不起作用。< br => =============================================== === ===========

void ERR(char * err_msg){

char tmp [1024],* pos;
unsigned int len,i,s;

memset(tmp,''\'',1024);
可能是浪费。 YMMV。

len = strlen(err_msg);
s = ntohl(len);


ntohl不是一个标准的C函数(这使得它在这里偏离主题);它

甚至不是一个标准的POSIX功能(这使得它甚至可以在更多的地方使用它。)它存在的唯一功能通常是将网络* long *的字节顺序改为主机* long *。出于某种原因

您已选择将此转换的结果存储到int大小的

对象中。这不是一件好事。如果你使用这个非标准的

函数与你的问题有关,请在新闻组中询问 - 而不是
comp.lang.c - 它在哪里局部。

pos = tmp;
memcpy(pos,& s,sizeof(s));
pos + = sizeof(s);
memcpy(pos ,ERR_MSG,LEN);
为什么不strcpy?哦,好吧......

printf("%s \ n",tmp);


这是一个字节重新排序长的效果,存储在一个字符串的第一个字节中,然后放入字符串的第一个字节中是。如果这些字节中的任何
为0,那么你就死定了。试着看看

printf("%s \ n",pos);

的确如此。 pos应该指向字符串的开头。

}



jt写道:

这里是我的代码,它不是printf。但是我通过使用for
语句并使用putchar到显示器,它在那里... ...

如何让它使用printf?如果问题最后需要一个空的
我怎么能这样做呢?
我在上一次memcpy之后尝试了pos =''\''',这也不起作用。< br => =============================================== === ===========

void ERR(char * err_msg){

char tmp [1024],* pos;
unsigned int len,i,s;


len更好地声明为size_t,因为它用于存储strlen()的返回值

,即size_t。

memset(tmp,''\'',1024);


或者,您可以声明tmp:


char tmp [1024] = {0};

len = strlen(err_msg);
s = ntohl(len);
pos = tmp;
memcpy(pos,& s,sizeof(s));
pos + = sizeof (s);
memcpy(pos,err_msg,len);


您应该检查len的值。它不应该超过tmp的大小 -

sizeof s。

上面说你试图通过把pos =''\'''来解决这个问题。那个

会导致memcpy()无用,如果你只是想稍后printf()存储在tmp中的

字符串。

printf (%s \ n,tmp;




为了使其表现良好,你应该确保tmp [sizeof tmp

- 1] ==''\'''。


-

ishs


Martin Ambuhl写道(现在声称):

jt写道:
....

void ERR(char * err_msg){

char tmp [1024],* pos;
unsigned int len,i,s;
memset(tmp,''\\ \\ 0'',1024);
len = strlen(err_msg);
s = ntohl(len);
pos = tmp;
memcpy(pos,& s, sizeof(s));
pos + = sizeof(s);
memcpy(pos,err_msg,len);
printf("%s\ n",tmp);



它在空中是什么效果的字节重新排序长存储在一个
int然后放置进入字符串的第一个字节可能是。如果这些字节中的任何一个是0,那么你就死定了。试着看看
printf("%s \ n",pos);
是什么。 pos应该指向字符串的开头。




[澄清]

除非err_msg非常长(长到足以放一个非-zero进入

* len的每个*字节以及s),你确定*有一个零字节

,其中包含tmp的第一个sizeof字节。这可以保证你的

字符串在存储的err_msg之前结束,并且除了

之外什么都没有看到,可能代表s的前导非零字节是什么乱码

as。但是err_msg是从pos开始存储的,它在*之后是*任意零

字节,所以如果你想看到
$ b,则放(pos)而不是puts(tmp) $ b err_msg。

}



Here is my code below, it doesn''t printf. But I step it thru using the for
statement and use putchar to the display, its there...

How can I get it to use printf? If the problem is needing a null at the end
and how can I do this?
I tried pos=''\0'' after my last memcpy and that doesn''t work either.
================================================== ===========

void ERR(char * err_msg){

char tmp[1024],*pos;
unsigned int len,i,s;

memset(tmp,''\0'',1024);

len=strlen(err_msg);
s = ntohl(len);

pos=tmp;
memcpy(pos,&s,sizeof(s));
pos+=sizeof(s);
memcpy(pos,err_msg,len);

printf("%s\n",tmp);

}

解决方案

jt wrote:

Here is my code below, it doesn''t printf. But I step it thru using the for
statement and use putchar to the display, its there...

How can I get it to use printf? If the problem is needing a null at the end
and how can I do this?
I tried pos=''\0'' after my last memcpy and that doesn''t work either.
================================================== ===========

void ERR(char * err_msg){

char tmp[1024],*pos;
unsigned int len,i,s;

memset(tmp,''\0'',1024); probably a waste. YMMV.
len=strlen(err_msg);
s = ntohl(len);
ntohl is not a standard C function (which makes it off-topic here); it
isn''t even a standard POSIX function (which makes it off-topic in even
more places). Where it exists, its sole function is usually to change
the byte order of a network *long* to a host *long*. For some reason
you have chose to store the result of this conversion into an int-sized
object. This cannot be a good thing. If your use of this non-standard
function has anything to do with your problem, ask in a newsgroup -- not
comp.lang.c -- where it is topical.
pos=tmp;
memcpy(pos,&s,sizeof(s));
pos+=sizeof(s);
memcpy(pos,err_msg,len); Why not strcpy? Oh, well...
printf("%s\n",tmp);
It is up in the air what the effect of a byte-reordered long stored in a
int and then placed into the first bytes of a string might be. If any
of those bytes are 0s, you''re dead. Try seeing what
printf("%s\n", pos);
does. pos should point to the beginning of the string.
}



jt wrote:

Here is my code below, it doesn''t printf. But I step it thru using the for
statement and use putchar to the display, its there...

How can I get it to use printf? If the problem is needing a null at the end
and how can I do this?
I tried pos=''\0'' after my last memcpy and that doesn''t work either.
================================================== ===========

void ERR(char * err_msg){

char tmp[1024],*pos;
unsigned int len,i,s;
len is better declared as size_t as it''s used to store the return value
of strlen(), which is size_t.
memset(tmp,''\0'',1024);
Alternatively, you could declare tmp:

char tmp[1024] = {0};
len=strlen(err_msg);
s = ntohl(len);
pos=tmp;
memcpy(pos,&s,sizeof(s));
pos+=sizeof(s);
memcpy(pos,err_msg,len);
You should check the value of len. It shouldn''t exceed sizeof tmp -
sizeof s.
Above you said that you tried to solve this by putting pos = ''\0''. That
would cause the memcpy() to be useless if you just want to printf() the
string stored in tmp later.
printf("%s\n",tmp);



In order for this to behave well, you should ensure that tmp[sizeof tmp
- 1] == ''\0''.

--
ishs


Martin Ambuhl wrote (and now claifies):

jt wrote: ....

void ERR(char * err_msg){

char tmp[1024],*pos;
unsigned int len,i,s;
memset(tmp,''\0'',1024);
len=strlen(err_msg);
s = ntohl(len);
pos=tmp;
memcpy(pos,&s,sizeof(s));
pos+=sizeof(s);
memcpy(pos,err_msg,len);
printf("%s\n",tmp);


It is up in the air what the effect of a byte-reordered long stored in a
int and then placed into the first bytes of a string might be. If any
of those bytes are 0s, you''re dead. Try seeing what
printf("%s\n", pos);
does. pos should point to the beginning of the string.



[Clarification]
Unless err_msg is very very long (long enough to put an non-zero into
*every* byte of len and so of s), you are *sure* to have a zero byte
withing the first sizeof s bytes of tmp. This guarantees that your
string ends before the stored err_msg and that you see nothing but
whatever gibberish the leading non-zero bytes of s might be represented
as. But err_msg is stored starting at pos, which is *after* any zero
bytes in s, so puts(pos) rather than puts(tmp) if you want to see the
err_msg.

}



这篇关于使用printf打印字符串时遇到问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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