简体   繁体   中英

Pointer to a file pointer, how do I do it?

To get it out of the way immediately, this is an homework assignment, I really want to solve it myself so just answer the question and do not spread all the StackExchange love and point out more potential problems in my code and I will appreciate it very much! :)

What I have problem with is that I am trying to make a pointer to a file pointer. In my main function I created FILE * fp; witch I pass to a function called open_file(). What I then want to do is to pass the fp pointer to another function I have written that parse through each word. This is the function I am calling.

int
open_file(FILE * fp)
  {
    /*Open the text file*/
    fp = fopen("mate.txt", "r");
    if(fp == NULL)
      {
        return 2;
      }

    return 0;
  }

The way I pass the file pointer to this function from main() is like this

ret = open_file(&fp);

The compiler catch no errors and compile successfully, but instead of scanning the file and print out each word the terminal just freezing. If I instead do everything in open_file(), that is create the file pointer, calling the parser function and so on it works, also if I open the file directly in main.

The flags I use is -g -Wall -Werror and -Wextra

Just consequently implement your idea, introduce another level of indirection:

#include <stdio.h>
#include <assert.h>

...

int
open_file(FILE ** pfp)
  {
    assert(pfp != NULL);

    /*Open the text file*/
    *pfp = fopen("mate.txt", "r");
    if (*pfp == NULL)
      {
        return -1; /* By convention -1 indicates failure. */
      }

    return 0;
  }

Use it like this:

int
open_file(FILE ** pfp);

...


FILE * fp;
int ret = open_file(&fp);

Nicer would be:

int
open_file(const char * file_name, FILE ** pfp)
  {
    int result = 0;

    assert(file_name != NULL);
    assert(pfp != NULL);

    *pfp = fopen(file_name, "r");
    if (*pfp == NULL)
      {
        result = -1; /* By convention -1 indicates failure. */
      }

    return result;
  }

I believe in main you declared fp as pointer, in your case it wouldn't work because you pass pointer to *fp which means when you open a file pointer to this file is on stack of function open_file, when you get out of function, you loose this pointer. You can do it in three (or more) different ways:

First is return FILE pointer:

FILE *open_file(void)
{
    return fopen("mate.txt", "r");
}

FILE *fp = open_file();

Or the other option is to pass pointer to pointer *fp:

int open_file(FILE **fp)
{
    /*Open the text file*/
    *fp = fopen("mate.txt", "r");
    if(*fp == NULL)
    {
        return 2;
    }

    return 0;
}

FILE *fp;

open_file(&fp);

Or just plainly in main:

FILE *fp = fopen("mate.txt", "r");

Define the function the following way

int
open_file(FILE ** fp)
  {
    /*Open the text file*/
    *fp = fopen("mate.txt", "r");
    if( *fp == NULL)
      {
        return 2;
      }

    return 0;
  }

You can not define the function like

int
open_file(FILE * fp)
          ^^^^^^^^
  {
    /*Open the text file*/
    fp = fopen("mate.txt", "r");
    if( fp == NULL)
      {
        return 2;
      }

    return 0;
  }

and call it like

open_file( fp );

because in this case the function will deal with a copy of the original pointer. Function parameters are its local variable. Any changes of a copy of an argument does not influence on the argument itself. The original argument will be unchanged.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM