在Linux系统中,可以使用C语言读取INI文件。这可以通过解析文件内容并提取所需的键值对来实现。以下是一个示例代码片段,展示了如何读取INI文件中的键值对:,,``c,#include,#include,#include,,#define MAX_LINE_LENGTH 256,,void read_ini(const char *filename) {, FILE *file = fopen(filename, "r");, if (!file) {, perror("Failed to open file");, return;, },, char line[MAX_LINE_LENGTH];, while (fgets(line, sizeof(line), file)) {, // Remove newline character, line[strcspn(line, "\n")] = '\0';,, // Skip comments and empty lines, if (line[0] == '#' || line[0] == '\0') {, continue;, },, // Parse key=value pairs, char *key = strtok(line, "=");, char *value = strtok(NULL, "=");,, if (key && value) {, printf("Key: %s, Value: %s\n", key, value);, }, },, fclose(file);,},,int main() {, read_ini("config.ini");, return 0;,},
`,,这个程序会打开一个名为
config.ini的文件,逐行读取内容,并打印出每个键值对。请确保你的INI文件格式正确,,,
`,# This is a comment,key1=value1,key2=value2,
``