FILE within a structure

This is a discussion on FILE within a structure within the C Programming forums, part of the General Programming Boards category; I having a problem with the following. Why doesn't this work ? Code: #include <stdio.h> struct myfile { FILE * ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    7

    FILE within a structure

    I having a problem with the following. Why doesn't this work ?

    Code:
    #include <stdio.h>
    
    struct myfile {
      FILE * myPtr;
    };
    
    int main(int argc, char **argv)  {
      struct myfile *file;
      file->myPtr = fopen("myfile.txt","a");
     return 0; 
    }
    when i run this, its segfaults.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,425
    You never allocate space for file. You just make a pointer to it, and that pointer is uninitialized, so file->myPtr is dereferencing invalid/unknown memory. You need to either malloc (and free) some memory, or make an actual struct and access the members with a . instead of a pointer to a struct using the -> operator.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    This will compile...
    Code:
    #include <stdio.h>
    
    struct myfile {
        FILE *myPtr;
    };
    
    int main( int argc, char **argv )
    {
        struct myfile file;
    
        file.myPtr = fopen( "myfile.txt", "a" );
        fclose( file.myPtr );
    
        return 0;
    }
    Or if you really want a pointer to a structure,
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct myfile {
        FILE *myPtr;
    };
    
    int main( int argc, char **argv )
    {
        struct myfile *file;
    
        file = ( struct myfile * )malloc( sizeof( struct myfile ) );
    
        if ( file != NULL ) {
            file->myPtr = fopen( "myfile.txt", "a" );
            /* do something */
            fclose( file->myPtr );
            free( file );
        }
    
        return 0;
    }
    Last edited by kmess; 04-25-2011 at 03:12 PM. Reason: Fixed the added code!.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    7
    sweet, thanx!

  5. #5
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    Glad it helped.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    This time you forgot to allocate memory...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct myfile {
        FILE *myPtr;
    };
    
    int main( int argc, char **argv )
    {
        struct myfile *file;
    
        file = malloc(sizeof( struct myfile ));
    
        if ( file != NULL ) {
            file->myPtr = fopen( "myfile.txt", "a" );
            /* do something */
            fclose( file->myPtr );
            free( file );
      }
        return 0;
    }
    (LOL... stuff happens.)

  7. #7
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    *blush* - I caught it, but evidently not fast enough! I was editing it the same time you were writing about it!

    Kevin

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kmess View Post
    *blush* - I caught it, but evidently not fast enough! I was editing it the same time you were writing about it!

    Kevin
    LOL... Stuff happens!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading structure from file
    By dxfoo in forum C Programming
    Replies: 3
    Last Post: 07-13-2010, 07:48 PM
  2. File -> structure
    By Etdim in forum C Programming
    Replies: 5
    Last Post: 12-20-2007, 03:13 PM
  3. Replies: 9
    Last Post: 05-21-2007, 12:10 AM
  4. Saving structure to a file
    By eth0 in forum C++ Programming
    Replies: 20
    Last Post: 01-07-2004, 03:26 AM
  5. Putting a structure into a file
    By Unregistered in forum C++ Programming
    Replies: 10
    Last Post: 06-25-2002, 04:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21