Thread: Error when login with valid user!!!

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    159

    Exclamation Error when login with valid user!!!

    Hi....again

    I was testing my program then i realise that i have a bug in my code.

    Well, i have a struct file with nickname and pass

    When i use the fread i see 3 registered users.
    When i want to login, i have only 2, and only one can log (fisrt one)

    If i use the fread() all the values are not read??

    My code is this one

    Code:
    int login()
    {
        struct jogador j;
        int reg=0, op;
    	char usr1[30], pass[30], auxiliar[30];
    	FILE *fp;
    	fp=fopen("registos.dat", "rb+");
    	printf ("\n*=============================================================================*\n");
        printf ("|                       >>>> Login de Jogadores <<<<                       |\n");
        printf ("*=============================================================================*\n");
        printf ("                                                                               \n");
    	printf("\n1- Registar | 2- Registado\n");
        scanf("\n%d", &op);
        getchar();
        if(op==1)
        {
            registo();
        }
        if(op==2)
        {
            reg=fread(&j, sizeof j, 1, fp);
            while(reg==1)
            {
                reg++;
                printf("\n %d players\n", reg);
                printf("\nnickname: \n");
                fgets(auxiliar,29,stdin);
                sscanf(auxiliar,"%[^\n]",usr1);
                fseek(fp, (reg - 1) * sizeof j, SEEK_SET);
                if(strcmp(usr1,j.nick)==0)
                {
                    printf("\npassword: \n");
                    fgets(auxiliar,29,stdin);
                    sscanf(auxiliar,"%[^\n]",pass);
                    if(strcmp(pass,j.pass)==0)
                    {
                        system("cls");
                        printf("\n  1 - Jogo curto (2 perguntas) \n");
                        printf("\n  2 - Jogo medio (4 perguntas) \n");
                        printf("\n  3 - Jogo grande (8 perguntas)\n");
                        printf ("*=============================================================================*\n");
                        scanf("%d", &op);
                        getchar();
                        switch (op)
                        {
                        case 1:
                            gp1_Ques(usr1);
                            break;
                        case 2:
                            gp1_Ques2(usr1);
                            break;
                        case 3:
                            gp1_Ques3(usr1);
                            break;
                        default:
                            printf("\nOpcao invalida\n");
                            return 0;
                        }
                    }
                    else
                    {
                        printf("Password errada");
                        return 0;
                    }
                }
                else
                {
                    printf("Nickname errado");
                    return 0;
                }
            }
        }
        fclose(fp);
    	return 0;
    }
    So, if i have 1 player is ok, if i have 20, only the first one in the list can log...

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    you only read a single record with fread from file 'fp'. later you do an fseek that really doesn't do anything because you don't read anything after that.

    edit: your fread will always return 1 based on the arguments you pass it. so doing the fseek with arithmetic on 'reg' will always give the same number. anyway the fseek doesn't do anything and anyway if you are reading sequentially you don't need to fseek as the fread will position the file at the next record. you do need the fseek if you are doing random access to the file.
    Last edited by dmh2000; 06-20-2012 at 09:31 AM.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Ok Thanks

    I don't have fseek anymore but still not working

    Code:
    int listarJog()
    {
        struct jogador j;
        int retorno, cont = 0;
    	FILE *f;
    	if ((f= fopen("registos.dat", "rb")) == NULL)
    	{
    		printf("Erro ao abrir ficheiro\n");
    	}
    	printf ("\n*=============================================================================*\n");
        printf ("|                       >>>>Visualizacao de jogadores<<<<                      |\n");
        printf ("*=============================================================================*\n");
        printf ("                                                                               \n");
    	retorno = fread(&j, sizeof j, 1, f);
    	printf("\n | NICKNAME        | Password\n");
    	while ( retorno == 1)
    	{
    		cont++;
    		printf("\n  %s                 %s      \n", j.nick, j.pass);
    		printf("\n");
    		retorno= fread(&j, sizeof j, 1, f);
    	}
    	printf(" \n\n %d jogadores registados \n", cont);
    	printf ("*=============================================================================*\n");
    	fclose(f);
    }
    with this function i read all, this one is valid to use strcmp?

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    that looks like it should work, unless your file isn't organized into records that are the exact size of 'struct jogador'.

    edit: are you saying that the function listarjog does print all the names and passwords? if so, i don't understand your question about strcmp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. total login time of each user in last
    By vril in forum Linux Programming
    Replies: 16
    Last Post: 04-20-2012, 05:28 PM
  2. need help with Tracking user login
    By erictu in forum C++ Programming
    Replies: 25
    Last Post: 04-01-2012, 06:57 PM
  3. User login history - bash
    By garcy in forum Linux Programming
    Replies: 3
    Last Post: 05-17-2011, 10:23 AM
  4. Replies: 5
    Last Post: 05-06-2004, 09:14 AM
  5. creating a user login system for web pages
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 07-04-2002, 11:02 PM