Anything similar to Perl's qq or "here document" available for CGIs written in C?

Posted on 2003-10-29
Last Modified: 2010-04-15
I am new to writing CGIs in C, and wonder if there is anything available similar to Perl's qq or "here document" so that I don't have to printf every single line separately and escape all of the double quotes - especially when I have a block of html to output.  I could make every block of html a file and read it in, but that gets messy and I would much rather have a single CGI program.  Is there any nice way to do this in C?  If not, any suggestions for something better than the printf method I described are much appreciated.


print qq{
<font color="red"><b>Isn't there some way to do this in C?</b></font>

print <<endofmultilinetext
<font color="yellow"><b>Or this?</b></font>

Question by:nickswanjan
  • 3
LVL 45

Expert Comment

ID: 9642524

Sorry.  C is still in the dark ages here.

If the contents of the html are static the "copy a file" method really isn't too bad.  Within your program you can just:

  system ("cp HtmlFilePath");

It's pretty easy and straight-forward.

LVL 45

Accepted Solution

Kdo earned 50 total points
ID: 9642683

Then again, a very small C program will convert your source to C source strings.

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

char Buffer[1000];

main ()
  char *bp;

  while (fgets (Buffer, 1000, stdin) != EOF)
    fputs (stdout, "  \"");
    for (bp = Buffer; *bp; bp++)
      switch (*bp)
        case '\n':
          break;   /*  disregard new line chars  */

        case '\\':  /*  Escape problematic characters  */
        case '\'':
        case '\"':
          fputc ('\'', stdout);
          fputc (*bp, stdout);
    fputs (stdout, "\"\n");

File A:

<font color="red"><b>Isn't there some way to do this in C?</b></font>

convert < A > B

  "<font color=\"red\"><b>Isn't there some way to do this in C?</b></font>"

Suitable for including in a C source file.


Author Comment

ID: 9650429
Not as clean as I would like, but the file conversion looks like a decent workaround - I can even make it put in the printf syntax as well.  What is the maximum line length though?  Sometimes individual html lines can get pretty long (I will qq several pages).  Although I suppose I could chop all lines at 75 characters or so using the same translation method...
LVL 45

Expert Comment

ID: 9650604

C doesn't impose line length limits in the C source file or in its I/O.  The only limit that you might encounter is string length.  16-bit systems usually limit strings to 65K.

The file conversion approach can actually be made quite attractive.  Generate each page into its own file as the string returned by the convert program.  Then your source could look like this:

char *HtmlMainPage  =
#include <Htmlmainpage.c>

char *HtmlLoginPage =
#include <Htmlloginpage.c>

Good Luck!

Featured Post

Save on storage to protect fatherhood memories

You're the dad who has everything. This Father's Day, make sure your family memories are protected. My Passport Ultra has automatic backup and password protection to keep your cherished photos and videos safe. With up to 3TB, you have plenty of room to hold the adventures ahead.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

895 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

14 Experts available now in Live!

Get 1:1 Help Now