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

This is a discussion on I'd like to load a text file's contents cleanly at compile time within the C Programming forums, part of the General Programming Boards category; I'm making a simple console app, but I would like to proivde a nice-looking HTML help/tutorial file along with it. ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    2

    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:

    Exporting HTML

    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
    Location
    Out of scope
    Posts
    4,066
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    Mmm - a text file to C code convertion is a few lines of perl
    Code:
    -- The perl program to do the magic
    $ cat bar.pl
    #!/usr/bin/perl -w
    use strict;
    while ( <> ) {
      chomp;s/\r//;
      s/([\\"])/\\$1/g;     # quote all backslashes and "
      print "\"$_\\n\"\n";
    }
    
    -- Example C program
    $ cat bar.c
    #include <stdio.h>
    int main ( ) {
      printf(
    #include "help.txt"
      );
      return 0;
    }
    
    -- The perl program run on itself
    $ bar.pl bar.pl > 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"
    "}\n"
    
    -- compile and test our program
    -- voila, our original perl program!
    $ gcc bar.c
    $ ./a.exe
    #!/usr/bin/perl -w
    use strict;
    while ( <> ) {
      chomp;s/\r//;
      s/([\\"])/\\$1/g;     # quote all backslashes and "
      print "\"$_\\n\"\n";
    }

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    2
    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 Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21