C++ Can You Make a Program Read From a Website Code

C programme is a powerful programming language.number of web interpreter languages and database are built past this C linguistic communication.even unix,Most of Windows and other basic network are driven by the C language.nosotros can't imagine without c language and thank you to Dennis.So,we are going to congenital a uncomplicated website using c program and compile using GCC compiler and execute equally CGI using Apache server.Thanks to Shuvojit Sarkar - programmer for configuring my linux environs successfully.


Requirements:

Apache server 2+ version is most preferable(Apache 2.2.22)
GCC compiler
Text Editor :D

configure Apache server:

Assuming you have already Apache server running or follow below instructions

sudo apt-get install apache2

after installation attempt to open your browser and visit your localhost and it should evidence the message Working!

Yous volition need to utilise the CGI interface of Apache ii webserver. So commencement find out the path for the cgi-bin directory. You lot tin can see it in the /etc/apache2/sites-available file where it mentions:

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

<Directory "/usr/lib/cgi-bin">

AllowOverride None

Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch

Order allow,deny

Allow from all

</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log

Finally it should look similar above.If you dont have simply try to copy it and configure it.

check now http://localhost/cgi-bin/ works without any errors,i mean with error message of Forbidden

Now let us code C language to run in Web browser.
there are two ways to running C programme to run in web browser via apache server.
1)the first way is to directly code the html codes in printf(); function which would exist tough in huge file of html.
ii)the 2d style is to create txt file and using file operation to import the text file and read it using the loop concept and generate html codings.
hope you got the clear idea on this.

Code hands:

create a C file using text editor.

#include <stdio.h>
int main()
{
    printf ("Content-Blazon: text/html\n\n");
    printf ("<html>");
    printf ("<head><title>C-Html sync</title></head>");
    printf ("<body>");

printf ("<img src=\"https://lh5.googleusercontent.com/-kTNnF876PYk/AAAAAAAAAAI/AAAAAAAAAus/QLD7dahdifE/s120-c/photo.jpg \"");

     printf ("This folio is created past a C program.<br />");
    printf ("I love to share my ideas to the world <strong>assuming</potent> and <em>italic</em> text.");
    printf ("</torso> </html>");
return 0;
}

we had created the C file and time to compile now using GCC compiler.

change it to the present working directory for compiling.

$ cd <preferable directory>

compile and it should be successful compilation.

$ gcc file.c

but run your output and meet .

$ ./a.out

have a look at my screenshot for visual reference.

after successful compilation now nosotros are in the stage to create awarding/executable that runs on apache server in the cgi-bin directory.

$ gcc -o <your preferred executable proper noun> <space><c programme file proper name>

eg: $ gcc -o alphabetize file.c

now it'due south time to test your file using cgi-bin directory

$ sudo cp index /usr/lib/cgi-bin

this prompt for root countersign and copy the file to cgi-bin directory using sudo cpy command.

open your browser test your file as

http:/localhost/cgi-bin/index   - awsome awaiting for you!

and then the above method is some difficult to code huge html page.and so, i take decided to try file operation in c programming.

steps:

i have coded the normal <html> page and saved as input.txt file in my own dorectory.

next step is i had made the c program to load the input file and read the file line past line and create output as html tags.

have look at c program coding:

#include <stdio.h>

int master()
{
FILE *ptr_file;
char blog[1000];
printf ("Content-Type: text/html\n\north");
ptr_file =fopen("input.txt","r");
if (!ptr_file)
return one;

while (fgets(blog,1000, ptr_file)!=NULL)
printf("%s",web log);

        fclose(ptr_file);
render 0;
}

the file operation opens the file input.txt in read mode and we have declared ptr_file as pointer and blog character array as thousand.since nosotros are going to read the file line by line and simply executing printf statement ,assuming that i dont have more than k characters in single line.and followed by verification of file existance ans using while loop and condition is end of the line must have a naught graphic symbol or new line character and stopping at that place and thus moves to next line and proceeds till the file is completely read.And we shut the file.

thus simply code your input.txt file with html codings on your way.

my coded input.txt

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ane.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<caput>
<meta http-equiv="Content-Type" content="text/html; charset=utf-eight" />
<title>Shivasurya iniatives</championship>
<style type="text/css">
.nav {
width: 100%;
float: left;
margin: 0 0 3em 0;
padding: 0;
listing-fashion: none;
groundwork-colour: #f2f2f2;
edge-bottom: 1px solid #ccc;
border-top: 1px solid #ccc; }
.nav li {
float: left; }
.nav li a {
brandish: block;
padding: 8px 15px;
text-decoration: none;
font-weight: assuming;
color: #069;
border-right: 1px solid #ccc; }
.nav li a:hover {
color: #c00;
groundwork-color: #fff; }
</manner>

</head>

<body style=" margin:0px;background-colour:#FFE7E7;">
<div id="s_01" mode=" height:120px; width:100%; groundwork-color: #900;">
<img src="http://www.tce.edu/sites/default/files/header.png" style="margin-left:100px;"  />
</div>
<div id="s_02" manner="width:100%;">
<ul class="nav" style="width:100%;"><li class="first leaf"><a href="/" title="" class="active" style="margin-left:110px; ">Home</a></li>
<li ><a href="/almost" championship="">About TCE</a></li>
<li><a href="/Academics" title="">Academics</a></li>
<li><a href="/access" championship="">Admission</a></li>
<li><a href="/departments" title="">Departments</a></li>
<li><a href="/Research" title="">Inquiry</a></li>
<li><a href="/contact" title="">Contact The states</a></li>
</ul>
</div>
<div id="main">
<img src="http://world wide web.tce.edu/sites/default/files/imprint/wrappergreen.jpg" style="width:100%; margin-top:-50px;" />
</div>

<div id="r1" style="height:auto; margin:20px;font-size:eighty%;">
<div id="m0" style="height:30px; width:350px;background-color: #FF5050;line-height: 30px;">&nbsp;&nbsp;&nbsp;<b>About</b></div>
<div id="m1" style="tiptop:100px; width:350px;background-color: #FFADAD;">
<div style="margin-left:10px;">
<div style="pinnacle:33px;line-height: 33px;">
Founder: <b>Karumuttu Thiagarajan Chettiar</b>
</div>
<div style="height:33px;line-top: 33px;">
Located in <b> Thiruparankundram,Madurai</b> </div>
<div fashion="acme:33px;line-pinnacle: 33px;">
Establishment Year: <b>1957 </b></div>
</div>
</div>
</div>
</torso>
</html> <! shivasurya productions !>

and now create the application/executable file using compilation procedure.

gcc -o mainhtml file.c

now copy the both executable and input.txt file to cgi-bin using sudo commands.

$ sudo cp mainhtml input.txt /usr/lib/cgi-bin

verify using $ echo $? must return 0 for success.

at present it's time to cheque the website.become to http://localhost/cgi-bin/mainhtml

my output based on input.txt file :

using c program we could get query and combine database functionality using mysql C API.i will depict in detail in my next mail service.

feel free to share and comment guys!detect out bugs if whatsoever an notify me via comments or contact past s.shivasurya@gmail.com

grissomexpeithe.blogspot.com

Source: https://www.i-visionblog.com/2014/02/creating-website-using-c-programming.html

0 Response to "C++ Can You Make a Program Read From a Website Code"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel