In C, how to compare a string obtained from strtok with another string

0

Well, in my case, I'm working on file manipulation, nothing complicated, but I came up with a problem that I can not solve, and because of the lack of definition in the searches, it's difficult to find a similar problem and solved on the internet, then I turn to you again

Without rolling more, in my code, in test, I ask the user to enter a name, so I call the function ReadDate to check if this name is available within the application. If they come in the code, when it arrives in the part to analyze between "token" and "name" nothing happens even being equal values. Of course they are not, I used the debug of CodeBlocks, and in token, instead of being for example only "maria", it is appearing "maria \ n001236", but when it is called printf, it correctly appears "maria" and not "maria \ n001236" are just an example. Why does it happen? What do I do to split this string again? Sorry for errors in editing, because I am without pc, using only the smartphone

users.txt files Obs: I used & as final character for testing for now, because using EOF, '-1', -1, always gave an infinite loop I do not know why. obs2: the contents of the users.txt file and what is inside the keys {

/ * System user registry, type csv (comma-separated command), where we have, user id (for system manipulation) user name

example:

0001, isolate

* /

# 001234, isolate 001235, Maria 001236, joao 001237, silas

& }

CustomerCustomer.c file

#include <stdio.h>
#include <conio.h>


void LerDados(char name[15])
{


    FILE *users;                // cria variável ponteiro para referenciar na 
                                // memoria um tipo de arquivo txt, no caso o
                                // user.txt
    char url[] = { "/sdcard/bd.cafeblue/bd/users.txt" };
    // url q se encontra o arquivo users.txt




    // abrindo o arquivo com tipo de abertura w
    users = fopen(url, "r");

    // testando se o arquivo foi realmente criado
    if (users == NULL)
    {
        printf("Erro na abertura do arquivo!");
        return 1;
    }
    else
    {


        int i = 0;
        char *string;
        int tamVetor = 0;

        string = malloc(sizeof(char **) * tamVetor);
         while ((lido = fgetc(users)) != '&')
        {
            if (lido == '#')
            {

                while ((lido = fgetc(users)) != '&')
                {


                    string[i] = lido;
                    i++;
                    tamVetor++;

                    string = realloc(string, sizeof(char **) * tamVetor);

                }
                break;

            }

        }



        printf("%s", string);



        char *token;


        token = strtok(string, ",");

        while (token != NULL)
        {


            printf("%s\n", token);



            if (strcmp(name, token) == 0)
            {


                printf("Usuario ja cadastradoe!\n");
                break;
            }



            token = strtok(NULL, ",");




        }


    }

    fclose(users);



}




int main()
{

    char user[15];
    printf("Entre com o nome: ");
    scanf("%s", &user);

    LerDados(user);

}
    
asked by anonymous 01.06.2018 / 03:33

0 answers