If I pass a FILE pointer to a function, is it updated?
Can I do something like the following?
FILE* fp;
size_t read, len;
char *key;
fp=fopen((tmpDir+"/"+filename).c_str(),"r");
while((read=getline(&key,&len,fp))!=-1){
if (header_section){
processHeader(fp);
}else{
processBody(fp);
}
}
fclose(fp);
void processHeader(FILE* fp){
size_t read, len;
char *key;
while((read=getline(&key,&len,fp))!=-1){
... do header processing ...
if(strcmp(key,"end_of_header")==0){
return;
}
}
}
void processBody(FILE* fp){
size_t read, len;
char *key;
while((read=getline(&key,&len,fp))!=-1){
... process body data ...
}
}
The above code doesn't work (I get a Segmentation Fault). Is there a way to process parts of a text file in different functions according to the section of the file?
key, either usingmallocor have it as an array. In your case it is achar *which points to nothing. So, yourreadlinewrites data in an undefined memory space, causing memoy corruption or segmentation violations.fp=fopen((tmpDir+"/"+filename).c_str(),"r");yield a valid FILE pointer first.