0

This is the code snippet, it belongs to a bigger project. Basicly it has to store and print how many times a character appears in a block of text. What i dont really seem to understand is how to reallocate the same array....

void subiect2_subpunct_c(char* nume_fisier)
{
    FILE *f1;
    char a;
    int i = 0, j, ok;
    char *caracter, *tempc;
    int *frecventa, *tempf;

    caracter = (char*)malloc(sizeof(char));
    frecventa = (int*)malloc(sizeof(int));

    f1 = fopen(nume_fisier, "r");

    while (a = fgetc(f1))
    {
        ok = 0;

        if (i == 0)
        {
            frecventa[i] = 1;
            caracter[i++] = a;
        }
        else
        {
            for (j = 0; j < i; j++)
            {
                if (caracter[j] == a)
                {
                    frecventa[j]++;
                    j = i;
                    ok = 1;
                }
            }

            if (ok == 0)
            {
                tempc = (char*)realloc(caracter, (i + 1)*sizeof(char));
                tempf = (int*)realloc(frecventa, (i + 1)*sizeof(int));

                caracter = tempc;
                frecventa = tempf;

                frecventa[i] = 1;
                caracter[i++] = a;
            }
        }
    }

    for (j = 0; j < i; j++)
    {
        printf("\n %c (%d)", caracter[j], frecventa[j]);
    }

    fclose(f1);
}
Inisheer
  • 20,376
  • 9
  • 50
  • 82
sorin.va
  • 41
  • 5
  • 1
    Does the code not work? If so, please explain what input you provide, what you expect to happen and what actually happens? Alternatively, if you don't understand part of the code, can you say more clearly what part you'd like to understand? – simonc Jan 22 '14 at 17:30
  • First off, [get rid of the casts, they're harmful](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858). Then, you'll need to construct a minimal example demonstrating the problem. –  Jan 22 '14 at 17:31
  • 1
    Do you `#include ` **and** run the code on a 64bit system? – alk Jan 22 '14 at 17:31
  • Why doesn't the code test whether `realloc()` failed? – alk Jan 22 '14 at 17:32
  • 1
    Please strip away all that's not necessary to understand the problem. Don't bother us with lines that don't matter; and there seem to be many here. – meaning-matters Jan 22 '14 at 17:33
  • @simonc well there is a line of characters in a file like "aaaabbcccc" and the program shoud output a (4) b (2) c (4). The part that doesnt work is the reallocation... – sorin.va Jan 22 '14 at 17:39
  • 1
    @alk I included stdlib but the code is run on 32 bit system. I don't really see the utility of testing everytime i do a realloc because i use the debugger to execute programs that dont work and therefor i check the values everytime and i would observe a 0 pointer. – sorin.va Jan 22 '14 at 17:41
  • Seems that the code enters the realloc function but doesnt modifies anything, weird acting.... – sorin.va Jan 22 '14 at 17:51

1 Answers1

2

The trouble does not come from realloc(). If you read a file and read characters in it, you should check a few facts as shown here http://www.cplusplus.com/reference/cstdio/fgetc/ :

  • The file is successfully opened : test the result of fopen() :

      f1 = fopen(nume_fisier, "r");
      if(f1==NULL)perror ("Error opening file");
    
  • The end of the file has not been reached : test the result of fgetc()

     a=fgetc(f1);
     while(a!=EOF){
        ...
        a=fgetc(f1);
     }
    

Your code failed because a=fgetc(f1) is always true (i don't know why !) : the program never got out of the while loop.

Bye,

Francis

francis
  • 9,525
  • 2
  • 25
  • 41