首页 > 解决方案 > printf 编译时文件(文档)的内容

问题描述

为 hacky 程序打印文档的多产方式通常是:

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

这是丑陋的 IMO,并且不容易修改文档。替代方案:

一般不屑一顾:

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

容易出错,复杂:

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

我想弥合解决方案 1 和 3 之间的差距,即:

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

我想我的问题是:

标签: cc-preprocessor

解决方案


创建一个将文档定义为变量的包含文件。

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);
}

您可以使用 shell 脚本从纯文件创建包含.txt文件。


推荐阅读