f=fopen("http:\\\\127.0.0.1\\cgi-bin\\database.txt","a");
There's your problem. I know the backslash/forwardslash thing has been sorted out, but there's more trouble here than that. fopen()'s first argument is supposed to be a filename, not a URL; to open a URL takes a lot more work (with sockets) and isn't what you want to do here anyway.
If the file is in the same directory as the CGI program, open it with:
f = fopen("database.txt", "a");
If you can't open for writing, check to make sure the file (database.txt) isn't set read-only. It might also be worth getting your CGI program to check if its present working directory actually is the program it's in - this isn't always true for CGi scripts, which sometimes run with root (/) as their PWD, in which case you will have to give the full path.
I haven't tested this snippet, but it should work:
#!/usr/bin/perl -wT
print "Content-type: text/plain\015\012\015\012";
print "Document Root: ".$ENV{'DOCUMENT_ROOT'}."\n";
If you put this in your cgi-bin and make sure it's set executable (and possibly that it's saved with a .pl or .cgi extension), it'll tell you your document root. On my development machine, this gives me /var/www/htdocs (it's a Slackware box with Apache).
What I really think you need, though, is a good book on CGI. Did you know that the arguments are sent as environment variables to the script? That POSTed form data can be read from the CGI script's standard input?
You're also best off with a good CGI library. I've never actually done any extensive CGI programming in C so I don't know of one for C. If you're using Perl, the CGI.pm Perl module is great. These libraries help make it easier to access your arguments, receive multipart uploads, and they'll even often help generate HTML code so you don't have to include it in your program, which can be a bit laborious.
Finally, CGI programming is Unix programming. Unix is the operating system of the internet. I don't care what anyone may say about market share; there's a reason why the slashes in URLs face that way. So you might find Eric S. Raymond's "The Art of UNIX Programming" a nice read.
Hope this helps!