Abd al rahman shebani
Abd al rahman shebani

Reputation: 35

When the file is opening in C++ and we start reading , does the whole file load to RAM from hard disk or just a block by a block?

When the file is opening in C++ and we start reading , does the whole file load to RAM from hard disk or just a block by a block ?

Upvotes: 1

Views: 295

Answers (1)

Chase R Lewis
Chase R Lewis

Reputation: 2307

You decide how much to read into RAM. When you do 'getline' or read an amount into a buffer you are moving it from disk to RAM. If the file format is 'streamable' you can work on small chunks of the file at a time and still do the entire operation. Some file formats you need to read in most of the file to do an operation which can be a problem for multiple reasons.

  1. If a file must be completely loaded before processing can begin you cannot start work immediately upon getting the first few bytes. This is a completely sequential operation which is slower than processing & loading at the same time.

  2. As you mentioned RAM is smaller than disk, but even if a file can fit in RAM computers are often multi-tasking these days. Having 1 process need 4 GB of RAM when the same operation can be done with a few kilobytes can cause issues for other processes on the machine.

Upvotes: 3

Related Questions