Malloc printf with strange characters....

This is a discussion on Malloc printf with strange characters.... within the C Programming forums, part of the General Programming Boards category; Hi (again) Hope this is my last post asking some help... I have a function to see my registered players ...

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

    Malloc printf with strange characters....

    Hi (again)

    Hope this is my last post asking some help...

    I have a function to see my registered players but i must use malloc

    Code:
    int listarTop()
    {
        struct jogador *j;
        int retorno, cont = 0;
    	FILE *f;
    	if ((f= fopen("registos.dat", "rb")) == NULL)
    	{
    		printf("Erro ao abrir ficheiro\n");
    	}
    	else
    	{
    	    j=(struct jogador *)malloc(sizeof(struct jogador));
    	    if (j==NULL)
    		{
    			printf("Erro na alocacao de memoria!");
    			exit (1);
    		}
    	    printf("\nNome: %s",j->nome);
    		printf("\nNickname : %s",j->nick);
    		printf("\nPassword : %s",j->pass);
    		printf("\nLog : %d",j->log);
    		printf("\n");
    	}
    	free(j);
    	fclose(f);
    	return 0;
    }
    my struct is
    char nome[100];
    char nick[100];
    char pass[100];
    int log;


    Well, i have no errors but the prints are strange characters

    What's wrong?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    The problem is that you immediately try to print j->nome, etc, after using malloc. You did not assign anything, so you get junk.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    154
    I think it's not necessary to use malloc since the assisgment don't specify it's must be.

    So i use a regular fread file

    Code:
    int listarTop(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 perguntas<<<<                      |\n");
        printf ("*=============================================================================*\n");
        printf ("                                                                               \n");
    	retorno = fread(j, sizeof *j, 1, f);
    	while ( retorno == 1)
    	{
    		cont++;
    		printf("\nNome: %s",j->nome);
    		printf("\nNickname: %s",j->nick);
    		printf("\nPassword: %s",j->pass);
    		printf("\nLog : %d",j->log);
    		printf("\n");
    		retorno= fread(j, sizeof *j, 1, f);
    	}
    	printf(" \n\n %d perguntas registadas \n", cont);
    	printf ("*=============================================================================*\n");
    	fclose(f);
    }
    But i have a seg fault here.

    retorno = fread(j, sizeof *j, 1, f);

    The file exist since the error is after, and i use a function login where i put my user and pass and i can access.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    How is listarTop called? Are you passing a valid pointer to a struct jogador? Post the code that calls listarTop.

    Also, your loop reads all data from the file into a single structure, so you will end up with only the last data that is read from the file.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    FILE *f;
    if ((f= fopen("registos.dat", "rb")) == NULL)
    {
        printf("Erro ao abrir ficheiro\n");
    }
    ... rest of your code
    You check if fopen() was succesful (which is good) but you only print an error message and then continue with the program. So this check is rather useless.

    The file exist since the error is after, and i use a function login where i put my user and pass and i can access.
    As you try to read from the file regardless if fopen() was succesful or not, you can't be sure, that f is a valid file pointer

    BTW: You declare listarTop() as returning an int, but you never return anything.

    Bye, Andreas

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    154
    Hi

    The listarTop is called like this

    Code:
    listarTop(&j)
    I just found out what was the problem, AndiPersti you gonna kill me with all reasons, arguments where in different order.
    I'm so stupid...

    Now, i can read the file, everything is ok but i have those fields

    name
    nickname
    pass
    log

    If after a game i just want to update the log field, i just have to write

    Code:
    fwrite(&log, sizeof *j, 1,f)

    Thanks people

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange problem with printf !!!
    By astroboy2000ir in forum C Programming
    Replies: 22
    Last Post: 06-28-2011, 09:54 PM
  2. Strange printf
    By brack in forum C Programming
    Replies: 4
    Last Post: 02-13-2011, 08:37 AM
  3. Strange behaviour of printf
    By pshirishreddy in forum C Programming
    Replies: 5
    Last Post: 08-30-2009, 12:46 AM
  4. Strange printf function
    By yougene in forum C Programming
    Replies: 4
    Last Post: 03-04-2008, 10:24 AM
  5. Printf... behaving Strange...
    By vsriharsha in forum C Programming
    Replies: 3
    Last Post: 04-02-2002, 02:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21