I am using the following function to read from a file.
void read_file(){
char buf[BUF_SIZE];
// file is a vraiable defined and assigned during initialization
int numread = pread(file->fd, buf, BUF_SIZE, file->curpos);
// other logic follows
file->buffer += buf;
}
The following function still in the same class evaluates the contents of the buffer read from file.
void evaluate(){
read_file();
//evaluate the file->buffer contents
}
From my understanding the stack variables are automatically 'removed' when a function exits but I cant seem to comprehend why the buf variable in the read_file() function is not being cleared on consecutive call to evauluate() .
For instance if I do;
int main(){
evaluate(); // first call works as expected
evaluate(); // second call buf variable still has contents from previous call
return 0;
}
I would appreciate a hint to the right direction in solving this. Thanks in advance.