Get specific value from a file

Asked

Viewed 24 times

0

Hello, how are you?

I have the following file format:

username=teste1;
status=ONLINE;
username=teste2;
status=ONLINE;
username=teste3;
status=OFFLINE;

Where one of the lines represents the user name and the bottom line the user status just above.

I’d like to search for a specific username and show its status, but I can’t think what the problem is behind this code.

For now I have the following code:

int isUserOnline(char *username) {

    FILE *f;

    f = fopen(FILE_NAME,"r");
    int i = 0;

    char line[MAX] = {'\0'};
    char destiny[MAX] = {'\0'};
    while(!feof(f)) {

        bzero(destiny, sizeof(destiny));

        fgets(line, sizeof(line), f);
        valueAfterEquals(destiny, line);

        if(strcmp(destiny, username)) {
            bzero(destiny, sizeof(destiny));
            fgets(line, sizeof(line), f);
            valueAfterEquals(destiny, line);
            printf("is: %s \n", destiny);
            if(strcmp(destiny, "OFFLINE")) {
                return 1;
            }
            else 
                return 0;
        }
    }
}

This above should fetch the username and check if it exists, if it is offline, returns a;

The function valueAfterEquals is is, simply takes the value after equals and concatenates:

void valueAfterEquals(char * destiny, char * buffer){

    int k = 0;
    while(buffer[k] != '='){ 
        k++;
    }

    int i = 0;
    k++; //pular o '='
    while(buffer[k] != ';'){
        destiny[i] = buffer[k]; 
        k++;
        i++;
    }
}

Thank you so much for your help!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.