0

I have a question regarding file pointers (i.e FILE *). I am having a hard time understanding conceptually what this represents in terms of its memory. When the concept of files was introduced in class, it was said that files are used so that we can store persistent data (i.e. data that will be lost even after closing our function). The way I comprehend that is that the usually when we run functions, we use the RAM to store memory. However, for files we will be storing the file data to the computers hard drive. Is my comprehension correct here?

Regarding file pointers: Are the file pointers storing the address of a FILE in the Hard drive? If so, do hard drives also have memory addressed based on hexadecimals? I am asking this because I just ran the following code:

int main(int argc, char *argv[])
{
FILE *file = fopen(argv[1], "r");
printf("%p", file);
}

and I got the following response printed:

~/ $ ./pset3b card.raw
0x1f4e260~/ $

Last question is an understanding of what a file pointer means in the context of a file that has been opened for reading? for example, in the following code, "file" seems to be storing an address. What is it storing an address of? If I were to deference "file", would I find the argv[1] file?

Thank you.

FILE *file = fopen(argv[1], "r");

1 Answer 1

1

A FILE pointer is a pointer to a struct. That struct contains numerous elements. (You'll learn about structs later, if you haven't already.) This particular struct is defined in stdio.h as follows:

typedef struct 
{
 short level ;
 short token ;
 short bsize ;
 char fd ;
 unsigned flags ;
 unsigned char hold ;
 unsigned char *buffer ;
 unsigned char * curp ;
 unsigned istemp; 
}FILE ;

Among other things, the struct keep track of how to find the file in secondary storage (disk, thumb drive, SSD, etc.), whether the file is open for read, write, append, etc., and the current position in the file.

The pointer actually points to the address of the struct where this data is stored. It does NOT actually point to the file. In your program above, you printed out the contents of the pointer. That's the address in memory where the FILE struct is physically stored.

If you really want to understand how all this works, google the various functions related to file manipulation and search for the source code to study it.

This is just a really basic overview to answer your question. To delve further into this is simply beyond doing here.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .