Thread: Malloc printf with strange characters....

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

    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
    28,413
    The problem is that you immediately try to print j->nome, etc, after using malloc. You did not assign anything, so you get junk.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    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
    948
    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
    159
    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, 08: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-29-2009, 11:46 PM
  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