C 正在填充缓冲区,按地址作为参数传递
我在一个相当简单的任务中遇到了一个问题:我想读取函数中的一个文件,该函数将指向缓冲区的指针作为其参数之一。在函数中,缓冲区应该与文件的内容一起归档,这些内容稍后将在函数外部使用 但是,它在C 正在填充缓冲区,按地址作为参数传递,c,file,pointers,C,File,Pointers,我在一个相当简单的任务中遇到了一个问题:我想读取函数中的一个文件,该函数将指向缓冲区的指针作为其参数之一。在函数中,缓冲区应该与文件的内容一起归档,这些内容稍后将在函数外部使用 但是,它在readFile()内部显示的内容不正确,在readFile外部显示的内容甚至更多。此外,我想用十六进制(%02x)显示文件的内容,但我不知道如何显示。我正在努力解决指针的问题。你能帮我吗 uint8_t *buffer; int main(int argc, char *argv[]){ uint3
readFile()
内部显示的内容不正确,在readFile
外部显示的内容甚至更多。此外,我想用十六进制(%02x)显示文件的内容,但我不知道如何显示。我正在努力解决指针的问题。你能帮我吗
uint8_t *buffer;
int main(int argc, char *argv[]){
uint32_t i = 0;
unsigned long fileLen;
// Read file
fileLen = readFile(argv[2], &buffer);
printf("Buffer afterward: %s\n", &buffer);
}
unsigned long readFile(char *fileName, uint8_t *buffer){
unsigned long fileLen = 0;
uint8_t i;
FILE *file;
file = fopen (fileName, "r"); /* open the file for reading */
if(file==NULL){
printf("Error reading %c.\n", fileName);
return 0;
}
fseek(file, 0, SEEK_END);
fileLen=ftell(file);
fseek(file, 0, SEEK_SET);
*buffer=malloc(fileLen+1);
if(!buffer)
{
fprintf(stderr, "Memory error!");
fclose(file);
return;
}
fread(&buffer, fileLen, 1, file);
printf("Source message (%s, %ld bytes):\n%s\n", fileName, fileLen, &buffer);
puts("\n");
fclose(file);
return fileLen;
}
这是输出:
源消息(bla,16字节):
布拉布拉布
1234
� ſ� ſUJZ�����
缓冲区:p`
如果bla
的内容为:
blablub
1234
如果要在readFile函数中分配缓冲区,则假定缓冲区通过引用传递,而不是通过值传递。即:
unsigned long readFile(char *fileName, uint8_t **buffer);
if(!*buffer)
{
fprintf(stderr, "Memory error!");
fclose(file);
return;
}
因此,当您为它分配内存时,您可以使用malloc()
并将地址存储到*缓冲区
,但为了测试分配是否成功,您必须测试*缓冲区
,而不是缓冲区
。即:
unsigned long readFile(char *fileName, uint8_t **buffer);
if(!*buffer)
{
fprintf(stderr, "Memory error!");
fclose(file);
return;
}
对于函数的其余部分,您将使用*buffer
,而不是buffer
fread(*buffer, fileLen, 1, file);
printf("Source message (%s, %ld bytes):\n%s\n", fileName, fileLen, *buffer);
puts("\n");
fclose(file);
有点让人困惑,因为你说的
将一个指向缓冲区的指针作为它的参数之一
,但实际上你并没有将指针传递给缓冲区,而是将指针传递给一个整数(用作指针,实际上是双指针)
就个人而言,我更喜欢在read函数之外进行分配,这样就不会发生所有权转移(使内存管理更容易)。比如:
unsigned long readFile(char *fileName, unsigned char *buffer, uint8_t bufferSize){
// -- read at most x number of bytes (bufferSize) from the file to buffer
// -- return number of bytes read
return fileLen;
}
但要回答您的问题,并不是按值传递指针,指针应该是正确的,只是printf语句是错误的。这:
printf(“缓冲区:%s\n”,&Buffer)代码>应该类似于:printf(“后面的缓冲区:%s\n”,(char*)缓冲区)代码>我对您的代码做了一些调整:
/**
* in order to allocate the buffer inside the function you need
* to pass the address to the pointer
*/
unsigned long readFile(char *fileName, uint8_t **buffer)
{
unsigned long fileLen = 0;
uint8_t i = 0;
char* ch = NULL;
/* open the file in binary mode to get exact content
otherwise the fileLen will be wrong */
FILE *file = fopen (fileName, "rb");
if (file==NULL)
{
perror(fileName);
return 0;
}
fseek(file, 0, SEEK_END);
fileLen=ftell(file);
fseek(file, 0, SEEK_SET);
*buffer=malloc(fileLen+1);
if(!*buffer)
{
fprintf(stderr, "Memory error!");
fclose(file);
return;
}
/* read into the buffer, note the * in front of the buffer */
fread(*buffer, fileLen, 1, file);
/* since you do not know what is in the buffer, the following printf is a bit
risky, you cannot be sure that the buffer is terminated by a \0
printf("Source message (%s, %ld bytes):\n%s\n", fileName, fileLen, *buffer); */
/* instead do something like this */
printf( "Source nessage (%s, %ld bytes):", fileName, fileLen );
for (ch = *buffer ; *ch < *buffer + fileLen; ++ch)
{
/* if you want the output in hex */
printf( "%02X", *ch );
}
fclose(file);
return fileLen;
}
/**
*为了在函数中分配所需的缓冲区
*将地址传递给指针
*/
无符号长读取文件(char*文件名,uint8\u t**缓冲区)
{
无符号长文件长度=0;
uint8_t i=0;
char*ch=NULL;
/*以二进制模式打开文件以获取确切内容
否则,文件将出错*/
FILE*FILE=fopen(文件名,“rb”);
if(file==NULL)
{
perror(文件名);
返回0;
}
fseek(文件,0,SEEK_END);
fileLen=ftell(文件);
fseek(文件,0,搜索集);
*缓冲区=malloc(fileLen+1);
如果(!*缓冲区)
{
fprintf(stderr,“内存错误!”);
fclose(文件);
返回;
}
/*读入缓冲区,注意缓冲区前面的**/
fread(*缓冲区,文件,1,文件);
/*由于您不知道缓冲区中有什么,下面的printf有点
有风险,您无法确定缓冲区是否由\0终止
printf(“源消息(%s,%ld字节):\n%s\n”,文件名,fileLen,*缓冲区)*/
/*相反,你可以这样做*/
printf(“源存储空间(%s,%ld字节):”,文件名,fileLen);
对于(ch=*缓冲区;*ch<*缓冲区+文件长度;++ch)
{
/*如果您想要十六进制的输出*/
printf(“%02X”,*ch);
}
fclose(文件);
返回文件;
}
在main
中如何定义buffer
?抱歉,忘记将其包含在代码中。补充说。