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

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.

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?

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.

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.

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.

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.

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.

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.

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.