Like Tree3Likes
  • 1 Post By anduril462
  • 1 Post By anduril462
  • 1 Post By laserlight

Can I access the members of FILE struct for a file that been opened with fopen()?

This is a discussion on Can I access the members of FILE struct for a file that been opened with fopen()? within the C Programming forums, part of the General Programming Boards category; Greetings EveryOne Can I access the members of FILE struct for a file that been opened with fopen()? After researching ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    61

    Can I access the members of FILE struct for a file that been opened with fopen()?

    Greetings EveryOne

    Can I access the members of FILE struct for a file that been opened with fopen()?

    After researching i found out that the implementation of FILE struct differs from compiler to another, so if it's possible to access this members to retrieve the state of the opened file, is there a portable way to do so?

    Where can I find documentation on FILE's members as I couldn't find any googling it.

    Thank You In Advance
    Knowledge Is Power!

    Don't give me a fish but teach me how to catch one

    I am not asking you to code it for me, what i need is guidance so i can learn

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    No, you can't. The file functions like fopen, fread, fwrite, fprintf, fscanf, fclose, etc form an API. The whole point of an API is that you get a certain, well-defined functionality without having to worry or care about how it's implemented. It's probably one of the most important tenets of good software design.

    Is there something in particular you need to do for which you think you need to access the members of a FILE struct? Maybe we can help you with another way?
    Salem likes this.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Open up stdio.h and take a look at your implementation of it. I don't know why you really need to access members of it though. You should know if it's open, and you should know if it's closed, and beyond that, there are built in functions to do anything else you need.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    61
    Thank you for your replies.

    No, you can't. The file functions like fopen, fread, fwrite, fprintf, fscanf, fclose, etc form an API. The whole point of an API is that you get a certain, well-defined functionality without having to worry or care about how it's implemented. It's probably one of the most important tenets of good software design.
    One thing that I don't like about API, which is shadowing you in a kind of way, what I mean is you lose the total control on the machine (You can't Play "God" with API). But I still agree that API have it's own benefits.

    For several days I have been looking for the source code (implementation) of the C99 standard but without any progress, is it available? from my researching I had the impression that it might be compiler dependent but that doesn't make any sense to me as standard means to be the same regardless of the compiler or the operating system.

    You might ask, why would you need the source code of the C standard. The answer is: because I would like to know and learn how things work in C, so I can come up with solutions that won't be found by asking others, only by learning how things works internally and manipulating things in the low-level.

    All I could find is a book called "THE STANDARD C LIBRARY" by P.J.PLAUGER (Prentice, 1992) as you can see the book is old but as I said that's all I could find.

    In Page 290, you can find the implementation of fgetc:

    Code:
    #include <stdio.h>
    
    int fgetc(FILE *str)
    {
        // get a character from stream;
        if(0<str->_Nback)
        {
            // deliver pushed back char;
            if(--str->_Nback == 0)
            {
                str->_Rend = str->_Rsave;
            }
    
            return (str->_Back[str->_Nback]);
        }
    
        if(str->_Next < str->_Rend)
        {
    
        }
        else
        if(_Frprep(str) <= 0)
        {
            return EOF;
        }
    
        return (*str->_Next++);
    }
    From the code you can see that members of FILE struct are implemented, how can that be from the standard if those members are not the same for all compilers?

    Thanks for any help I can get
    Knowledge Is Power!

    Don't give me a fish but teach me how to catch one

    I am not asking you to code it for me, what i need is guidance so i can learn

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I have or had that book. The book is not about writing standard C. It's about writing your own implementation of the standard libraries.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Quote Originally Posted by Laythe View Post
    One thing that I don't like about API, which is shadowing you in a kind of way, what I mean is you lose the total control on the machine (You can't Play "God" with API). But I still agree that API have it's own benefits.
    Well, you're talking apples and oranges here. The point of an API is to provide a set functionality, with a consistent interface so your code will work regardless of the underlying implementation. It's meant to make coding and maintenance easier, not to give unfettered access to the system at the lowest possible level (though a good API often gives good control). You're looking to get functionality not provided by the standard C library's API. That's not a reason to dislike APIs. It's a reason to like the right tool for the job. If you want total, bare-metal control over your PC, then you have to give up any modern OS and start coding assembly that keeps your processor in kernel mode and out of user mode, but I'm not sure that's what you're really after.

    For several days I have been looking for the source code (implementation) of the C99 standard but without any progress, is it available? from my researching I had the impression that it might be compiler dependent but that doesn't make any sense to me as standard means to be the same regardless of the compiler or the operating system.
    It makes perfect sense if you think about it. The library functions described in the C standard only define an API -- an interface. The behavior is the same, but the implementation may differ. Think of an API as "what you want to do". It's like saying I want you to go pick up milk at the grocery store. I don't care how you get there (walk, bike, bus, car) or which route you take, I just want some milk. I call getMilk(GALLON, WHOLE), and suddenly I have a gallon of whole milk. Same with fopen. I don't care what the FILE structure looks like, or how many if statements you have in the fopen code, I just want you to return a valid FILE * to the opened file, or NULL if there was a problem. It's the behavior of those functions, not their implementation, that is crucial to making sure standard C programs behave the same on all implementations.

    As for the "source code of the C99 standard", there is no such thing, because the standard doesn't describe any implementation, thus there can't be a single, authoritative source code. The standard is a set of rules for an implementation to follow, and a list of features and functions that implementation must provide. It does not care how you get there. Different systems (hardware/architecture, OS, compiler, etc) may require different implementations. What you're really interested in is source code of the C standard libraries, and there still is no single answer for that. One sample implementation, which you can look at since it's open source, is GNU's glibc. It has all the source code for all the standard C library funcitons, as well as some GNU extensions. Be warned though, the code is very complex, and it's chock full of your favorite: layers upon layers of good, abstracted APIs.

    You might ask, why would you need the source code of the C standard. The answer is: because I would like to know and learn how things work in C, so I can come up with solutions that won't be found by asking others, only by learning how things works internally and manipulating things in the low-level.
    Excellent reason, and very ambitious. But by studying one implementation, you will not "learn how things work in C", you will only learn how that implementation works. The standard (get your draft copy here) gives all the details about how standard C works. GNU's implementation will probably differ significantly from that of Pelles C or Microsoft, so learning it wont give you much portable knowledge. Still, I encourage you to look at GNU's implementation. It's fascinating if you can get through it. It wont greatly improve your coding skills, but you might learn a few advanced C tricks and see some examples of good API design. Still, you would do better by practicing programming than reading that code.

    From the code you can see that members of FILE struct are implemented, how can that be from the standard if those members are not the same for all compilers?
    If you read the standard, it doesn't dictate exactly the types and members of a FILE struct. It just says that a FILE struct must be able to provide position indicator, buffer pointer, error indicator and EOF indicator. It doesn't specify the type of each of those, or what the member names should be, or whether there can be other members. Just what features it must provide.
    Laythe likes this.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Laythe View Post
    One thing that I don't like about API, which is shadowing you in a kind of way, what I mean is you lose the total control on the machine (You can't Play "God" with API).
    That's kind of the point... Application Programming Interfaces provide you with sufficient functionality to write end user software *without* exposing the underlying code. They don't tell you what's going on because you aren't supposed to know what's going on.

    For example the Windows API is a collection of some 30,000 function calls. We are given access to the functions and their behavior is documented but the underlying code is guarded as a secret by Microsoft. Just think how bad the virus problem would be if every Tom Dick and Harry could mess with the OS at any depth they chose....

    Last I looked "Programmer" is a lot closer to a synonym for "Poltergeist" than it is to "Angel". Just look how often the first thought through a newbies head is nefarious... We see it here all the time. Some guy not even a month into a programming course is asking about sending files over the internet, trapping I/O activity or accessing system security files... We KNOW what he's thinking, he's learned how to write a do-while() loop and now he's fancying himself a hacker, cracker or some other kind of Big Shot Troublemaker.

    Trust me... you aren't given access beyond a certain depth for very good reasons.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by CommonTater View Post
    For example the Windows API is a collection of some 30,000 function calls. We are given access to the functions and their behavior is documented but the underlying code is guarded as a secret by Microsoft. Just think how bad the virus problem would be if every Tom Dick and Harry could mess with the OS at any depth they chose....
    Access to the OS at greater depths isn't prevented by keeping the system calls source code secret though, it is about not allowing user-space applications to mess with stuff inside kernel-space.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by iceaway View Post
    Access to the OS at greater depths isn't prevented by keeping the system calls source code secret though, it is about not allowing user-space applications to mess with stuff inside kernel-space.
    Yeah secrecy is part of it... Same concept as in C++ ... publishing interfaces but not giving out source for the implementation.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    To continue on a discussion of OS API and security, go to OS API and Security.
    iceaway likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Dec 2010
    Posts
    61
    Thank you so very much Mr. anduril462 for your positive and informative reply.

    Last I looked "Programmer" is a lot closer to a synonym for "Poltergeist" than it is to "Angel". Just look how often the first thought through a newbies head is nefarious... We see it here all the time. Some guy not even a month into a programming course is asking about sending files over the internet, trapping I/O activity or accessing system security files... We KNOW what he's thinking, he's learned how to write a do-while() loop and now he's fancying himself a hacker, cracker or some other kind of Big Shot Troublemaker.
    And thank you Mr. CommonTater for that, it's so encouraging.

    I am not asking anyone to teach me how to break into other people's hardware or protected software for that matter, all i am seeking is knowledge to how things work and how things are designed, and imho that's all into my question.
    Knowledge Is Power!

    Don't give me a fish but teach me how to catch one

    I am not asking you to code it for me, what i need is guidance so i can learn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Delete opened file
    By gidion in forum C Programming
    Replies: 25
    Last Post: 12-04-2008, 09:35 AM
  2. Replies: 7
    Last Post: 06-16-2008, 01:18 PM
  3. Replies: 12
    Last Post: 04-01-2008, 08:58 AM
  4. How to access members of a struct
    By Bnchs in forum C Programming
    Replies: 9
    Last Post: 03-25-2008, 02:28 PM
  5. How can I access a struct (from a header file)?
    By loxslay in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2006, 01:25 PM

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