c - 在 C 中的文本文件中记录和替换变量

标签 c variables makefile

我有点卡在我正在做的项目上。我正在尝试编写一个函数,它通过一个文本文件并将任何变量赋值存储在由键(变量名)和数据(变量数据)组成的数据结构中。 Here是这个项目,有兴趣的可以看看。

到目前为止,我能够做的是对文件进行索引并确定有多少行包含等号“=”符号,每次找到这样的行时递增一个变量并使用该变量初始化一个新的构造以保持变量。这是函数的代码,称为 replaceVariables:(这还没有完成)

struct VarMap {
    char data[1000];
    char key[1000];
};

// Use a hash table to store the variables - variablename : definition
void replaceVariables(FILE* spData) {

    // Initialise the counting variable buffer to hold the file line by line
    int varCount = 0;
    char buffer[10000];
    while (fgets(buffer , sizeof(buffer) , spData) != NULL) {
        // Skip commented lines (preceede by hash '#')
        if (buffer[0] == '#') continue;
        for (int i = 0; buffer[i] != '\n' ; i++) {
            if (buffer[i] == '=') {
                // Increment if line with equals sign is found
                varCount++;
                break;
            }
        }
    }
    printf ("varCount has counted %d equals signs.\n\n" , varCount);

    // This will hold the variables
    struct VarMap variables[varCount];
    while (fgets(buffer , sizeof(buffer) , spData) != NULL) {
        if (buffer[0] == '#') continue;
        for (int i = 0; buffer[i] != '\n' ; i++) {
            if (buffer[i] == '=') {
                // Store everything before the first equals sign in the key, and everything after the equals sign as data

                break; // We are not interested if the same line contains anymore equals signs
            }
        }
    }


}

这是上下文的 main 函数:(这里可能需要注意替换变量不是程序的主要目的;完成的程序将实现所提供功能的一小部分通过 make 程序)

int main(int argc, const char * argv[]) {

    char filepath[1000];
    printf("Enter the filepath of the Bakefile or bakefile: ");
    scanf("%s" , filepath);
    FILE* spData = fopen(filepath , "r");
    if (spData == NULL) {
        printf ("Cannot open file.");
        exit(EXIT_FAILURE);
    }
    replaceVariables(spData);

    fclose(spData);
    return 0;
}

我的问题是:

如何在我创建的结构中存储变量名和数据(用等号线表示)?

存储后,我应该如何遍历文件并用它们的定义替换所有变量(在变量前用美元符号“$”表示,变量两边用括号表示,如 $(VARIABLE) )?

最佳答案

要回答问题的第一部分(我不确定第二部分),只需对您的代码稍作修改。替换:

   // This will hold the variables
    struct VarMap variables[varCount];
    while (fgets(buffer , sizeof(buffer) , spData) != NULL) {
...

与:

   // This will hold the variables
    struct VarMap variables[varCount];
    int j=0;
    rewind(spData);
    while (fgets(buffer , sizeof(buffer) , spData) != NULL) {
        if (buffer[0] == '#') continue;
        char *p=strstr(buffer,"=");
        if(p){
            *p++=0;
             strncpy(variables[j].key, buffer,1000);
             strncpy(variables[j].data, p,1000);
             j++;
        }
    }

更新: 为了测试,我在最后一个循环之后又添加了 2 行:

for(int i=0; i<varCount;i++)
        printf("k: %s, v: %s\n",variables[i].key,variables[i].data);

./a.out

输入Bakefile或bakefile的文件路径:Makefile

varCount 已经数到 11 个等号。

k: MK_ARCH_DIR ?, v: ../

k: CC, v: 海湾合作委员会

k: LDFLAGS, v: -Wl,-rpath,/usr/local/lib -L/usr/local/lib -lNBiometrics -lNCore -lNLicensing

k: CXX, v: g++

k: CFLAGS +, v: -Wall -I/home/xxx/work/megamatcher/Include

等...

关于c - 在 C 中的文本文件中记录和替换变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52491340/

相关文章:

c++ - 如何从 C :\Windows\System32 folder? 执行 Windows System() 命令

c# - 使用 C# (Windows 8) 产生问题的静态变量

makefile - 模式规则取决于另一个模式规则

c - 仅获取 C 中数组的所有其他值

c - ARM 处理器是否可行?

c - 以指数方式更新并打印一个数字

excel - 如何创建 ActiveX 控件 CommandButton 并将其设置为变量 Excel VBA

variables - TFS 2015 构建变量可以访问其他构建变量吗?

python - 在cygwin中安装python和make

ubuntu - 无法安装 Linux* open-iSCSI 发起程序