Thread: How does FILE struct work in stdio.h

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    41

    How does FILE struct work in stdio.h

    Hey guys,

    I am trying to understand the file struct in C.
    How does this extend into a fully fledged file with
    formats and the rest.

    All there seems to be is an int that, unless this was a
    flippantly used variable name, remains unused.

    Barring that rather unfounded assumption. I cannot make
    heads or tails of this:

    Code:
    struct
    Code:
    __sFile 
    00047 {
    00048   int unused;
    00049 };
    00050   
    00051 typedef struct __sFILE FILE;
    00052 
    00053 #define __SLBF  0x0001          /* line buffered */
    00054 #define __SNBF  0x0002          /* unbuffered */
    00055 #define __SRD   0x0004          /* OK to read */
    00056 #define __SWR   0x0008          /* OK to write */
    00057         /* RD and WR are never simultaneously asserted */
    00058 #define __SRW   0x0010          /* open for reading & writing */
    00059 #define __SEOF  0x0020          /* found EOF */
    00060 #define __SERR  0x0040          /* found error */
    00061 #define __SMBF  0x0080          /* _buf is from malloc */
    00062 #define __SAPP  0x0100          /* fdopen()ed in append mode - so must  write to end */
    00063 #define __SSTR  0x0200          /* this is an sprintf/snprintf string */
    00064 #define __SOPT  0x0400          /* do fseek() optimisation */
    00065 #define __SNPT  0x0800          /* do not do fseek() optimisation */
    00066 #define __SOFF  0x1000          /* set iff _offset is in fact correct */
    00067 #define __SMOD  0x2000          /* true => fgetline modified _p text */
    00068 #if defined(__CYGWIN__) || defined(__CYGWIN__)
    00069 #define __SCLE        0x4000          /* convert line endings CR/LF <-> NL */
    00070 #endif
    00071
    How does this work??

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps it would help if you look a took at the corresponding implementation of functions like fopen.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    That obviously isn't the FILE struct.
    You haven't said what system you're using or where that code came from.

    EDIT:
    Here's more what you're looking for (in glibc, at least). Remember that FILE is an opague data structure so it's definition will not appear in the stdio.h include file (or anything included by it). I found it in libio.h of glibc-2.25.
    Code:
    struct _IO_FILE {
      int _flags;        /* High-order word is _IO_MAGIC; rest is flags. */
    #define _IO_file_flags _flags
    
      /* The following pointers correspond to the C++ streambuf protocol. */
      /* Note:  Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
      char* _IO_read_ptr;    /* Current read pointer */
      char* _IO_read_end;    /* End of get area. */
      char* _IO_read_base;    /* Start of putback+get area. */
      char* _IO_write_base;    /* Start of put area. */
      char* _IO_write_ptr;    /* Current put pointer. */
      char* _IO_write_end;    /* End of put area. */
      char* _IO_buf_base;    /* Start of reserve area. */
      char* _IO_buf_end;    /* End of reserve area. */
      /* The following fields are used to support backing up and undo. */
      char *_IO_save_base; /* Pointer to start of non-current get area. */
      char *_IO_backup_base;  /* Pointer to first valid character of backup area */
      char *_IO_save_end; /* Pointer to end of non-current get area. */
    
      struct _IO_marker *_markers;
    
      struct _IO_FILE *_chain;
    
      int _fileno;
    #if 0
      int _blksize;
    #else
      int _flags2;
    #endif
      _IO_off_t _old_offset; /* This used to be _offset but it's too small.  */
    
    #define __HAVE_COLUMN /* temporary */
      /* 1+column number of pbase(); 0 is unknown. */
      unsigned short _cur_column;
      signed char _vtable_offset;
      char _shortbuf[1];
    
      /*  char* _save_gptr;  char* _save_egptr; */
    
      _IO_lock_t *_lock;
    };
    Last edited by algorism; 04-23-2017 at 10:29 PM.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    So did this answer your question? Did you find the FILE struct on your system (whatever system that is)? Do you understand what I mean by an "opaque" data structure?

  5. #5
    Registered User
    Join Date
    Sep 2016
    Posts
    41
    Sorry, been on a show last few days.

    So did this answer your question?


    Um, sort of: I now understand what an opaque data type is and why it's used (really frustrating to learn that's a thing... grrrr). Wish I'd known that a long time ago, would have made my digging and deciphering a lot easier; so many times I thought I was just going insane or not 'getting it' when I haven't understood the internals of a given code set. Now I know it's because of opaque data types.

    So headers are for declaration. Where are the definitions? I can't believe I haven't learnt this yet... can't seem to find anything through a google on the topic. Just lots of posts about what should and shouldn't go in header files.

    I did not find the FILE struct in my system. I apologise; I may need some hand-holding in this one. Where would it be located? I am running several different machines, but all my programming is done on Arch Linux. If you would kindly point me in the right direction and I'll have a dig.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The whole point about opaque types is a 'need to know' basis.

    You don't need to know what's hiding inside a FILE structure, because all the things you're allowed to do with it are via an API consisting of fopen / fclose / fread / fwrite / fseek / ftell etc etc.

    It might not even be visible on your system unless you've downloaded the development package for libc for your system. This contains the actual source code for the likes of fopen(), and is compiled into a library called libc.

    When you write your own code calling fopen(), the compiler automatically links with libc, so you don't have to worry about it.

    Even if it is visible, it's location (on the file system) and content (member variables) can both vary. These are irrelevant facts if you just stick to the published API, and just keep a hold of your FILE* variables as required.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2016
    Posts
    41
    Quote Originally Posted by Salem View Post
    The whole point about opaque types is a 'need to know' basis.

    You don't need to know what's hiding inside a FILE structure, because all the things you're allowed to do with it are via an API consisting of fopen / fclose / fread / fwrite / fseek / ftell etc etc.
    Yeh well I need to know. I am a curious cat. I don't care about the API, I know how to use it... I'm not just asking so that I can use this in a code-base, I'm asking because I want to gain an intimate understanding of how files are created and how it all works, under the hood. Isn't this information something that should be readily available, being that C is an open source language?

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by MedicineMan25 View Post
    Yeh well I need to know. I am a curious cat. I don't care about the API, I know how to use it... I'm not just asking so that I can use this in a code-base, I'm asking because I want to gain an intimate understanding of how files are created and how it all works, under the hood. Isn't this information something that should be readily available, being that C is an open source language?
    C is NOT an open source language; C is an open standard language.
    The implementation details are often closed source.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by MedicineMan25 View Post
    Isn't this information something that should be readily available?
    I showed the glibc FILE (_IO_FILE) struct and gave a link to where you can download the source.

  10. #10
    Registered User
    Join Date
    Sep 2016
    Posts
    41
    Quote Originally Posted by algorism View Post
    I showed the glibc FILE (_IO_FILE) struct and gave a link to where you can download the source.
    So you did.. thanks!

    C is NOT an open source language; C is an open standard language.
    The implementation details are often closed source.

    Tim S.


    oh... well that's boring... I'll have to keep digging and see what I find.

    Thanks everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ignoring the stdio.h file in a c file
    By Raj 89 in forum C Programming
    Replies: 4
    Last Post: 11-28-2012, 11:08 AM
  2. "File" undeclared issue/stdio not found
    By drewtoby in forum C Programming
    Replies: 6
    Last Post: 05-15-2011, 10:52 AM
  3. error: stdio.h: No such file or directory
    By MindLess in forum C Programming
    Replies: 9
    Last Post: 12-20-2007, 08:11 AM
  4. conflict in FILE and stdio
    By George2 in forum C Programming
    Replies: 29
    Last Post: 10-12-2007, 02:23 AM
  5. stdio.h file loading/saveing
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 01-26-2002, 02:37 AM

Tags for this Thread