我遇到了一个相当简单的任务问题:我希望在函数中找到一个文件,它将指向缓冲区的指针作为其参数之一。在函数中,缓冲区应该与文件的内容一起存档,稍后将在函数外部使用内容。
然而,它在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字节): blablablub 1234 s sUJZ
之后缓冲:p`
如果bla
的内容是:
blablablub
1234
答案 0 :(得分:3)
如果要在readFile函数中分配缓冲区,则假定buffer
通过引用传递,而不是按值传递。那就是:
unsigned long readFile(char *fileName, uint8_t **buffer);
因此,当您为其分配内存时,使用malloc()
并将地址存储到*buffer
,但为了测试分配是否成功,您必须测试*buffer
,而不是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);
答案 1 :(得分:0)
有点令人困惑,因为你说which takes a pointer to a buffer as one of its arguments
然后你实际上没有将指针传递给缓冲区,而是将指针传递给一个整数(用作指针,实际上是一个双指针)。
就个人而言,我更喜欢在读取功能之外进行分配,因此不会转移所有权(使内存管理更容易)。类似的东西:
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("Buffer afterward: %s\n", &buffer);
应该是:printf("Buffer afterward: %s\n", (char*)buffer);
答案 2 :(得分:0)
我稍微调整了你的代码:
/**
* 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;
}