cfgetcnon-unicode

Strange chars while reading from file with fgetc


When I printf the variable answer it contains several strange chars. What might be the reason?

int flag=0;
char answer[512];
char a[2];
a[1]='\0';

int c;
int status=1;
do {
      c = fgetc(pp);
      if( feof(pp) ) {
         break ;
        }
        if(c=='F' || status==0){
                a[0]=(char)c;
                strcat(answer,a);
                status=0;
        }
   } while(TRUE);

Solution

  • strcat expects the destination string to be valid - which means it must be null terminated. You are not explicitly null terminating the answer string. Add answer[0]=0 before your loop.