c - What happens when I fopen a file (RAM and ROM matter) -


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

I have read data from a file and I use classical as always:

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

There is a structure in the FILE indicator that I do not understand very well.

The main question is: When I put a file in RAM or every time I use the file pointer, it reads it from HD is?

OS: Linux Mint, GCC compiler

returned indicator one Black-box is an indicator for the structure. You should not understand the real structure, just how to use the pointer to it.

What's happening, the fopen function dynamically allocates this structure (e .g malloc ) and gives an indicator. The structure is in virtual memory , which can be in RAM or in swap (if you have swap enabled). The indicator itself is a store - sometimes compiler stores variables (local variables on stacks, global variables elsewhere). No matter where the variable is stored, it is stored in virtual memory just like a structure in another place.

Then file-handling operations are by default all file-stream buffed , which means that when you try to read anything from FILE indicator If you do, the function that you call will be read in private buffer, and you will have to return back a portion of that buffer (or if you want to read more than the internal buffer can have multiple buffer copies) this buffer also has virtual memory Is stored in, is unknown.

And at the lower level the operating system also has its own buffer, but it is stored in the memory in the OS private. There may be several buffers in the OS.

As far as all these buffers are stored, one point or another will be in physical RAM, but it is not in your control, and it does not really matter to you. As a program programmer, you create your own buffer, for example ask fill to fill the data with a file, and that's all you really know as a start .

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -