Passing a file handle to a function - newb Q

I want to make a little function called openfile that would take a file name and a file handle and go about opening the file. This is so that i could have a function that checks if the file exists, and if not then creates it. Or if there's an error, then display it. It's just supposed to be a building block function that handles that kind of stuff for me.

Anyway here's the function so far (i haven't put in a lot of error checking yet, so it's sort of redundant right now, but that'll change)

void openfile(char file_name[11],FILE *file_handle){

file_handle=fopen(file_name,"r");
if(file_handle == NULL) {
printf("Problem Opening file!");
}
}

And i have a similar closefile function that looks just about the same except that it only takes a FILE *file_handle

And in main i have:

FILE *myfile;

openfile("afile.txt",myfile);

closefile(myfile);
One wouldn't think that this would be a problem, but they'd be wrong.

It compiles fine but when i run it i get: Segmentation fault. The interesting thing is that it runs fine when i comment out the closefile function. Obviously the problem is pointer related... i just don't know what it is. A little help would be appreciated.

Thanks in advance,
KIRT

Comments

  • Hey,

    You should learn to figure out how to use a debugger. Anyway as close(myfile) seems to be the problem - you should have posted the function for closefile.

    Anyway here is how you open a file

    [code]
    void OpenFile(char *filename, FILE *fp)
    {
    fp = fopen(filename, "r");

    // do whatever you want with the file

    fclose(fp);
    }
    [/code]

    I am assuming that myFile is null even after you open the file in the function OpenFile so when the CloseFile(myFile) executes - it tries to close myFile which is null.

    : I want to make a little function called openfile that would take a file name and a file handle and go about opening the file. This is so that i could have a function that checks if the file exists, and if not then creates it. Or if there's an error, then display it. It's just supposed to be a building block function that handles that kind of stuff for me.
    :
    : Anyway here's the function so far (i haven't put in a lot of error checking yet, so it's sort of redundant right now, but that'll change)
    :
    : void openfile(char file_name[11],FILE *file_handle){
    :
    : file_handle=fopen(file_name,"r");
    : if(file_handle == NULL) {
    : printf("Problem Opening file!");
    : }
    : }
    :
    : And i have a similar closefile function that looks just about the same except that it only takes a FILE *file_handle
    :
    : And in main i have:
    :
    : FILE *myfile;
    :
    : openfile("afile.txt",myfile);
    :
    : closefile(myfile);
    : One wouldn't think that this would be a problem, but they'd be wrong.
    :
    : It compiles fine but when i run it i get: Segmentation fault. The interesting thing is that it runs fine when i comment out the closefile function. Obviously the problem is pointer related... i just don't know what it is. A little help would be appreciated.
    :
    : Thanks in advance,
    : KIRT
    :

  • Well i could do that but the point of having an openfile function was to open the file and then have other functions do whatever it is that they do with it. Then use a separate closefile function later on when i was done with the file. (BTW, when i did what you described the program worked fine, so there's no problem with closefile. The program just didn't work the way that i wanted it to)

    It seems to me that it would be benficial to have some specialized and reusable functions that can all operate on an already opened file. Say, one that reads in strings line by line and one that reads binary files, etc. That way i could combine them to perform specific tasks. I'm trying to think UNIX here. =)

    So what i really need to know is, is it possible to leave a file open after it has been opened inside a function, then close it outside of that function? Or am i approaching this whole situation the wrong way?

    : Hey,
    :
    : You should learn to figure out how to use a debugger. Anyway as close(myfile) seems to be the problem - you should have posted the function for closefile.
    :
    : Anyway here is how you open a file
    :
    : [code]
    : void OpenFile(char *filename, FILE *fp)
    : {
    : fp = fopen(filename, "r");
    :
    : // do whatever you want with the file
    :
    : fclose(fp);
    : }
    : [/code]
    :
    : I am assuming that myFile is null even after you open the file in the function OpenFile so when the CloseFile(myFile) executes - it tries to close myFile which is null.
    :
    : : I want to make a little function called openfile that would take a file name and a file handle and go about opening the file. This is so that i could have a function that checks if the file exists, and if not then creates it. Or if there's an error, then display it. It's just supposed to be a building block function that handles that kind of stuff for me.
    : :
    : : Anyway here's the function so far (i haven't put in a lot of error checking yet, so it's sort of redundant right now, but that'll change)
    : :
    : : void openfile(char file_name[11],FILE *file_handle){
    : :
    : : file_handle=fopen(file_name,"r");
    : : if(file_handle == NULL) {
    : : printf("Problem Opening file!");
    : : }
    : : }
    : :
    : : And i have a similar closefile function that looks just about the same except that it only takes a FILE *file_handle
    : :
    : : And in main i have:
    : :
    : : FILE *myfile;
    : :
    : : openfile("afile.txt",myfile);
    : :
    : : closefile(myfile);
    : : One wouldn't think that this would be a problem, but they'd be wrong.
    : :
    : : It compiles fine but when i run it i get: Segmentation fault. The interesting thing is that it runs fine when i comment out the closefile function. Obviously the problem is pointer related... i just don't know what it is. A little help would be appreciated.
    : :
    : : Thanks in advance,
    : : KIRT
    : :
    :
    :
  • hey, hope this helps

    [code]
    FILE *openfile(const char *filename, const char *mode) {
    FILE *temp = fopen(filename,mode);
    if(temp == NULL) {
    printf("openfile: cannot open %s!
    ",
    filename);
    return(NULL);
    } else
    return(temp);
    }

    int main() {
    FILE *test;
    test = openfile("test.txt","w");
    fclose(test);
    return(0);
    }
    [/code]

    All it does is check if the open was succsessful or not but yeah u will probly want to add your own stuff
  • Thanks for replying, but i managed to figure that out already, and my function looked almost identical.

    KIRT

    : hey, hope this helps
    :
    : [code]
    : FILE *openfile(const char *filename, const char *mode) {
    : FILE *temp = fopen(filename,mode);
    : if(temp == NULL) {
    : printf("openfile: cannot open %s!
    ",
    : filename);
    : return(NULL);
    : } else
    : return(temp);
    : }
    :
    : int main() {
    : FILE *test;
    : test = openfile("test.txt","w");
    : fclose(test);
    : return(0);
    : }
    : [/code]
    :
    : All it does is check if the open was succsessful or not but yeah u will probly want to add your own stuff
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion