PJ wrote On 09/29/05 10:28,:
Hi,
I have a file that contains some data. I want to write a function that
reads the file and return a string(char *) which contains the content
of file.(File can be of any size).
Use fopen() to "connect" an input stream to your file.
Use malloc() to obtain a memory area to hold the
content.
Use getc() to read characters from the stream, one at
a time. As you read each character, store it in the next
byte of your memory area. If you receive more characters
than will fit, use realloc() to enlarge the area.
When getc() says there are no more characters, store
a '\0' to mark the end of the string. Remember that this
terminator takes space just like "real" characters; if
all the "real" characters exactly fill your memory area
you need one more byte for the '\0'.
Use fclose() to "disconnect" the stream from the file.