无法将整数写入缓冲区内的偏移量(char *)

时间:2022-07-11 21:43:19

I'm trying to write an unsigned integer to a specific offset in buffer in my C program. The buffer is typical 1 byte char * buffer. I'm using memcpy to do this with some pointer arithmetic to point memcpy destination to a particular offset withing that buffer.

我试图在我的C程序中将无符号整数写入缓冲区中的特定偏移量。缓冲区是典型的1字节char *缓冲区。我正在使用memcpy来执行此操作,使用一些指针算法将memcpy目标指向具有该缓冲区的特定偏移量。

Code:

码:

char* ph = (char*) malloc(4096);

//Init buffer with '\0'
memset(ph, '\0', 4096);

//Set int to be written
unsigned int tupleCnt = 4;

//Write to 4th byte offset (int* + 1)
memcpy(((int*) ph) + 1, (void *) &tupleCnt, sizeof(tupleCnt));

However, this doesn't write anything to this buffer.

但是,这不会向此缓冲区写入任何内容。

Here's the hexdump of the file to which this buffer is written:

这是写入此缓冲区的文件的hexdump:

0000000 0000 0000 0000 0000 0000 0000 0000 0000
               ^

If I write it to 0th offset, it works:

如果我把它写到第0个偏移量,它可以工作:

//Write to 0th byte offset (int* + 0)
memcpy(((int*) ph) + 0, (void *) &tupleCnt, sizeof(tupleCnt));

Here's the hexdump:

这是hexdump:

0000000 0004 0000 0000 0000 0000 0000 0000 0000
          ^

By the way I'm using fwrite to write this buffer to file, if it makes any difference.

顺便说一句,我正在使用fwrite将此缓冲区写入文件,如果它有任何区别。

fwrite(ph, 1, strlen(ph), fp);

I also tried using byte by byte increment on char* pointers, it didn't help either. For example:

我也尝试在char *指针上使用逐字节递增,它也没有帮助。例如:

//Write to 4th byte offset (int* + 1)
memcpy(ph + 4, (void *) &tupleCnt, sizeof(tupleCnt));

Thanks in advance! Or is there any other way to write int (or any numeric) values to char* buffers? Except int to string conversation, which I really want to avoid. I think it's too much overhead and naive method. :)

提前致谢!或者有没有其他方法可以将int(或任何数字)值写入char *缓冲区?除了int到字符串对话,我真的想避免。我认为这是太多的开销和天真的方法。 :)

2 个解决方案

#1


1  

You problem not in memcpy but in the way you write to file:

你问题不在memcpy中,而在于你写入文件的方式:

fwrite(ph, 1, strlen(ph), fp);

this code write 0 bytes, because of strlen return count of bytes from begin to first '\0' in your case it zero bytes.

此代码写入0个字节,因为strlen返回从开始到第一个'\ 0'的字节数,在您的情况下为零字节。

#2


1  

strlen(ph) will stop counting when it sees a null character. Since you have zeroed out the buffer, strlen(ph) returns zero when you write on the 4th byte offset but not when you write on the first byte offset. Use fwrite(ph, 1, 4096, fp);

当strlen(ph)看到空字符时,它将停止计数。由于您已将缓冲区清零,因此当您在第4个字节偏移上写入时,strlen(ph)将返回零,但在写入第一个字节偏移时则不会。使用fwrite(ph,1,4096,fp);

Also to write integer you can use this

写整数也可以使用它

int *ih = (int*)ph;
ih[1] = tuplecnt;

#1


1  

You problem not in memcpy but in the way you write to file:

你问题不在memcpy中,而在于你写入文件的方式:

fwrite(ph, 1, strlen(ph), fp);

this code write 0 bytes, because of strlen return count of bytes from begin to first '\0' in your case it zero bytes.

此代码写入0个字节,因为strlen返回从开始到第一个'\ 0'的字节数,在您的情况下为零字节。

#2


1  

strlen(ph) will stop counting when it sees a null character. Since you have zeroed out the buffer, strlen(ph) returns zero when you write on the 4th byte offset but not when you write on the first byte offset. Use fwrite(ph, 1, 4096, fp);

当strlen(ph)看到空字符时,它将停止计数。由于您已将缓冲区清零,因此当您在第4个字节偏移上写入时,strlen(ph)将返回零,但在写入第一个字节偏移时则不会。使用fwrite(ph,1,4096,fp);

Also to write integer you can use this

写整数也可以使用它

int *ih = (int*)ph;
ih[1] = tuplecnt;