Link to home
Start Free TrialLog in
Avatar of Vernon012999
Vernon012999

asked on

How to call a txt file in a CGI script

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
ASKER CERTIFIED SOLUTION
Avatar of jconde
jconde

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vernon012999
Vernon012999

ASKER

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?
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
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....