Question

This question came to my mind because I'm trying to speed up my program.

I read data from a file and as usual I use the classical:

FILE *fp;
fp=fopen("c:\\test.txt", "r");

The FILE pointer contains a structure that I don't understand very well.

The main question is: when I perform fopen I put somehow the file into the RAM, or every time I use the file pointer it reads it from the HD?

OS: Linux Mint, gcc compiler

Was it helpful?

Solution

The returned pointer is a pointer to a black-box structure. You're not supposed to understand the actual structure, just how to use the pointer to it.

As for what's happening, the fopen function dynamically allocates this structure (by e..g malloc) and returns a pointer. The structure is stored in virtual memory, which might be in RAM or in the swap (if you have swap enabled). The pointer itself is store where-ever the compiler stores variables (e.g. local variables on the stack, global variables somewhere else). No matter where the variable is stored, it's stored in virtual memory just like the structure, just in another place.

Then comes the file-handling operations. By default all file-streams are buffered, meaning that when you try to read anything from a FILE pointer, the function you call will read into a private buffer, and return to you a part of that buffer (or multiple buffer copies if you want to read more than the internal buffer can hold). This buffer is also stored in virtual memory, unknown where.

And at the lower levels the operating systems have its own buffering too, but it's stored in memory private to the OS. The OS may even have multiple buffers.

As to where all these buffers are stored, one point or another it will be in physical RAM, but it's not under your control, and it doesn't really matter for you as an application programmer. You create your own buffer, ask e.g. fread to fill it with data from a file, and that's all you really need to know as a beginner.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top