Member Avatar for Vytautas

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;
}

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

Member Avatar for Vytautas

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

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.

Member Avatar for Vytautas

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.

Member Avatar for Vytautas

*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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.