473,761 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,s izeof(s));
pos+=sizeof(s);
memcpy(pos,err_ msg,len);

printf("%s\n",t mp);

}

Nov 14 '05 #1
11 1829
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,s izeof(s));
pos+=sizeof(s);
memcpy(pos,err_ msg,len); Why not strcpy? Oh, well...
printf("%s\n",t mp);
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,s izeof(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",t mp);


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,s izeof(s));
pos+=sizeof(s);
memcpy(pos,err_ msg,len);
printf("%s\n",t mp);

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******** ************@to rnado.tampabay. rr.com...
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,s izeof(s));
pos+=sizeof(s);
memcpy(pos,err_ msg,len);

printf("%s\n",t mp);

}

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******** ************@to rnado.tampabay. rr.com...
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(er r_msg);
s = ntohl(len);

pos=tmp;
memcpy(pos,&s ,sizeof(s));
pos+=sizeof(s );
memcpy(pos,er r_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,s izeof(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",t mp);
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

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

Similar topics

5
6512
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 -344350048
5
4086
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 "converted". Here's part of the assignment text: ---------------------------------------------------------------- Example: main() { char *s = "This is string one";
10
32544
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 there any formtat I can use? %08uX ?
7
96320
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 %% should be used. Wouldn't it have been better (from design perspective) if the same escape character had been used in this case too. Forgive me for posting without verfying things with any standard compiler, i don't have the means for now.
5
1759
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 character set? Thanks in advance.
57
5671
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 expects type 'void *; but argument 2 has type 'void
10
1438
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 result: cod : 2 id : 20 Buffer is 
4
8161
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"); literally as "%f" rather than a zero value floating point/constant double (0.000000). I tried to escape the % sign but the compiler complained with a
3
2105
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 compiled and I finally got my Inventory class compiled but when I run the program it prints the first line correctly then I input the item name but then it prints the next two lines when it should print one and allow me to input then print the next. ...
0
9554
marktang
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...
0
9377
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
Oralloy
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...
1
9925
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,...
0
9811
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7358
isladogs
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...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.