Home > front end >  Struct in headerfile doesnt contain right data
Struct in headerfile doesnt contain right data

Time:01-10

i have created a headerfile containing a Struct:

functions.h

typedef struct ConfigData
{
    char hostServerName[256];
    unsigned short int portNmbr;
    char gameKind[256];
} config;

extern config configInput;

in the file config.c I am adding data to the struct and I am able to print it correctly.

#include "functions.h"

config configInput;

char* splitString (char *lineInFile){
    char *word = strtok (lineInFile, " ");
    word = strtok (NULL, " ");
    word = strtok (NULL, " ");
    return word;
}

// Function removing spaces from a string
char * removeSpacesFromStr(char *string)
{
    int non_space_count = 0;
 
    for (int i = 0; string[i] != '\0'; i  )
    {
        if (string[i] != ' ')
        {
            string[non_space_count] = string[i];
            non_space_count  ;//non_space_count incremented
        }    
    }
    
    string[non_space_count] = '\0';
 
    return string;
}



void readConfig (char* configFile){
    
    FILE *fPointer= fopen (configFile, "r");
    if (fPointer == NULL){
        perror("ERROR: Couldnt open confg file!\n");
    }

    char bufferIn[256];
    int count = 0; 
        
    while(fgets(bufferIn, 256, fPointer)) {  // eventuell != NULL
        if(strcmp(bufferIn, "") != 0){

            bufferIn[strcspn(bufferIn, "\n")] = 0;

            switch (count) {
                case 0:
                    strcpy(configInput.hostServerName, splitString(bufferIn));
                    break;
                case 1:
                    configInput.portNmbr = atoi(splitString(bufferIn));
                    break;
                case 2:
                    strcpy(configInput.gameKind, splitString(bufferIn));
                    break;
            }
            count  ;
        }   
        
    }

    printf("\n>>>Config File Data<<<\n");
    printf("HostServerName: %s\n", configInput.hostServerName);
    printf("PortNumber: %d\n", configInput.portNmbr);
    printf("GameKind: %s\n\n ", configInput.gameKind);  

}

but when I try to print the data from this struct in the main method, it doesn't work properly. It just prints some random chars

#include "functions.h"


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

    char gamekindname[256]= "NMMorris";
    char *hostname[256] = "sysprak.priv.lab.nm.ifi.lmu.de";
    int portnumber = 1357;

    char* gameID = argv[2];
    char playerNumber[256];
    char configFile[256] = "client.conf" ;

    ...

    //read in Data from config File
    
    readConfig(configFile);
    config configInput;

    strcpy(gamekindname, configInput.gameKind);
    strcpy(hostname, configInput.hostServerName);
    portnumber = configInput.portNmbr;

}

So when I try to access the data of the configInput struct it doesn't show the correct one.

Best Enno :)

CodePudding user response:

For starters the program should not compile at least due to this invalid declaration

char *hostname[256] = "sysprak.priv.lab.nm.ifi.lmu.de";

It seems that instead of the array of pointers you mean a character array

char hostname[256] = "sysprak.priv.lab.nm.ifi.lmu.de";

This statement

if(strcmp(bufferIn, "") != 0){

does not make sense. It seems you mean

if ( bufferIn[0] != '\n' ){

This switch statement within the while loop

        switch (count) {
            case 0:
                strcpy(configInput.hostServerName, splitString(bufferIn));
                break;
            case 1:
                configInput.portNmbr = atoi(splitString(bufferIn));
                break;
            case 2:
                strcpy(configInput.gameKind, splitString(bufferIn));
                break;
        }
        count  ;

also does not make sense. After three iterations of the while loop the variable count will be equal to 3 and the compound statement of the switch statement will be skipped.

The function splitString looks suspecious.

char* splitString (char *lineInFile){
    char *word = strtok (lineInFile, " ");
    word = strtok (NULL, " ");
    word = strtok (NULL, " ");
    return word;
}

There is no check whether word is equal to NULL.

Within main you are using the local variable configInput that is not initialized

config configInput;

strcpy(gamekindname, configInput.gameKind);
strcpy(hostname, configInput.hostServerName);
portnumber = configInput.portNmbr;

Pay attention to that there is no sense to declare character arrays with 256 characters if they store string literals with much less characters as for example

char configFile[256] = "client.conf"; 

It is much better to declare a pointer like

const char *configFile = "client.conf";

Shortly speaking you need to rewrite the whole program anew.

CodePudding user response:

I would suggest to pass a pointer to config in the readConfig()method. Something like this:

config localConfig;
readConfig(configFile, &localConfig);

And the function:

void readConfig (char* configFile, config* ptr_config){

FILE *fPointer= fopen (configFile, "r");
if (fPointer == NULL){
    perror("ERROR: Couldnt open confg file!\n");
}

char bufferIn[256];
int count = 0; 
    
while(fgets(bufferIn, 256, fPointer)) {  // eventuell != NULL
    if(strcmp(bufferIn, "") != 0){

        bufferIn[strcspn(bufferIn, "\n")] = 0;

        switch (count) {
            case 0:
                strcpy(*ptr_config->hostServerName, splitString(bufferIn));
                break;
            case 1:
                *ptr_config->portNmbr = atoi(splitString(bufferIn));
                break;
            case 2:
                strcpy(*ptr_config->gameKind, splitString(bufferIn));
                break;
        }
        count  ;
    }   
    
}

printf("\n>>>Config File Data<<<\n");
printf("HostServerName: %s\n", *ptr_config->hostServerName);
printf("PortNumber: %d\n", *ptr_config->portNmbr);
printf("GameKind: %s\n\n ", *ptr_config->gameKind);  

}
  • Related