Click to See Complete Forum and Search --> : File into string


goon12
08-30-2002, 03:33 PM
How would I go about copying a text file's contents into a string, I am able to open the file and show it on the screen using getc() and putc(), but instead of showing on stdout, I would like to "copy" it into a string.


Thanks,
goon12

Rüpel
08-30-2002, 04:37 PM
get the text line by line using fgets()
man fgets
should help you

goon12
08-30-2002, 04:54 PM
I got it a little differently:
f_send = fopen("/etc/file", "r");
while( ( c = getc(file)) != EOF )
{
strncat(t_buf, (const char *)&c, 1);
}



Might not be that efficient, but it works for now.

Thanks,
goon12

l01yuk
09-02-2002, 05:07 AM
What if there are more chars in your file than are available in your array t_buf?

Learn about fgets(), it isn't hard and it works better.