File handling in C

P

PJ

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

PJ
 
M

Mike Wahler

PJ said:
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).

So write it. If you get stuck, post your code and
ask specific questions. That's how to get help here.

If you're looking for someone to write the code for
you, try a 'help wanted' group.

-Mike
 
E

Eric Sosman

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

Gregory Pietsch

PJ said:
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).

PJ

This seems like another question with the same answer. Go get FreeDOS
Edlin (available at either ibiblio or alt.sources) and see how I did
it.

Gregory Pietsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top