Solved

How to call a txt file in a CGI script

Posted on 1999-01-29
4
178 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to count occurrences of each item in an array.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now