-3

I'm getting this error:

( Stack around the variable 'ptemp' was corrupted. )

My code

list crea_lista(list l, FILE *fp, FILE *fp2){

    char ptemp[11];
    char ptemp2[11];
    element el;
    el.numero_volte=1;

    while(!feof(fp)){

        fscanf(fp,"%s",ptemp);

        while(!feof(fp2)){

            fscanf(fp2,"%s",ptemp2);

            if(strcmp(ptemp,ptemp2)==0){


                el.numero_volte=el.numero_volte+1;


            }

        }

        strcpy(el.parola,ptemp);
        l=insord(l,el);
        el.numero_volte=1;
        fflush(stdin);

        rewind(fp2);

    }

    return l;

}

How can I solve this problem?

Sneftel
  • 40,271
  • 12
  • 71
  • 104
  • 1
    `while(!feof(fp)){` is an antipattern, [don't use it](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – M.M Jun 24 '15 at 15:01
  • you are probably causing buffer overflows; change `%s` to `%10s` and try again. Also, `fflush(stdin)` causes undefined behaviour, and you didn't show how `el.parola` was initialized – M.M Jun 24 '15 at 15:02

1 Answers1

1

You initialized ptemp with memory for 11 characters. If the string you copy from fp is larger than 11 characters, fscanf will write past ptemp into other memory. T fix, either copy only 11 characters or increase the size of ptemp to the maximum size of the strings you are reading (if this is known).

Caleb An
  • 366
  • 1
  • 10
  • @RiccardoPirani If the answer solves your problem, please accept it by clicking the tick mark next to the answer. If you still have any problem, please leave a comment and edit your question if required. – Mohit Jain Jun 28 '15 at 13:22