【发布时间】:2011-04-14 01:21:06
【问题描述】:
我需要在 C 程序中读取一个文件,并且我不想硬编码该文件的路径。我想将该路径作为 Make 变量提供,然后在 C prog 中使用它。
file name is xyz.txt and I want to do something like this in C prog:
fopen ("PATH/xyz.txt", "r");
here PATH is specified in make command that compiles this file.
我该怎么做?
【问题讨论】:
-
在编译时通过 make 设置
PATH与硬编码没有什么不同;它仍然是在可执行文件中预定义的。你最好让它接受命令行参数或在运行时从环境变量中读取。 -
@Ken:我相信,它比硬编码更好。该文件的位置因该程序的使用者而异。所以我需要一种不需要修改/更改程序界面的方法。例如,对于 consumer1 - 它的 makefile 设置 PATH = /a/b/c 和对于 comsumer2 - 它的 makefile 设置 PATH = /d/e/f/