FILE pointers C

Started by
7 comments, last by demonkoryu 18 years ago
Hi im trying to get a pointer in c to pass right. I have a function that is stand alone. Im trying to open a file then pass the files address to another file pointer in the main function. So that another function or the main one can write to the file. When I try to compile this I get assignment from incompatible pointer type. Yet both pointers are from the FILE declartion. Here is the section im working on.

#include <stdio.h>

int new_text_file(FILE *text_address)
  {
    FILE *new_text;
    
    
    new_text = fopen("weekly.txt", "w");
    
    if (new_text == NULL)
      {
        printf("Cannot Open Weekly.txt");
        return(1);
      }

    *text_address = &new_text;
    return(0);

  }


int main(void)
  {
    
    char line[10];
    FILE *text_address;

    if(new_text_file(&text_address) == 1)
      {
        printf("Error opening file\n\n");
        exit(2);
      }

    fgets(line, sizeof(line), stdin);
    
    

  }

Do or do not there is no try -yoda
Advertisement
I have not tried it, but I think this will solve your problem. Note that the parameter should be a pointer to a pointer.

#include <stdio.h>int new_text_file(FILE **text_address) /* DOUBLE POINTER */  {    FILE *new_text;            new_text = fopen("weekly.txt", "w");        if (new_text == NULL)      {        printf("Cannot Open Weekly.txt");        return(1);      }    *text_address = &new_text;    return(0);  }int main(void)  {        char line[10];    FILE *text_address;    if(new_text_file(&text_address) == 1)      {        printf("Error opening file\n\n");        exit(2);      }    fgets(line, sizeof(line), stdin);          }


Hope that works for you.
BTW, I'm right across the bridge from ya. [smile]
*text_address = new_text;

#include <stdio.h>int new_text_file(FILE **text_address)  {    FILE *new_text;            new_text = fopen("weekly.txt", "w");        if (new_text == NULL)      {        printf("Cannot Open Weekly.txt");        return(1);      }    *text_address = new_text;    return(0);  }int main(void)  {        char line[10];    FILE *text_address;    if(new_text_file(&text_address) == 1)      {        printf("Error opening file\n\n");        exit(2);      }    fgets(line, sizeof(line), stdin);          }


But where is the fclose()?! Also, I would highly recommend you switch around the 1 and 0 error codes, since 0 automatically goes to false in an if statement, and 1 to true. That way, I can say:
if(!new_text_file(text_address)) {    //error }


The way you have it, what I wrote would be when a file IS created, which doesn't really make much sense...
I am, by no means, an expert on pointers, but it seems to me that since you're passing the address of(&) text_address to the function, when you use the assignment operator, it should be:

&text_address = &new_text;

Again, I'm no expert by any means, but it's just what my (with my unqualified opinion and all) intuition tells me. Hope it works out for you.

-AJ

EDIT: Seems some people already stopped by with, what I assume is the correct answer. I tried [smile]
V/R,-AJThere are 10 kinds of people in the world: Those who understand binary and those who don't...
The address of a local variable is the address of a slot on the stack.

*text_address = &new_text;

There's no guarantee that address will remain valid after the function returns.

You'll need another level of indirection on the new_text_file function. Consider that the code already calls it that way

FILE *text_address;

if(new_text_file(&text_address) == 1)

This is the address of a pointer - or a pointer pointer. However, the function is declared to simply accept a pointer

int new_text_file(FILE *text_address)

I hope you're starting to see the problem here.

One solution is to change the function to accept a pointer pointer

int new_text_file(FILE **text_address)

and then assign the contents of the local variable to the dereferenced argument

*text_address = new_text;



"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Ok when I fixed that in and compiled it I got another error to go away. That one was about assigning pointers between the functions. But the assignment from incompatible pointer type error is still coming up. As if new_text and text_address where not from the same varible type FILE.


Btw thanks for the last bit of info. Much apperciated.
Do or do not there is no try -yoda
Nvm. I got it thanks to you guys.

That was a nice pickle for my first day back to C in some time.

Again thanks for all the help.
Do or do not there is no try -yoda
The most obvious (and easier) solution is for the function to just return the file pointer, or NULL or error. It's a common convention (return a pointer, or NULL on error), easy to work with (FILE* some_file = gimme_a_file()), and removes a redundant way of returning data (the data returned is "a file" or "not a file"). Also, error reporting is best handled by higher-level code (unless you have a more generic way of reporting an error message).

FILE* new_text_file() {  FILE *new_text = fopen( "weekly.txt", "w" );  if( new_text == NULL )    fprintf( stderr, "Could not open file\n" );  // Also handles returning NULL, since that's also what fopen returns  return new_text;}


Isn't that nice and clean?
Quote:Original post by jonahrowley
The most obvious (and easier) solution is for the function to just return the file pointer, or NULL or error. [...]
*** Source Snippet Removed ***

Isn't that nice and clean?


Quoted for truth. [smile]

This topic is closed to new replies.

Advertisement