Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a file into dynamic memory array using malloc and POSIX file operations [duplicate]

Possible Duplicate:
reading a text file into an array in c

I'm trying to read a file into a dynamic array. Firstly I open the file using open() so I get the file descriptor But then I don't know how can I allocate the memory using malloc to a dynamic array in order to do some data modification in the file from memory.

like image 220
Peter Avatar asked Nov 15 '22 14:11

Peter


1 Answers

open(), lseek() to the end of file, getting the size of file using tell(), and then allocating memory accordingly would work well as suggested above.

Unless there is specific reason to do it, fopen(), fseek(), ftell() is a better idea (portable staying within the C standard library).

This certainly assumes that the file you are opening is small. If you are working with large files, allocating memory for the whole of file may not be a good idea at all.

You may like to consider using memory mapped files for large files. POSIX systems provide mmap() and munmap() functions for mapping files or devices in memory. See mmap man page for more description. Memory mapped files work similar to C arrays though the responsibility of getting actual file data is left to the OS which fetches appropriate pages from disk as and when required while working on the file.

Memory mapped files approach has limitations if the file size is bigger than 32-bit address space. Then you can map only part of a file at a time (on 32-bit machines).

like image 143
Shailesh Kumar Avatar answered Dec 11 '22 08:12

Shailesh Kumar