2013-11-20 94 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; 
}