2013-11-20 57 views
1

我遇到了一个相当简单的任务:我想在一个函数中创建一个文件,该文件将一个指向缓冲区的指针作为其参数之一。在函数中,缓冲区应该与文件的内容一起归档,并且稍后将在函数外使用内容。填充缓冲区,通过地址作为参数传递

但是,它不显示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; 
} 

这是输出:

'Source消息(BLA,16个字节): blablablub 小号sUJZ

缓冲液之后:p`

如果bla内容是:

blablablub
1234

+0

如何'buffer'在'main'界定? – user694733

+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); 
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);

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