[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to call a txt file in a CGI script

Posted on 1999-01-29
4
Medium Priority
?
225 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 150 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

656 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