malajedala
malajedala

Reputation: 907

Load file content into memory, C

I will be dealing with really huge files, for which I want just partially to load the content into memory. So I was wondering if the command:

 FILE* file=fopen("my/link/file.txt", "r");  

loads the whole file content into memory or it is just a pointer to the content? After I open the file I use fgets() to read the file line by line. And what about fwrite()? Do I need to open and close the file every time I write something so It doesn't overloads or it is managed in the background?

Another thing, is there maybe a nice bash command like "-time" which could tell me the maximal peak memory of my executed program ? I am using OSx.

Upvotes: 2

Views: 5764

Answers (4)

ameyCU
ameyCU

Reputation: 16607

loads the whole file content into memory or it is just a pointer to the content?

No, fopen() open file with the specified filename and associates it with a stream that can be identified by the FILE pointer.

fread() can be used to get file contents into buffer.

Multiple read/write operations can be carried out without any need for opening files number of times. Functions like rewind() and fseek() can be used to change position of cursor in file.

Upvotes: 0

Alon
Alon

Reputation: 2929

fopen does not load all the file into the memory. It create a file descriptor to the file. Like a pointer to the place of the open file table. in the open file table you have a pointer to the location of the file on the disk.

if you want to go to place on the file use fseek.

another Option is to use mmap. This is create new mapping in the virtual address space of the calling process. You can access to the file like an array.. (not all the file load into the memory. it use the memory pages mechanism to load the data)

Upvotes: 2

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

As per the man page for fopen(),

The fopen() function opens the file whose name is the string pointed to by path and associates a stream with it.

So, no, it does not load the content of the file into memory or elsewhere.

To operate on the returned file pointer, as you already know, you need to use fgets() and family.

Also, once you open the file, get a pointer and does not fclose() the same, you can use the pointer any number of time to write into the file (remember to open the file in append more). You don't need to open and close for every read and write made to the pointer.

Also, FWIW, if you want to move the file pointer back and forth, you may feel fseek() can come handy.

Upvotes: 6

Ronny Brendel
Ronny Brendel

Reputation: 4845

fopen does not read the file, fread and fgets and similar functions do.

Personally I've never tried reading and writing a file at the same time. It should work, though. You can use multiple file pointers to the same file.

There is no command like time for memory consumption. The simplest way is to look at top. There exist malloc/new replacement libraries which can do that for you.

Upvotes: 1

Related Questions