tags:

views:

749

answers:

5

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

Hi,

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.

Thanks for the help

A: 

like this:

char *buffer = (char *)malloc(size);
int actual = read(buffer,size,filehandle);

now the bytes are in the buffer

Jeff
This is as useful as firewire on a commodore 64.
Tim Post
+3  A: 

Please search SO before posting questions, I think you can find your answer here:

reading a text file into an array in c

Jacob
Not a duplicate - this question is about file descriptors, whereas that question is about C and so naturally all the answers are in terms of FILE*.
Steve Jessop
A: 

Thanks for the answer. Jacob I was checking the post you mentioned before posting but in the that case fopen is beingh used, so we have a FILE *. In this case I'm using open to read the file so I have an 'int fd'

Jeff, thank you very much, the variable 'size' supposed to be the size of the file? How can I define it? I tried to use ftell, but this function is using a FILE * variable as an input :(

Peter
I see - I didn't know you wanted to use `open` explicitly, I assumed from the title that you just wanted to "Read a file into dynamic memory array using malloc"
Jacob
+1  A: 

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).

Shailesh Kumar
Hi Shailesh, could you please advise how to do it using mmap?Thanks
Peter
Look 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.
Steve Jessop
A: 

Thanks Steve for the asnwers. I really apreciate that, I think I solved the problem.

Thanks

Peter