1

So I have declared a pointer *f of type FILE and now I say that that pointer is equal to fopen("text.txt", "r"). So since a pointer stores an address, is fopen giving back the address of a file?

FILE *f;
f = fopen("text.txt","r");
Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
Tenko
  • 123
  • 5
  • 2
    [C11 7.2.1](https://port70.net/~nsz/c/c11/n1570.html#7.21.1) "The header `` declares the type `FILE` which is an object type capable of recording all the information needed to control a stream, including its file position indicator, a pointer to its associated buffer (if any), an error indicator that records whether a read/write error has occurred, and an end-of-file indicator that records whether the end of the file has been reached" (*I did some small editing*) – pmg Dec 12 '21 at 19:29
  • 1
    Not an address of the file itself, but of some thing that describes the file. It being a pointer doesn't matter though; there is no point in dereferencing it, all you do with it is passing it to standard functions dealing with files. – HolyBlackCat Dec 12 '21 at 19:30
  • 1
    It's giving back the address of an in-memory data structure that contains data related to the file and opening, such as file name, opening mode, current file pointer (relative address in file, etc.). – Mark Lavin Dec 12 '21 at 19:31
  • Some people say that it returns an adress and other not. When I debugg it, the pointer changes its adress, so why it is not an adress ? – Tenko Dec 12 '21 at 19:33
  • It IS an address. However, since the `FILE` type is opaque, you are not meant to examine the contents at that address. – Daniel Walker Dec 12 '21 at 19:34
  • 1
    It doesn't matter if it's an address or not, since you're not meant to dereference it anyway. Since it got a `*`, it's technically a pointer, but for all you know, it may or may not be an integer casted to a pointer (or not). – HolyBlackCat Dec 12 '21 at 19:35
  • you mean that I can not dereference the pointer as the file type is opaque, so it is not possible to make *f isn´t it? – Tenko Dec 12 '21 at 19:35
  • Ah, good point, @HolyBlackCat. I hadn't even considered that possibility. – Daniel Walker Dec 12 '21 at 19:35
  • You can make the pointer. You just can't dereference it. – Daniel Walker Dec 12 '21 at 19:36
  • *"so it is not possible to make *f"* Mhm. – HolyBlackCat Dec 12 '21 at 19:36
  • I think he means that it's not possible to deference `f` via `*f`. – Daniel Walker Dec 12 '21 at 19:37
  • you are right daniel – Tenko Dec 12 '21 at 19:37
  • but why is the file type opaque if there is content inside the file? – Tenko Dec 12 '21 at 19:39
  • It's not pointing to the file in the sense of the contents of the file. It points to a special data structure which contents contextual information which allows you to access the contents through functions like `fopen`, `fwrite`, and `fseek`. – Daniel Walker Dec 12 '21 at 19:42
  • Does this answer your question? [What does a file pointer point to in C?](https://stackoverflow.com/questions/52805607/what-does-a-file-pointer-point-to-in-c) – Daniel Walker Dec 12 '21 at 19:44
  • 1
    so the fopen does not return the adress of a sprecific file, it returns the adress of a file type ? – Tenko Dec 12 '21 at 19:44
  • 2
    *"why is the file type opaque"* Because you're meant to pass it to standard functions rather than dereferencing it. Inability to dereference = opaque. – HolyBlackCat Dec 12 '21 at 19:46

4 Answers4

5

is fopen giving back the adress of a file?

No. There is no such thing as "address of a file".

What fopen returns is a pointer to dynamically allocated opaque structure FILE, which describes how to get the contents of the file. This description is opaque in a sense that it provides no useful info to you. But routines such as fgets(), fread(), etc. know how to use that info to get the actual file contents.

fclose deallocates this structure, so if you have matching fopen and fclose there are no memory leaks (from these functions).

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • what would happen if I don´t close it with fclose? What is the use of fclose? – Tenko Dec 12 '21 at 20:19
  • @Tenko `fclose()` writes any outstanding buffered data to disk (If it's a file opened for writing) and cleans up any resources used by the `FILE` object (Like freeing allocated memory and closing OS-level file descriptors). If you don't use it, you get leaks and potentially data loss. – Shawn Dec 12 '21 at 20:49
2

The function fopen is returning the address of an object of type FILE. According to §7.21.1 ¶2 of the ISO C11 standard, this object must be capable of

recording all the information needed to control a stream, including its file position indicator, a pointer to its associated buffer (if any), an error indicator that records whether a read/write error has occurred, and an end-of-file indicator that records whether the end of the file has been reached;

The exact size and contents of this object may differ from compiler to compiler and is of no interest to the average programmer. All that the average programmer must know is that they must pass the pointer returned by fopen to other I/O functions provided by the C standard library.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
2

The standard does not specify how FILE should be defined. The only thing it says is what result you will get when you pass an object of that type to various functions. This means that this type may be different in various implementations. This is ONE way that is used:

typedef struct _iobuf
{
    char*   _ptr;
    int _cnt;
    char*   _base;
    int _flag;
    int _file;
    int _charbuf;
    int _bufsiz;
    char*   _tmpfname;
} FILE;

It comes from MinGW32.

klutt
  • 30,332
  • 17
  • 55
  • 95
1

Yes, it's giving back the address of a FILE object. Now that type is opaque. That is, you are meant to not know the actual contents of the data structure being referenced and hence must never dereference the pointer (happily, the compiler will not let you get away with this).

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45