2016-07-09 14 views
2

Ich versuche, ein einfaches Programm zu machen, um eine Konfigurationsdatei in C zu lesen. Aber die Daten in meinen Strukturelementen bleiben nicht dort, wo ich es hinstelle.Pass struct durch Verweis in C, aber Modifikationen sind nicht alle

Code an der Unterseite.

Dies ist, was ich denken ich tue:

  • ich eine Struktur erschaffe, dass
  • durch Verweis die Struktur Passing
  • die Mitglieder der Struktur aus der Konfigurationsdatei Bestücken

Wenn ich versuche, auf die Mitglieder zuzugreifen, kann ich nur die Daten aus der letzten Zeile in der Konfigurationsdatei abrufen.

Danke!


Edit: Ich erwartete, dass es die Struktur mit Schlüssel/Werte aus der Konfigurationsdatei bevölkern. Stattdessen hatte es nur den Wert der letzten Zeile in der Konfigurationsdatei.

erwartete Ausgabe

example.com

tatsächliche Ausgabe

food.txt

Problem ist mit:

conf->key[i] = key; 
conf->value[i] = value; 

main.c

/* 
* Goal is to make simple key value storage with struct and pointers 
*/ 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#define MAXLEN 100 
#define CONFIG_MAX_BUF 1024 
#define DELIM "=" 

struct config_s { 
    char *key[MAXLEN]; 
    char *value[MAXLEN]; 
}; 

void read_config_file(struct config_s *conf, char *filename, int *size_of_array); 

int main() { 
    int i = 0, size_of_array; 
    // type config_s has key and value members 
    struct config_s c; 

    read_config_file(&c, "test.cfg", &size_of_array); 
    printf("size of array: %i\n", size_of_array); 

    // if i want to print them all out, i can use this, but i'll have 
    // a function to search for a certain key and return the value. 
    int l = 0, e = 1; 
    printf("********** TEST 1 ***********\n"); 
    printf("key: [%s] value: [%s] i: [%i]\n", c.key[l], c.value[l], l); 
    printf("key: [%s] value: [%s] i: [%i]\n", c.key[e], c.value[e], e); 
    printf("********** TEST 2 ***********\n"); 

    return 0; 
} 

void read_config_file(struct config_s *conf, char *filename, int *size_of_array) { 
    FILE *file; 
    file = fopen(filename, "r"); 

    char line[CONFIG_MAX_BUF]; 

    int i = 0; 

    char *cfline; 
    char *key; 
    char *value; 

    while(fgets(line, sizeof(line), file) != NULL) { 
     cfline = strstr((char *)line, DELIM); 
     value = cfline + strlen(DELIM); 
     value[strlen(value) - 1] = '\0'; 

     key = strtok((char *)line, DELIM); 
     printf("%i %s %s\n", i, key, value); 
     conf->key[i] = key; 
     conf->value[i] = value; 
     printf("%i %s %s\n", i, (*conf).key[i], (*conf).value[i]); 
     i++; 
    } 
    printf("%s %s\n", (*conf).key[0], (*conf).value[0]); 
    fclose(file); 
    *size_of_array = i; 
    return; 
} 

test.cfg

server=example.com 
port=443 
file=food.txt 
+5

Das ist, weil Sie Zeiger sind zuweisen, keine Strings. – Peter

+2

'Pass struct durch Verweis in C', aber es gibt keine * durch Verweis * Mechanismus in ** C **! C hat nur * übergeben nach Wert * Mechanismus – Cherubim

+1

Sie sind nicht durch Verweis übergeben, weil C das nicht unterstützt. Sie übergeben einen Zeiger auf die Struktur. Die Verwendung der richtigen Terminologie ist wichtig. – Donnie

Antwort