Thread: I'd like to load a text file's contents cleanly at compile time

  1. #1
    Registered User
    Join Date
    Jan 2006

    I'd like to load a text file's contents cleanly at compile time

    I'm making a simple console app, but I would like to proivde a nice-looking HTML help/tutorial file along with it. What I would like is that the user can specify a -h command line argument or something, and my program will write a "help.htm" file to the current directory, which is of course the aforementioned pretty-looking help file. It seems like I found someone with a similar problem:

    but I would hate to parse it manually like that and insert it into the program; that looks pretty dirty. It would be great if there's some mysterious concoction of preprocessing directives that gets me what I want. Or.. anything. Some kind of weird include/stringify mix? Any suggestions? Thanks

    PS ooh and how about a binary file? Like if I wanted to write a jpg file also? That would be gravy on top of gravy, if anyone knows a way to do that.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Out of scope
    Hmm... I'm not sure your magic concoction exists. It seems like you're saying: "I'd like a help file, but I don't want to write it". Which isn't possible.

    If you don't want it in the program, like the post you provided showed, then consider perhaps having you -h argument call a command to download the file from a server or even just open a web page. Other than that, it seems to me you'd have to just output the HTML as text to make your help file.

    Sorry if this wasn't the answer you're looking for.
    Sent from my iPadŽ

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Mmm - a text file to C code convertion is a few lines of perl
    -- The perl program to do the magic
    $ cat
    #!/usr/bin/perl -w
    use strict;
    while ( <> ) {
      s/([\\"])/\\$1/g;     # quote all backslashes and "
      print "\"$_\\n\"\n";
    -- Example C program
    $ cat bar.c
    #include <stdio.h>
    int main ( ) {
    #include "help.txt"
      return 0;
    -- The perl program run on itself
    $ > help.txt
    -- The file it produced, which is the C-string equivalent
    $ cat help.txt
    "#!/usr/bin/perl -w\n"
    "use strict;\n"
    "while ( <> ) {\n"
    "  chomp;s/\\r//;\n"
    "  s/([\\\\\"])/\\\\$1/g;     # quote all backslashes and \"\n"
    "  print \"\\\"$_\\\\n\\\"\\n\";\n"
    -- compile and test our program
    -- voila, our original perl program!
    $ gcc bar.c
    $ ./a.exe
    #!/usr/bin/perl -w
    use strict;
    while ( <> ) {
      s/([\\"])/\\$1/g;     # quote all backslashes and "
      print "\"$_\\n\"\n";

  4. #4
    Registered User
    Join Date
    Jan 2006
    Salem got the right idea; thanks. I wasn't saying that I didn't want to write the help file. I want to author the help file first in Wordpad (my usual HTML editing tool) and then have the "help.htm" file complete. Then I want my C program to write that "help.htm" file to the current directory upon request (-h flag), having stored all the text internally (I suppose even as a literal, as in Salem's code).

    I don't know Perl, but I'm very familiar with PHP, so I can implement the same kind of thing in PHP. I should've thought of something like that before, but I guess I was hoping that some magicks native to the C language/preprocessor exist that did what I want. This looks like it'll work fine, though. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. reading a char at a time from text
    By dudeomanodude in forum C++ Programming
    Replies: 7
    Last Post: 01-29-2008, 11:27 AM
  3. Help with loading files into rich text box
    By blueparukia in forum C# Programming
    Replies: 3
    Last Post: 10-19-2007, 12:59 AM
  4. Reading text files on the internet
    By Mavix in forum C# Programming
    Replies: 8
    Last Post: 06-20-2007, 05:38 AM
  5. I apologize. Good bye.
    By doubleanti in forum A Brief History of
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM
Website Security Test