9

If we write:

//in main

FILE *p = fopen("filename", "anymode");

My question is: to what is p pointing?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
OldSchool
  • 2,123
  • 4
  • 23
  • 45

3 Answers3

16

The file pointer p is pointing a structure handled by the C library that manages I/O functionality for the named file in the given open mode.

You can't tell, a priori, whether what it points at is statically allocated memory or dynamically allocated memory; you don't need to know. You treat it as an opaque pointer.

Note that the standard says:

ISO/IEC 9899:2011 7.21.3 Files

The address of the FILE object used to control a stream may be significant; a copy of a FILE object need not serve in place of the original.

This says (roughly): Don't futz around with the pointer; pass it to the functions that need it and otherwise leave well alone.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    Amongst other things, yes, the structure it points at contains a pointer to the buffer for the file. On Unix, it records the file descriptor that underlies the file stream (probably a file handle of some sort on Windows). It contains information about the current position in the file and in the buffer; it contains information about pushed back characters; it contains all the information that the library needs to handle I/O correctly. – Jonathan Leffler Mar 05 '14 at 01:35
  • is that pointer in structure FILE not points to the opened file or it indirectly points to the opened file through buffer – OldSchool Mar 05 '14 at 01:48
  • 2
    Your question is a bit confusing. There is a structure type `FILE`. What you get back from `fopen()` is a `FILE *`, which points at a `FILE` structure. The structure that is pointed at contains all the information that the library needs to manage I/O to the file. One consequence is that it seldom contains the file name; that isn't needed once the file is open. The `FILE` structure is not the buffer; the buffer is an array of characters. The `FILE` structure contains a pointer to the buffer, either supplied by the system or provided by the user via `setvbuf()` or `setbuf()`. _[...continued...]_ – Jonathan Leffler Mar 05 '14 at 01:56
  • 2
    _[...continuation...]_ The structure contains more than just a pointer to the buffer. I'm not sure of the details for Windows, but the structure contains any information needed at the O/S level to manage the file in a way compliant with the C standard. The phrase 'indirectly points to the opened file through buffer' is wrong; the buffer is the character array where data is held until it is read or written to disk, so that the system doesn't do single-character I/O operations (usually; even an unbuffered `FILE *` avoids single-character operations when it can). – Jonathan Leffler Mar 05 '14 at 02:00
  • thnaks a lot for the help – OldSchool Mar 05 '14 at 02:26
3

p points to a memory location holding a FILE structure. That's all you really need to know, the contents of that structure are entirely implementation-specific.

If it's implemented correctly in terms of encapsulation (i.e., as an opaque structure), you shouldn't even be able to find out what it contains.

All you should be doing with it (unless you're the implementer of the standard library) is receive it from fopen and pass it to other functions requiring a FILE *, such as fwrite, fread and fclose.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

p is a pointer to the stored FILE object from fopen().

It is really a pointer to the stream to the file, through which operations can be performed. However, the actual inner workings of the object are abstracted away.

From http://www.cplusplus.com/reference/cstdio/fopen/ :

fopen

FILE * fopen ( const char * filename, const char * mode );

Opens the file whose name is specified in the parameter filename and associates it with a stream that can be identified in future operations by the FILE pointer returned.

Blue Ice
  • 7,888
  • 6
  • 32
  • 52