Thread: equivalent of "print <<<END;"

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    3

    Question equivalent of "print <<<END;"

    I'm trying to put large amount of html into my C/Cpp cgi code
    but found that it's been difficult to do something similar to how php handles large text. Do i have to \" for every occurance of a quote or is there somethign similar in C/C++????

    in PHP you can just do
    print <<<END;
    ....
    ....
    ....
    END

    or in Perl

    print <<__EOF__

    ....
    ...
    .....
    __EOF__;

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And in Python you can do
    Code:
    print """
    ...
    ...
    """
    You can't do anything like that in C, not directly at least. I wrote a program that would take a text file such as this
    Code:
    And he said unto me, "Hello, World!\n"
    
    -- The first AI programmer
    and convert it to this
    Code:
    const char *str = "And he said unto me, \"Hello, World!\\n\"\n"
        "\n"
        "-- The first AI programmer\n";
    You could write a similar program.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int c;
    while( (c = fgetc( fp )) != EOF )
    {
        putchar( c );
    }
    You mean something like that? If you're reading it from a file, just stick it into a string. You don't need to escape it if it is coming in from a file you're reading.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You do need to escape it if you read it from a file and stick the generated output in a C program as a string. It's a little more compilcated than that.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >or is there somethign similar in C/C++????
    Echoing Quzah's idea, it's probably easiest just to create a separate text file in the same directory with your code, read the file using a loop (maybe with fgets), and print it to the screen.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You can also do something like:
    Code:
       printf("What "
       "is "
       "your "
       "favorite "
       "food? "
       "Pizza, Hamburger, Lasagna\n");
    This may not work in the C89 standard.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It does. Concatenating adjacent string literals is a well-defined job of the preprocessor.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    GCC may complain if you have a string literal longer than 509 characters, though. Apparently the C89 standard only requires that compilers supports string literals 509 characters long. (Why they chose 509, I don't know, but I think it's 509 chars + NULL + 4 byte string length = 512 characters (2^9).) 509 sounds like a lot, but it isn't if you have a lot of information. I sometimes have to have several puts() calls in my usage printing functions.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    3

    delimiter overriding? using files defeats the purpose

    Is it possible to change or override the cout/printf quote(") delimiter to something else,
    like __END__ ... more importnalty WITHOUT having ___END__ replace every other occurance of the actual " I intend to use.

    Using a file is pointless and defeats the purpose. the whole point of using this print<<<END; thing is precisely to avoid opening extra files and to stick it all inside one code.

    What do they call this feature in PHP/Perl anyway? "inline code embedding"?
    It'll be nice to have a name for it so I don't have to keep referring to it as "this thing", lol)

    print<<<END;
    ...
    ...
    END

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    An idea would be to put all your HTML text into some external resource file and have the program read that at startup.

    You can store that "raw" as it were, and it has the flexibility of being easy to change without having to edit and recompile the whole code.

    C doesn't have <<<END tricks, get used to it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Using a file is pointless and defeats the purpose.
    If you have a large program, normally you're compiling several files anyway. I can see your point though. You may have several places where you have want to print text to the screen. So that would get messy.

  12. #12
    Registered User
    Join Date
    Aug 2006
    Posts
    3

    C++ Server Pages

    Check this out. You can use C++ as your web coding language, very similar to PhP........

    http://www.micronovae.com/default_csp.html

    looks like this might be an easier way to get my shiznit working the way I want it to.
    Anyone know how popular/feasible C++ Server Pages is?

    I guess not many people use it as PhP or Perl / java...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string equivalent of sprintf()
    By e66n06 in forum C++ Programming
    Replies: 4
    Last Post: 08-16-2007, 03:30 PM
  2. Pointer equivalent to array notation
    By bekkilyn in forum C Programming
    Replies: 4
    Last Post: 12-06-2006, 08:22 PM
  3. C++ Equivalent JPEG Functions?
    By stickman in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2006, 10:50 AM
  4. Replies: 10
    Last Post: 08-17-2005, 11:17 PM
  5. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM