Hi I'm reading data from a file put it in the buffer. Now I read the data from the file and got it in the buffer, but somehow the buffer is filled with some junk. In fact, I get the code from http://www.cplusplus.com/reference/clibrary/cstdio/fread/.I always get the result Reading Error and when I check the size of the lSize and result , they two are not the same.I'm new to C or C++.Can somebody help me? I tag both C and C++ since I don't know which one is the correct one. Sorry.
FILE *data_fp, *keyFile;
long lSize;
int i,j;
char hvalue[21];
char dt[300];
uint64_t insert_key;
data_fp = fopen("./aaa", "r+");
if (data_fp == NULL) {
printf("fopen aaa error\n");
exit(-1);
}
// obtain file size:
fseek (data_fp , 0 , SEEK_END);
lSize = ftell (data_fp);
rewind (data_fp);
// allocate memory to contain the whole file:
buffer = (char*) malloc (sizeof(char)*lSize);
if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
size_t result = fread (buffer,1,lSize,data_fp);
if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
puts(buffer);