Read a file into dynamic memory array using malloc and POSIX file operations
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).
Peter
Updated on June 14, 2022Comments
-
Peter about 2 years
Possible Duplicate:
reading a text file into an array in cI'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.
-
Tim Post over 14 yearsThis is as useful as firewire on a commodore 64.
-
Peter over 14 yearsHi Shailesh, could you please advise how to do it using mmap? Thanks
-
Steve Jessop over 14 yearsLook up the documentation for mmap - in effect it gives you an array which contains the bytes of the file (and optionally, modifying the array modifies the file on disk). But I think in the case of mmap, you should read it for yourself rather than just take a code snippet, because it has quite a few options according to exactly how you want it to work.