怀疑打印效果的结果! [英] Doubt in printing results on function!

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

问题描述

你好!


我正在创建一个将一些信息复制到缓冲区的函数

输入一个分隔符字符串// ;结果之间。


我的问题是当我打印缓冲区时,看起来这个奇怪的结果:


cod:2

id:20

缓冲区是?

缓冲区是//?

最终缓冲区是://?


我为此做错了什么?我在键入下面使用的

函数。在此先感谢您的帮助。

int criaRespCreate(int cod,int id,char * buffer){

printf(" \\\
cod:%d",鳕鱼);

printf(" \ n id:%d",id);


int tam = 0;

char str [] =" //" ;;

int tcd = strlen(str);


//插入ID


memcpy (缓冲区,& id,LENGTH);


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


//插入delimitador caracter


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

tam + = tcd;


// inserir代码


memcpy(缓冲区+ tam,& cod,LENGTH);

tam + = LENGTH;


//插入delimitador caracter


memcpy(缓冲+ tam,& str,tcd);

tam + = tcd;


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


return tam;

}

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;
}

推荐答案

没关系!我已经解决了这个问题。


对于任何想知道问题的人,我都不能直接打印

缓冲区到屏幕使用简单的printf函数。我必须创建一个辅助函数,将特定的

偏移量的内容复制到变量然后打印出来!


然后下班! =)

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! =)


Pedro Pinto写道:
Pedro Pinto wrote:

>

没关系!我已经解决了这个问题。


对于任何想知道这个问题的人都是

,我不能直接打印

使用简单的printf函数将缓冲区添加到屏幕上。我必须创建一个辅助函数,将特定的

偏移量的内容复制到变量然后打印出来!


然后下班! =)
>
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 (缓冲区,%d%s%d%s,id,DELIM,cod,DELIM);


printf(" \\\
cod:%d \ n" ;,cod);

printf(" id:%d \ n",id);

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

返回tam;

}


int main(无效)

{

int cod = 2;

int id = 20;

char buffer [2 * sizeof id * CHAR_BIT / 3 + sizeof DELIM ];


printf(tam是%d \ n,criaRespCreate(cod,id,buffer));

返回0;

}


/ * END new.c * /


-

pete

/* 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("\nBuffer 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




Pedro Pinto写道:

Pedro Pinto wrote:

int criaRespCreate(int cod,int id,char * buffer){


printf(" \\\
cod:%d",cod);

p rintf(" \ n id:%d",id);


int tam = 0;

char str [] =" //" ;;

int tcd = strlen(str);


//插入ID


memcpy (缓冲区,& id,LENGTH);


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


//插入delimitador caracter


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

tam + = tcd;


// inserir代码

memcpy(缓冲区+ tam,& cod,LENGTH);

tam + = LENGTH;
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;



你不能通过将cod复制到char

缓冲区来将cod转换为字符串,这看起来就像你的试图做。什么是

LENGTH,你还没有提供我们的定义。这将产生

完全未定义的行为,具体取决于整数数据类型是如何代表



MQ

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


这篇关于怀疑打印效果的结果!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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