它的时间结束了,但无论如何我想完成这个问题。我想读取二进制文件到缓冲区,稍后我想将这个缓冲区复制到数组。我就是这样做的;
int i=0;
char *buffer;
buffer=(char *)malloc(filelen+1); //filelen is length of binary file
while()
{
fread(buffer,100,1,filepointer); //filepointer is input binary file pointer,i wanna read 100 byte
strcpy(tup[i],buffer); //tup[] is char array.i want copy buffer to in this array
i++;
}
我在 strcpy 行出错,你不能将指针复制到整数之类的东西。
谢谢。
最佳答案
我想你想写:
strcpy(&tup[i],buffer);
但是还有许多其他问题。
- 你怎么知道 tup 和 buffer 是空终止的。缓冲区不太可能是空终止。因此,您应该使用
memcpy
(使用已知的计算长度) - 您确定无法覆盖
tup
的 then 结尾吗? - 由于您的临时缓冲区一次仅读取 100 个字节,因此您不需要分配整个文件大小的内存。
关于c - 从二进制文件中读取并复制到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15792944/