By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,988 Members | 2,232 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,988 IT Pros & Developers. It's quick & easy.

CGI C and HTML

P: n/a
Merry Christmas to You all.
My system: WIN XP, DEV CPP 4.x(C compiler).

Consider this HTML code.

<HTML>
<BODY>

<FORM NAME="MyForm" ACTION="c:\PATH\MyProgram.exe" METHOD="GET"
ENCTYPE="application/x-www-form-urlencoded">

<INPUT TYPE="TEXT" NAME="MyString">
<INPUT TYPE="SUBMIT" NAME="Iaccept" VALUE="OK">
</FORM>
</BODY>
</HTML>
This code should have to produce a string("MyForm") and on SUBMIT,
this should be sent to MyProgram.exe to be processed.

Consider this C code.

#include <stdio.h>

void main(int argc, char *argv[]){

/* Your code */
char *MyForm;
FILE *filePtr;
filePtr=fopen("c:\\PATH\\MyText.txt","w");
fprintf(filePtr,"%s",MyForm);
return NULL;

}

The above C code should have to capture the HTML contents and to put
it into MyText.txt.
Does't interest to tokenize and to unescape the string.
I need to understand the mechanism of CGI C(pure ANSI C, better
unstructured).
Particularly, how could I capture by a C program a string comes from
another program.
For example, on command line by argv[], I can pass a string to the
program; unfortunately this procedure doesn't run between two
programs. Better I'm not able to exchange a string between programs,
without using any disk, only from RAM.

Greetings from Italy

Sergio
Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Sergio wrote:
Consider this C code.

#include <stdio.h>

void main(int argc, char *argv[]){ ^^^^
This is not C code/

/* Your code */
char *MyForm;
FILE *filePtr;
filePtr=fopen("c:\\PATH\\MyText.txt","w");
Not checking the return from fopen() is poor form.
fprintf(filePtr,"%s",MyForm);
Trying to use the wild pointer MyForm is insane.
return NULL;
Returning a pointer is just stupid.
}

The above C code should have to capture the HTML contents and to put
it into MyText.txt.


No, it should eat shit and die.


--
Martin Ambuhl

Nov 14 '05 #2

P: n/a
Sergio wrote:
I need to understand the mechanism of CGI C(pure ANSI C, better
unstructured).


It's quite easy to do CGI in standard C. I suggest that you get hold of a
good book on CGI.

Hints: you will need to use getenv to find out the value of the
"REQUEST_METHOD" environment variable. If it's "GET", then you'll need to
call getenv again to find out the value of the "QUERY_STRING" environment
variable (which contains your data). Otherwise, if it's "POST", you'll need
to use getenv() to query the value of the "CONTENT_LENGTH" environment
variable, which tells you how much data you can expect to find on stdin.

Easy, huh?

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #3

P: n/a

"Sergio" <em*****@libero.it> wrote in message
news:6d**************************@posting.google.c om...
Merry Christmas to You all.
My system: WIN XP, DEV CPP 4.x(C compiler).

Consider this HTML code.

<HTML>
<BODY>

<FORM NAME="MyForm" ACTION="c:\PATH\MyProgram.exe" METHOD="GET"
ENCTYPE="application/x-www-form-urlencoded">

<INPUT TYPE="TEXT" NAME="MyString">
<INPUT TYPE="SUBMIT" NAME="Iaccept" VALUE="OK">
</FORM>
</BODY>
</HTML>
This code should have to produce a string("MyForm") and on SUBMIT,
this should be sent to MyProgram.exe to be processed.

Consider this C code.

#include <stdio.h>

void main(int argc, char *argv[]){

/* Your code */
char *MyForm;
FILE *filePtr;
filePtr=fopen("c:\\PATH\\MyText.txt","w");
fprintf(filePtr,"%s",MyForm);
return NULL;

}

The above C code should have to capture the HTML contents and to put
it into MyText.txt.
Does't interest to tokenize and to unescape the string.
I need to understand the mechanism of CGI C(pure ANSI C, better
unstructured).
Particularly, how could I capture by a C program a string comes from
another program.
For example, on command line by argv[], I can pass a string to the
program; unfortunately this procedure doesn't run between two
programs. Better I'm not able to exchange a string between programs,
without using any disk, only from RAM.

Greetings from Italy

Sergio


To pass data between two programs you can read and write to stdin and
stdout. So if you have
prog1, prog2 and prog3 that all read from stdin and write to stdout then on
the command line you can
do something like:

prog1 | prog2 | prog3

Sean

Nov 14 '05 #4

P: n/a
em*****@libero.it (Sergio) writes:
Merry Christmas to You all.
My system: WIN XP, DEV CPP 4.x(C compiler).

Consider this HTML code.
[...]

Sorry, no, I don't think I will.
Consider this C code.

#include <stdio.h>

void main(int argc, char *argv[]){

/* Your code */
char *MyForm;
FILE *filePtr;
filePtr=fopen("c:\\PATH\\MyText.txt","w");
fprintf(filePtr,"%s",MyForm);
return NULL;

}


Ok.

I doubt that the above code would compile (since you try to return a
value from a void function). If you're going to post C code here,
please make sure it compiles and cut-and-paste it exactly; don't try
to paraphrase, or we'll get stuck on any errors you introduce by
paraphrasing it.

main() returns int, not void.

You fail to check the result of fopen(). What happens if it fails?

MyForm is never initialized, so you're passing garbage to fprintf().

Returning NULL from main makes no sense. The values you can portably
return from main() (assuming you declare it properly to return int)
are 0, EXIT_SUCCESS (both indicating success) and EXIT_FAILURE
(indicating failure). EXIT_SUCCESS and EXIT_FAILURE are macros
defined in <stdlib.h>. (Other values can be used, but not in portable
code; for example, the commonly used "exit(1);" indicates failure on
Unix, but success on VMS.) The NULL macro expands to a null pointer
constant; in some cases, the compiler may fail to warn you if you use
"return NULL;" in main(), but it's still incorrect.

Any questions about CGI and HTML belong in another newsgroup. If you
have questions about C, such as how to read from standard input or how
to obtain the values of environment variables, they're appropriate
here, but first consult your C textbook, your online documentation,
and the C FAQ at <http://www.eskimo.com/~scs/C-faq/top.html>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.