0

The prolific way of printing documentation for hacky programs is generally:

void print_help(){
    printf("a whole bunch of information
            generally spanning many lines and looking ugly
            requires editing a c file to modify documentation");
}

This is ugly IMO, and doesn't make it easy to modify the documentation. The alternatives:

generally dismissive:

void print_help(){
    printf("read the README, you idiot");
}

error prone, complex:

void print_help(){
    fopen("readme.md", "r");
    //error check;
    while (read from file){
         printf("%s", line);
    }
}

I'd like to bridge the gap between solutions 1 and 3, namely:

void print_help(){
    printf("#include"help_file.txt"");
}

I guess my questions would be:

  • is it really this simple? Does the preprocessor jump over strings, or will it notice the include directive?
  • Potential issues? I know anything that won't printf nicely will cause issues if it's put in the files
Brydon Gibson
  • 1,179
  • 3
  • 11
  • 22
  • 2
    Did you try to compile it? Yes, you can do something *like* this, but not the way you did. – Eugene Sh. Sep 13 '21 at 16:05
  • Preprocessor directives have to be at the beginning of the line (except for whitespace). And they're not processed inside strings. – Barmar Sep 13 '21 at 16:06
  • @EugeneSh. I did try this, and any permutation of escape sequences failed. How would one do something _like_ this? Does the entire printf have to be in the text file? – Brydon Gibson Sep 13 '21 at 16:09
  • Duplicate of https://stackoverflow.com/questions/43256465/include-a-file-as-a-string ? – KamilCuk Sep 13 '21 at 16:11
  • @KamilCuk You're not wrong, however it seems the best answers to this are still in this question – Brydon Gibson Sep 13 '21 at 16:14

2 Answers2

1

Create an include file that defines the documentation as a variable.

help_file.h:

char *help_text = "
a whole bunch of information\n\
generally spanning many lines and looking ugly\n\
requires editing a c file to modify documentation"

program.c:

void print_help(){
    #include "help_file.h"
    printf("%s", help_text);
}

You can use a shell script to create the include file from a plain .txt file.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

The contents of strings are not processed by the preprocessor, so you can't substitute the contents of a file with a string like that.

If you want the help file to be plain text that doesn't look like a series of C strings, your only option is to read the contents of an external file at run time and print it. And I wouldn't exactly call it error prone or complex:

void print_help()
{
    FILE *f = fopen("readme.md", "r");
    if (!f) {
        printf("can't print help file");
    } else {
        char line[500];
        while (fgets(line, sizeof line, f){
          puts(line);
        }
        fclose(f);
    }
}
dbush
  • 205,898
  • 23
  • 218
  • 273
  • What makes it error-prone is finding a reliable filepath for the first argument to `fopen("readme.md", "r");` – rici Sep 13 '21 at 19:25