Question about file data structure?
HI, I am reading some C text at the address: https://cs.senecac.on.ca/开发者_如何转开发~btp100/pages/content/files_p.html In the text, they mention about file data structure as the image:
I don't understand what contains in the file data structure and how it connects to the physical file? Can anyone elaborate on that, please? Thanks.
As stated in the comments, you shouldn't need to access the details of the object pointed to by a FILE*
- use fread()
, fgetc()
, etc.
If you want to know what it contains, the definition is usually in stdio.h, but it won't be very informative!
What it does is to provide a buffer for the 'raw file' which is denoted by a file descriptor (just an int, usually) and used with functions like open()
, read()
etc. The internal fields are there to manage the buffer, position in the file, error flags and so on.
If you use fgetc()
to read one character from a file, the library will actually load a block of data from the underlying file descriptor, store it and return one character. The next time you use fgetc()
it takes the next character from its store and doesn't have to access the actual file again. Once all the characters from the store have been returned, the underlying file will be accessed to fetch the next block. This is more efficient if you want to read character by character. The same applies to writing with fputc()
.
It's also worth noting that a file descriptor might not actually be a file on disk, it could be a serial port, pipe or something else.
精彩评论