Solved

How to call a txt file in a CGI script

Posted on 1999-01-29
4
203 Views
Last Modified: 2013-12-25
What would be the best way to call a small txt file in a cgi script.  For example I have a header file that I would like to be called because it contains my advertising information.  How could I do that in the following sitution...

----------------
print NEWFILE "<html>\n";
   print NEWFILE "  <head>\n";
   print NEWFILE "    <title>Discussion Group</title>\n";
   print NEWFILE "  </head>\n";
   print NEWFILE "  <body bgcolor=#FFFFFF>\n";
   print NEWFILE "    <center>\n";

----------------

I would like to call the header file right before the body of the page starts?  Any thoughts?

Thanks
0
Comment
Question by:Vernon012999
  • 3
4 Comments
 
LVL 7

Accepted Solution

by:
jconde earned 50 total points
ID: 1832469
Hi, what I do is the following (C Source code)

I created a library that contains the following function:

int Print_Header(char *File)
{
  FILE *f;
  char line[100];
  if ((f=fopen(File,"r")) == NULL)
    return -1;
  while (!feof(f))
    {
       fgets(line,100,f);
       printf ("%s",line);
    }
  fclose (f);
  return 1;
}

Then, in my CGI, all I do is the following:

puts ("Content-Type: text/html\n");
if (Print_Header("My_Header.html") == -1)
  puts ("There was an error printing the header file\n");


As you can see, the source code is pretty simple, so you should have no problems what so ever if you're doing it in perl or any other language.  The good thing is, you can print the header, the footer, and any static html content in your cgi.

Regards,

Jorge del Conde
0
 

Author Comment

by:Vernon012999
ID: 1832470
I'm sorry if this is wasting your time but your answer is a little too complex for me.  I have a very basic understanding of CGI/Perl but I'm not sure exactly what you mean by "creating a library".  Is this a seperate file?  Also where exactly do I place the code in the CGI file?
0
 
LVL 7

Expert Comment

by:jconde
ID: 1832471
No problem, the library is just like a DLL.  You can place functions, compile it once, and then, in your applications, you just need to link to that lib in order to be able to use all of the functions.  You don't need a lib for this, you can paste the Print_Header function directly into your cgi.

Just paste the converted function to perl in your cgi, and call it right after you do the following;

print "Content-Type: text/html\n\n";
if (Print_Header("My_Header.html") == -1)
      print "There was an error printing the header file\n";

I hope its clear now!....

Just to make sure...I'll post the whole thing up again (In c, but can be written in perl easily):

File.c

#include <stdio.h>
#include <stdlib.h>

int Print_Header(char *File)
    {
      FILE *f;
      char line[100];
      if ((f=fopen(File,"r")) == NULL)
        return -1;
      while (!feof(f))
        {
           fgets(line,100,f);
           printf ("%s",line);
        }
      fclose (f);
      return 1;
    }

 void main()   <<<<------------Startup function in C
{
    puts ("Content-Type: text/html\n");
    if (Print_Header("My_Header.html") == -1)
      puts ("There was an error printing the header file\n");

    puts ("Do your stuff here");

    if (Print_Header("My_Footer.html") == -1)
      puts ("There was an error printing the footer file\n");
}

-------------End of File.c-----------

If you need more help, please comment again!

Regards,

Jorge
0
 
LVL 7

Expert Comment

by:jconde
ID: 1832472
Since this is C code, this must be compiled (Unlike perl, since it's an interpreted language) and then placed in the CGI's path....
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question