3

I've always tought of pointers as being RAM adresses, pointing towards bytes of memory that can be random accessed. However, when we create a file in C, we use the pointer FILE*, that points towards the file but, after i close the program, isn't the created file saved in my HD? So, i see two possibilities here:

1) A pointer can points towards a HDD file

2) The file is saved in RAM (that doesn't make much sense to me)

Which one of it is true? Or, if there is a third possibility, what is it?

Thanks in advance.

embedded_dev
  • 195
  • 9
  • 2
    Think of the FILE pointer as a variable that identifies an open file. This pointer doesn't point to some buffer, but it points to some opaque, implementation defined data structure you don't need to care about. Upon success the `fopen` function returns a pointer and then you use that pointer for read or write operations and eventuelly for closing the file. That's basically all. – Jabberwocky Feb 14 '17 at 13:31
  • 2
    `FILE` doesn't mean "the file's contents on disk", it means "the standard library's internal data structure representing a file on disk". These are very different. The structure is in memory, and the pointer points at the structure in memory. The library uses the information in the structure to control access to the on-disk file under the hood. – unwind Feb 14 '17 at 13:34
  • 1
    You really shouldn't think of pointers as addresses in either the virtual address space or RAM for that matter. ["A pointer type describes an object whose value provides a reference to an entity of the referenced type."](http://port70.net/~nsz/c/c11/n1570.html#6.2.5p20) How those references are implemented can vary. Of course depending on how much portability you're willing to sacrifice you can make such assumptions. – Ilja Everilä Feb 14 '17 at 13:39

1 Answers1

5

As mention in How exactly does fopen(), fclose() work

  • When called, fopen allocates a FILE object on the heap. Note that the data in a FILE object is undocumented - FILE is an opaque struct, you can only use pointers-to-FILE from your code.
  • The FILE object gets initialized. For example, something like fillLevel = 0 where fillLevel is the amount of buffered data that hasn't been flushed yet.
  • A call to the filesystem driver (FS driver) opens the file and provides a handle to it, which is put somewhere in the FILE struct.

  • To do this, the FS driver figures out the HDD address corresponding to the requested path, and internally remembers this HDD address, so it can later fulfill calls to fread etc.

  • The FS driver uses a sort of indexing table (stored on the HDD) to figure out the HDD address corresponding to the requested path. This will differ a lot depending on the filesystem type - FAT32, NTFS and so on.

  • The FS driver relies on the HDD driver to perform the actual reads and writes to the HDD.

  • A cache might be allocated in RAM for the file. This way, if the user requests 1 byte to be read, C++ may read a KB just in case, so later reads will be instantaneous.

  • A pointer to the allocated FILE gets returned from fopen.

Community
  • 1
  • 1
Avantika Saini
  • 792
  • 4
  • 9