-1
37行,我試圖在我的緩衝區指針中打印數據。但我有一個指針(gdhFileProp->緩衝區)到一個指針(char *緩衝區)。由於我使用的是fread(),它在讀取文件內容時不會添加\ 0。由於它是一個指針指針,我不能添加\ 0。fread()不添加空終止符[C],不能添加一個,因爲我有一個指針指針
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Struct to hold the properties of our file
typedef struct {
int size;
char* name;
char* buffer;
} FILEPROP;
FILEPROP* gdhFileProp;
// Function to parse file
int parseFile(char name[]){
// set name
gdhFileProp = malloc(sizeof(FILEPROP));
gdhFileProp->name = name;
// load file
FILE* gdhFilePointer;
gdhFilePointer = fopen("test.txt", "rb+");
// get file size
fseek(gdhFilePointer, 0, SEEK_END);
gdhFileProp->size = ftell(gdhFilePointer);
//get data and put it into the buffer
fseek(gdhFilePointer, 0, SEEK_SET);
fread(&gdhFileProp->buffer, 1, gdhFileProp->size, gdhFilePointer);
//Print data
printf("File properties:\nSize: %d bytes\nName: %s \nData: %s\n", gdhFileProp->size, gdhFileProp->name, &gdhFileProp->buffer);
return 1;
}
int main()
{
char name[] = "test.txt";
parseFile(name);
return 0;
}
我拿出malloc()函數,並用「。」替換了「 - >」。現在工作。謝謝。 – 2014-10-17 04:42:58
所以你還是不會調用'malloc()'並將結果賦給'buffer'?如果這是真的,那麼你的程序仍然是壞的。 – 2014-10-17 05:01:40
我爲char *分配了內存。而已。 – 2014-10-18 04:06:29