I am trying to perform a read() from a file of which I don't know it's exact size into a variable so that I can do stuff on it later on, so I am looping like this:
char buf[BUFSIZE];
char* contentsOfFile;
fd = open(file, O_RDONLY);
while ( (nbytes = read(fd, buf, sizeof(buf)) ) > 0) { // keep reading until the end of file or error
strcat(contentsOfFile, buf);
}
Of course, this explodes unless contentsOfFile is another char array, but I cannot do this as I could have a bigger file than the number of bytes it could hold.
Is there any other library solution, or should I resort to malloc
?