Welcome to WebmasterWorld Guest from

Forum Moderators: phranque

Message Too Old, No Replies

A C Function clone of PHP's file get contents()

file_get_contents() C

1:42 pm on Nov 15, 2008 (gmt 0)

New User

10+ Year Member

joined:July 25, 2004
posts: 17
votes: 0

I've developing PHP for a very long time now and have only messed with C on occasion. I have therefore got far too used to simple functions such as file_get_contents() to retrieve the HTML / Data stored on an external http server.

I am looking for a simple method ie. preferable a function in C which does the same thing as the PHP function without using a library such as libcurl which needs compiling etc. I'm using gcc to compile and have only half managed to write one which involves -lsocket and -lnsl

Thank you all

PS. Hope this thread is correctly placed in the forums

9:34 am on Dec 21, 2008 (gmt 0)

New User

10+ Year Member

joined:Dec 20, 2008
votes: 0

Maybe I didn't understand properly the question, but why do you use such libraries to create a simple file_get_contents() procedure in C.

You'll create a file handler:

FILE* fp;

And then you will open the file with fopen, read everything from it in the string and close it. Now, how to read everything from it to a string variable? One way is to get the file length and dynamically create a string, holding such ammount of characters, so no overflow may occur. You should use malloc() to create your string.

I haven't touched C in...two years...and I'm sorry if I wasn't helpful enough.