I am trying to read a file and update a buffer with the contents. I didn't want readToBuffer
return char *
as I need to know the buffer size for later use. When I try to print the buffer
from main
It appears to be empty.
#include <stdlib.h>
#include <stdio.h>
long readToBuffer(char* path, char* buffer) {
FILE* handle = fopen(path,"rb");
fseek(handle,0L,SEEK_END);
long bufferSize = ftell(handle);
rewind(handle);
buffer = malloc(bufferSize*sizeof(char));
fread(buffer, sizeof(char), bufferSize, handle);
fclose(handle);
printf("%s",buffer);
return bufferSize;
}
int main(int argc, char* argv[]) {
char* path = argv[1];
char* buffer;
long bufferSize = readToBuffer(path, buffer);
printf("%ld\n",bufferSize);
printf("%s",buffer);
return 0;
}
Given a helloWorld.txt
input file why is the output as follows:
hello world
12
(null)$
why can I not read the buffer that was updated in the procedure readToBuffer
?