Hello,

I'm trying to open file using a function in a header file and I think it works but I'm unsure if it's written as it should be. Should I use double pointer inside the FileOpen function like argv is or not? It also works without double pointers and that's why I'm asking this.

int main(int argc, char **argv)
{
    FILE *data, *result;

    FileOpen (&data, argv[1], "r");
    FileOpen (&result, argv[2], "w");
    fclose(data);
    fclose(result);

return 0;
}
int FileOpen (FILE **file, char *filename, char *mode) {
   *file = fopen (filename, mode);
    if (*file == NULL) {
		fprintf(stderr, "File not found: %s\n", filename);
		return FALSE;
    }
    return TRUE;
}

Edited 6 Years Ago by Vytautas: n/a

If you want to find out if this works...write and read some data to you files.

Edited 6 Years Ago by gerard4143: n/a

It works. I think now I know that double pointers aren't needed.

Are you sure about that..Try run the code without double pointers.

Are you sure about that..Try run the code without double pointers.

Well, it works without double pointers... Why can I use a pointer or even a triple pointer to the given string (strings are the one's exluded with " " and chars with ' ', right?) while passing only "blah blah", or even a double pointer to that function... Ok, I'm passing for instance "r" to the function. There I use it as a pointer to char. Then I'm passing argv[x] which should be used as a pointer, not double one. I think that's correct?

Well, it works without double pointers... Why can I use a pointer or even a triple pointer to the given string (strings are the one's exluded with " " and chars with ' ', right?) while passing only "blah blah", or even a double pointer to that function... Ok, I'm passing for instance "r" to the function. There I use it as a pointer to char. Then I'm passing argv[x] which should be used as a pointer, not double one. I think that's correct?

What are you talking about, FILE **file or char *filename.

I think that FileOpen and fclose are not compatible . Is there function pairs FileOpen-FileClose or fopen-fclose. I have at least once meet problem using wrong closing function in C. Databuffer may not be flushed in this case.

This article has been dead for over six months. Start a new discussion instead.