Solved

How to call a txt file in a CGI script

Posted on 1999-01-29
4
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

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.
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

729 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