c - 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"");
}
我想我的问题是:
- 真的这么简单吗?预处理器会跳过字符串,还是会注意到包含指令?
- 潜在问题?我知道如果将其放入文件中,任何不能很好地 printf 的东西都会引起问题
解决方案
创建一个将文档定义为变量的包含文件。
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
文件。
推荐阅读
- python - 适当的 numpy/scipy 函数来插入在单纯形(非规则网格)上定义的函数
- c# - 无法从 BST 中删除节点 - C#
- javascript - 如何将 Route 匹配到除基本(主)路径之外的任何路径?
- arrays - 希望计算公司名称出现在列中的次数,同时排除另一列中的重复值
- c++ - C++/OpenGL 随机分段错误
- java - 进程资源包 maven 错误 calssnotfoundexception org.apache.commons.collections.ExtendedProperties
- javascript - 使用哈希滚动时更改 url
- javascript - 返回按钮返回登录页面而不是角度 4 中的主页
- python - 重叠多边形时地理熊猫的空间连接?
- dataframe - 如何在闪亮的应用程序上将数据框作为数据表项返回