0

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?

7
  • 2
    Use std::ifstream, std::getline and std::string. Commented Jul 10, 2017 at 10:09
  • If *n is non-zero, the application shall ensure that *lineptr either points to an object of size at least *n bytes, or is a null pointer. getline. You have not initialized key and len. Commented Jul 10, 2017 at 10:12
  • the posted code does not compile on any C++ compilers (that is not called "Segmentation Fault"). Commented Jul 10, 2017 at 10:16
  • 1
    you need to allocate space for the key, either using malloc or have it as an array. In your case it is a char * which points to nothing. So, your readline writes data in an undefined memory space, causing memoy corruption or segmentation violations. Commented Jul 10, 2017 at 10:20
  • Make sure fp=fopen((tmpDir+"/"+filename).c_str(),"r"); yield a valid FILE pointer first. Commented Jul 10, 2017 at 10:21

1 Answer 1

1

Yes, it is possible to pass a FILE * to a function. After all, various standard C I/O functions accept an argument which is a pointer.

However, FILE is an opaque type. Whether the FILE * points at something (e.g a data structure) which is updated is implementation defined. But if your code is doing things that are valid on a FILE * (e.g. passing it to C I/O functions) then that would not explain a segmentation fault.

The partial code you have supplied is not sufficient to identify the cause of your "segmentation fault". Odds are, if the program is crashing, some code in your program is exhibiting undefined behaviour. But the simple act of passing a FILE *, obtained as a return value from fopen(), as an argument to a function would not be the cause. You need to look at other code in your program.

And, in C++, you would be better off using C++ streams than C I/O functions. But, at most, that will only change the symptom. If other code is the cause of your undefined behaviour, changing method of I/O (assuming you do it correctly) won't fix that.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Peter. As long as the position of the file pointer is updated correctly when the function returns then I think my question is answered. I've been over my code and the Segmentation Fault seems unrelated to the code I posted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.