473,770 Members | 2,781 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Doubt in printing results on function!

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 
Buffer is // 
final buffer is: // 

What am i doing wrong for this to happen? I'm typing bellow the
function used. Thanks in advance for any help.
int criaRespCreate( int cod, int id, char *buffer){
printf("\n cod : %d", cod);
printf("\n id : %d", id);

int tam = 0;
char str[]=" // ";
int tcd = strlen(str);

// insert ID

memcpy(buffer,& id,LENGTH);

printf("\n Buffer is %s", buffer);

// Insert delimitador caracter

memcpy(buffer + tam,&str,tcd);
tam += tcd;

// inserir code

memcpy(buffer + tam,&cod,LENGTH );
tam += LENGTH;

// Insert delimitador caracter

memcpy(buffer + tam ,&str,tcd);
tam += tcd;

printf("\n Buffer is %s", buffer);

return tam;
}

Dec 2 '06 #1
10 1441
Never mind! I've solved the issue.

For anyone who wishes to know the issue was that i can't print directly
the buffer to the screen using a simple printf function. I have to
create an auxiliary function that copies the content of a specific
offset to a variable and then print it!

Off to work then! =)

Dec 2 '06 #2
Pedro Pinto wrote:
>
Never mind! I've solved the issue.

For anyone who wishes to know the issue was
that i can't print directly
the buffer to the screen using a simple printf function. I have to
create an auxiliary function that copies the content of a specific
offset to a variable and then print it!

Off to work then! =)
/* BEGIN new.c */

#include <stdio.h>
#include <string.h>
#include <limits.h>

#define DELIM " // "

int criaRespCreate( int cod, int id, char *buffer)
{
int tam = sprintf(buffer, "%d%s%d%s", id, DELIM, cod, DELIM);

printf("\n cod : %d\n", cod);
printf( " id : %d\n", id);
printf("\nBuffe r is :%s\n", buffer);
return tam;
}

int main(void)
{
int cod = 2;
int id = 20;
char buffer[2 * sizeof id * CHAR_BIT / 3 + sizeof DELIM];

printf("tam is %d\n", criaRespCreate( cod, id, buffer));
return 0;
}

/* END new.c */

--
pete
Dec 2 '06 #3
MQ

Pedro Pinto wrote:
int criaRespCreate( int cod, int id, char *buffer){
printf("\n cod : %d", cod);
printf("\n id : %d", id);

int tam = 0;
char str[]=" // ";
int tcd = strlen(str);

// insert ID

memcpy(buffer,& id,LENGTH);

printf("\n Buffer is %s", buffer);

// Insert delimitador caracter

memcpy(buffer + tam,&str,tcd);
tam += tcd;

// inserir code
memcpy(buffer + tam,&cod,LENGTH );
tam += LENGTH;
You can't just convert cod to a string by copying it into a char
buffer, and that looks like what you are trying to do. And what is
LENGTH, you have not provided us the definition. This will produce
totally undefined behaviour depending on how the integer data type is
represented.

MQ

Dec 2 '06 #4
Pedro Pinto wrote:
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 
Buffer is // 
final buffer is: // 
You haven't told us what your intended result is.
What am i doing wrong for this to happen? I'm typing bellow the
function used. Thanks in advance for any help.
int criaRespCreate( int cod, int id, char *buffer){
printf("\n cod : %d", cod);
printf("\n id : %d", id);

int tam = 0;
char str[]=" // ";
int tcd = strlen(str);
size_t would be better.
>
// insert ID

memcpy(buffer,& id,LENGTH);
What is LENGTH? What did you want this statement to do?

At the moment you're just copying the bit pattern for an int
directly into a buffer that appears to be character string.
The resultant 'characters' needn't be printable.
printf("\n Buffer is %s", buffer);

// Insert delimitador caracter

memcpy(buffer + tam,&str,tcd);
tam += tcd;

// inserir code

memcpy(buffer + tam,&cod,LENGTH );
tam += LENGTH;

// Insert delimitador caracter

memcpy(buffer + tam ,&str,tcd);
tam += tcd;

printf("\n Buffer is %s", buffer);

return tam;
}
It looks like you want something closer to...

int criaRespCreate( int cod, int id, char *buffer)
{
return sprintf(buffer, "%d // %d // ", id, cod);
}

--
Peter

Dec 2 '06 #5
On Sat, 02 Dec 2006 02:01:48 GMT, pete <pf*****@mindsp ring.comwrote:
>Pedro Pinto wrote:
>>
Never mind! I've solved the issue.

For anyone who wishes to know the issue was
that i can't print directly
the buffer to the screen using a simple printf function. I have to
create an auxiliary function that copies the content of a specific
offset to a variable and then print it!

Off to work then! =)

/* BEGIN new.c */

#include <stdio.h>
#include <string.h>
#include <limits.h>

#define DELIM " // "

int criaRespCreate( int cod, int id, char *buffer)
{
int tam = sprintf(buffer, "%d%s%d%s", id, DELIM, cod, DELIM);

printf("\n cod : %d\n", cod);
printf( " id : %d\n", id);
printf("\nBuffe r is :%s\n", buffer);
return tam;
}

int main(void)
{
int cod = 2;
int id = 20;
char buffer[2 * sizeof id * CHAR_BIT / 3 + sizeof DELIM];

printf("tam is %d\n", criaRespCreate( cod, id, buffer));
return 0;
}

/* END new.c */
sizeof DELIM equals sizeof(char*). That would most likely differ from
strlen(DELIM), which is what I think you meant.

--
jay
Dec 2 '06 #6
>On Sat, 02 Dec 2006 02:01:48 GMT, pete <pf*****@mindsp ring.comwrote:
[enormous amounts of snippage]
>>#define DELIM " // "
char buffer[2 * sizeof id * CHAR_BIT / 3 + sizeof DELIM];
In article <g9************ *************** *****@4ax.com>,
jaysome <ja*****@spamco p.netwrote:
>sizeof DELIM equals sizeof(char*).
Possibly, but generally not. Since DELIM expands to " // ", sizeof
DELIM must be exactly 5. The reason is that " // " is an array
containing 5 "char"s -- specifically {' ', '/', '/', ' ', '\0'} --
so its size must be 5 * sizeof(char), or 5 * 1, or 5. This is
the same as sizeof(char *) only if sizeof(char *) is 5 (which is
unusual, in my experience).
>That would most likely differ from strlen(DELIM) ...
This is true, because strlen() does not count the terminating '\0'.
>which is what I think you meant.
No, had he used strlen(), he would have needed an extra +1 -- and
the declaration would have been valid only in C99, because strlen(arg)
is not a constant-expression, turning "buffer" into a Variable
Length Array.

Note also that sizeof "abc\0def" is 8, while strlen("abc\0de f") is
3: the result of sizeof will be more than one greater than the
equivalent strlen if the string literal contains embedded '\0'
characters.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Dec 2 '06 #7
jaysome wrote:
>
On Sat, 02 Dec 2006 02:01:48 GMT, pete <pf*****@mindsp ring.comwrote:
Pedro Pinto wrote:
>
Never mind! I've solved the issue.

For anyone who wishes to know the issue was
that i can't print directly
the buffer to the screen using a simple printf function. I have to
create an auxiliary function that copies the content of a specific
offset to a variable and then print it!

Off to work then! =)
/* BEGIN new.c */

#include <stdio.h>
#include <string.h>
#include <limits.h>

#define DELIM " // "

int criaRespCreate( int cod, int id, char *buffer)
{
int tam = sprintf(buffer, "%d%s%d%s", id, DELIM, cod, DELIM);

printf("\n cod : %d\n", cod);
printf( " id : %d\n", id);
printf("\nBuffe r is :%s\n", buffer);
return tam;
}

int main(void)
{
int cod = 2;
int id = 20;
char buffer[2 * sizeof id * CHAR_BIT / 3 + sizeof DELIM];

printf("tam is %d\n", criaRespCreate( cod, id, buffer));
return 0;
}

/* END new.c */

sizeof DELIM equals sizeof(char*). That would most likely differ from
strlen(DELIM), which is what I think you meant.
The type of (" // ") is (array of 5 char).
String literals are not converted to pointers
when they are the operand of sizeof.

String literals are also not converted to pointers when
they are the operand of the address operator.
(&" // ")

String literals are also not converted to pointers when
they are the initializer for an array.
char array[] = " // ";

And those are the three exceptions to string literals
always being converted to pointers.

--
pete
Dec 2 '06 #8
Geeeeee i was thinking in creating a print function to extract the
results but the solutions provided here give me a lot more choices! =)

This is a client program that send with an UDP socket a buffer with
some information to a server! LENGTH is the length of an int, that is
4. Thank you for the help, i'll post later today the final solution!

Regards

Pedro Pinto

pete escreveu:
jaysome wrote:

On Sat, 02 Dec 2006 02:01:48 GMT, pete <pf*****@mindsp ring.comwrote:
>Pedro Pinto wrote:
>>
>Never mind! I've solved the issue.
>>
>For anyone who wishes to know the issue was
>that i can't print directly
>the buffer to the screen using a simple printf function. I have to
>create an auxiliary function that copies the content of a specific
>offset to a variable and then print it!
>>
>Off to work then! =)
>
>/* BEGIN new.c */
>
>#include <stdio.h>
>#include <string.h>
>#include <limits.h>
>
>#define DELIM " // "
>
>int criaRespCreate( int cod, int id, char *buffer)
>{
int tam = sprintf(buffer, "%d%s%d%s", id, DELIM, cod, DELIM);
>
printf("\n cod : %d\n", cod);
printf( " id : %d\n", id);
printf("\nBuffe r is :%s\n", buffer);
return tam;
>}
>
>int main(void)
>{
int cod = 2;
int id = 20;
char buffer[2 * sizeof id * CHAR_BIT / 3 + sizeof DELIM];
>
printf("tam is %d\n", criaRespCreate( cod, id, buffer));
return 0;
>}
>
>/* END new.c */
sizeof DELIM equals sizeof(char*). That would most likely differ from
strlen(DELIM), which is what I think you meant.

The type of (" // ") is (array of 5 char).
String literals are not converted to pointers
when they are the operand of sizeof.

String literals are also not converted to pointers when
they are the operand of the address operator.
(&" // ")

String literals are also not converted to pointers when
they are the initializer for an array.
char array[] = " // ";

And those are the three exceptions to string literals
always being converted to pointers.

--
pete
Dec 2 '06 #9
Pedro Pinto wrote:
>
Geeeeee i was thinking in creating a print function to extract the
results but the solutions provided here give me a lot more choices! =)

This is a client program that send with an UDP socket a buffer with
some information to a server! LENGTH is the length of an int, that is
4. Thank you for the help, i'll post later today the final solution!
int tam = sprintf(buffer, "%d%s%d%s", id, DELIM, cod, DELIM);
char buffer[2 * sizeof id * CHAR_BIT / 3 + sizeof DELIM];
That should be
char buffer[2 * (sizeof id * CHAR_BIT / 3 + sizeof DELIM)];
instead.

I forgot that DELIM was written twice.

--
pete
Dec 2 '06 #10

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

Similar topics

9
4118
by: Jody Gelowitz | last post by:
I am trying to find the definition of "Safe Printing" and cannot find out exactly what this entitles. The reason is that I am trying to print contents from a single textbox to no avail using the PrintDialog control under a security setting with only SafePrinting allowed. I have attached a sample project that I am using to try to accomplish this. The print dialog appears, but when I press the Print button, I get an exception (at the end...
6
1397
by: Rex Eastbourne | last post by:
Hi all, I've written the following simple macro called debug(aname, avalue) that prints out the name of an expression and its value: def debug(aname, avalue): print aname, 'is': pprint.pprint(avalue) An example call is:
4
6478
by: Suzanka | last post by:
Hello, I have an application written in C# on visual studio .NET. It is a web aplication. The application consists of many different forms, that users occassionaly want to print out for filing. When they log to application (through web browser) and choose the print option, on the right margin few cm get cut off (so some fields do not print out). Is there any function that ensure that when user pritns he gets the
2
2199
by: Tim | last post by:
Hi, I have created some code for printing a column report. The default page size is 850 x 1100, which is correct. The print preview looks perfect. The data is positioned exactly where it should be. (centered) When I go to print however, the data is shifted to the right. About a margin's worth. I set the margins at 50 and it looks like it is at 100. I checked the code and the starting X position is at 50. So why is it
6
2219
by: MJ | last post by:
Is it possible to print varying numbers of labels from Access?
11
2251
by: krishnamaddi | last post by:
Hi Friends, I am having some doubt. I want to print an ASP Page, for this i need to use java script. the problem is while printing if the line is too big, i can't print those data properly . i am losing some data in the last. What i want is if at all, the line is big, it should take landscape proprety in place of Portrait.. Does anyone knows the answer give me reply soon
7
2382
by: Burhan | last post by:
Hello Group: I am in the planning stages of an application that will be accessed over the web, and one of the ideas is to print a barcode that is generated when the user creates a record. The application is to track paperwork/items and uses barcodes to easily identify which paper/item belongs to which record. Is there an easy way to generate barcodes using Python -- considering the application will be printing to a printer at the...
1
1963
by: benfly08 | last post by:
Hi, I got a printing problem for my C# program. I need to pull out records from database and format them and then print them out. I used PrintDocument class and PrintPageEventHandler function to handle multiple page printing. When I tried printing the records in testing environment which has an HP LaserJet 2420dn printer, it worked fine. However, when I put the program into production environment which has an EPSON FX-2190, the results...
8
9018
by: Frank Rizzo | last post by:
I am trying to print huge images (much bigger than target paper). I try and use e.PageSettings.HardMarginX and e.PageSettings.HardMarginY in the PrintDocument's PrintPage event to try and determine the maximum area that I can print on. However, the edge of the image invariably gets cut off, as if the HardMargin info is wrong. I posted the code below as I can't understand what I am doing wrong. Is information in e.PageSettings reliable?...
0
9591
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
10057
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10002
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
9869
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...
0
5312
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...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3970
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3575
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
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.