473,327 Members | 1,967 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,327 software developers and data experts.

Having problem on printing a string using printf?

jt
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);

}

Nov 14 '05 #1
11 1790
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.
}

Nov 14 '05 #2
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

Nov 14 '05 #3
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.
}

Nov 14 '05 #4
jt
None of your replies worked unfortunately.

I'm still confused why I can get this working...

Thanks for trying to help.
jt
"jt" <jt****@hotmail.com> wrote in message
news:om********************@tornado.tampabay.rr.co m...
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);

}

Nov 14 '05 #5
jt wrote:
None of your replies worked unfortunately.

I'm still confused why I can get this working...


Probably give an example input and the expected output...?

--
ade ishs

Nov 14 '05 #6
jt wrote:
None of your replies worked unfortunately.

I'm still confused why I can get this working...


A string is an array of characters with a zero-valued
character after its "payload" portion. Functions that work
with strings operate only on the portion up to (and sometimes
including) the zero byte, and pay no attention to anything
that comes after it. In particular, the "%s" specifier to
printf() will output all the characters that precede the
zero, and nothing else.

Your code fills the first few bytes of a character array
with a number in binary form, and then appends a string after
this numeric prefix. It then hands the whole thing to "%s",
which will print out successive characters from the array
until it finds a zero byte. There is almost certainly a zero
byte in the prefix, so "%s" stops generating output before it
ever gets to the appended string; as far as "%s" is concerned,
the string ended somewhere in the prefix. (It's likely that
the very first byte of the prefix is zero, so "%s" decides that
the string is empty and prints no characters at all.)

Even if "%s" did produce some output, the output would
most likely be garbage: the binary prefix probably doesn't
make any sense when viewed as characters.

What's the fix? Well, what are you trying to do? What is
the purpose of all this prefixing and printing?

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 14 '05 #7
jt wrote:
None of your replies worked unfortunately.

I'm still confused why I can get this working...

Thanks for trying to help.
jt
"jt" <jt****@hotmail.com> wrote in message
news:om********************@tornado.tampabay.rr.co m...
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);

}

So you've created a char array with a mangled int at the start and then
a string after, and you want to look at it? (Before presumably doing
something more useful.)

Maybe it'd be better to tell printf exactly what you want to look at.
(Not a string, but some numbers then a string). Something like:

int count;
for (count = 0; count < sizeof s; count++) {
printf("%02X", tmp[count]);
}
printf("%s\n", tmp+sizeof s);

This could give you output something like:
13000000There was an error.

Maybe that's what you're looking for.

--
Rob Morris: arr emm four four five (at) cam dot ac dot uk
Nov 14 '05 #8
On Sun, 12 Jun 2005 06:11:32 +0000, 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){
This is a good opportunity for a const char * parameter.
char tmp[1024],*pos;
unsigned int len,i,s;

memset(tmp,'\0',1024);
A string is terminated by the first null character, it isn't normally
necessary to set the whole array containing a string to null.
len=strlen(err_msg);
You have the length of the string here so it would be easy as well as good
programming practice to check that it will fit in tmp.
s = ntohl(len);
ntohl() is not defined by the C language. There is a function by this
name associated with socket libraries which changes byte order of a long
sized integer from network to host byte order. If that is what you want
then you should be using it with long sized value and result variables.
Note that using it here doesn't make sense because the value you are
passing it is in host byte order, not network byte order.
pos=tmp;
memcpy(pos,&s,sizeof(s));
Now you're trying to copy data from a binary object to what appears to be
intended to be the start of a string. This again makes no sense.
pos+=sizeof(s);
memcpy(pos,err_msg,len);
If you use

memcpy(pos,err_msg,len+1);

or just

strcpy(pos,err_msg);

you will copy the err_msg string properly i.e. including the null
character at the end.
printf("%s\n",tmp);
The string you're trying to output contains binary data from len. This is
likely to appear as garbage when you try to display it.
}


The code as it stands doesn't do anything sensible. And it is so oddball
that it is not clear what you wanted it to do. Explain what you want the
code to do and the output you expect. I'll start with a suggestion and see
where we go from there:

void ERR(const char *err_msg)
{
printf("%lx %s\n", (unsigned long)strlen(err_msg), err_msg);
{


Nov 14 '05 #9
jt
> void ERR(const char *err_msg)
{
printf("%2x %s\n", (unsigned long)strlen(err_msg), err_msg);
{


That is good, but we are not quite there of what I need to accomplish.

This is what printf prints: 2Athis is a test for the err messages to cad
(that good and we are getting somewhere not)

The client will read it in binary hex and you see that the 1st 4 bytes (32
61): 32 61 74 68 78 20 and so on .... would be a very large decimal.

note: my requirement is that fist four bytes should be in network byte order
and read as 00 2A ..... in binary hex. Not ascii as above.

I tried everything and it always comes out in ascii as you have it as well.

jt

Nov 14 '05 #10
jt wrote:
void ERR(const char *err_msg)
{
printf("%2x %s\n", (unsigned long)strlen(err_msg), err_msg);
{

That is good, but we are not quite there of what I need to accomplish.

This is what printf prints: 2Athis is a test for the err messages to cad
(that good and we are getting somewhere not)

The client will read it in binary hex and you see that the 1st 4 bytes (32
61): 32 61 74 68 78 20 and so on .... would be a very large decimal.

note: my requirement is that fist four bytes should be in network byte order
and read as 00 2A ..... in binary hex. Not ascii as above.

I tried everything and it always comes out in ascii as you have it as well.


You need something like this:

unsigned long len = strlen(err_msg);
printf ("%c%c%c%c%s",
(int)(len >> 24),
(int)((len >> 16) & 0xFF),
(int)((len >> 8) & 0xFF),
(int)(len & 0xFF),
err_msg);

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 14 '05 #11
On Sun, 12 Jun 2005 07:01:31 GMT, Martin Ambuhl
<ma*****@earthlink.net> wrote:
jt wrote:

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). <snip>


It is POSIX now, as of 2001/2002 = SUS3. Still not standard C.

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #12

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

Similar topics

5
by: drj0nson | last post by:
Without getting too mathematical : how do you print/handle big numbers? for ( int i = 1 ; i < 10 ; i ++) cout << (123456789 << i) << endl; 246913578 493827156 987654312 1975308624...
5
by: Kit | last post by:
Hey there, and thanks for reading a newbie post. I have a lex assignment in which we are supposed to grab strings from a C source file, and print them out to the screen, with escaped characters...
10
by: Maxime Barbeau | last post by:
Hi. I have the following code: uint32_t quadlet = 0x12345678; printf("quadlet = %08X\n", quadlet); my compiler give me the following warning: unsigned int format, uint32_t arg (arg 2) Is...
7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
5
by: Ross | last post by:
I have a quick question regarding printing string constants. When you printf() a string constant, how are the characters encoded that get sent to the standard output? Is it in the execution...
57
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p...
10
by: Pedro Pinto | last post by:
Hi there! I'm creating a function that copies some information to a buffer and enters a delimiter string "//" between results. My issue here is when i print the buffer it appears this weird...
4
by: Mukesh_Singh_Nick | last post by:
I am practicing printf formatting options. I want to print out a string literal as a raw string as opposed to its special meaning. For e.g I want to print out the "%f" part in: printf("%f\n"); ...
3
trkrbabe
by: trkrbabe | last post by:
Hi, this is my first time posting here. It appears that I am taking the same class as a few other people here. I have only been learning Java for about five weeks now. I have my Product class...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.