You can't use strcpy
with c
, because c
is a single character, and strcpy
expects a null-terminated characters sequence. I am surprised this code even works. You should use your own method to write to the string. For example, you can keep an index i
that stores the next position where you can write.
Sample code derived from yours:
int i = 0;
while (read(tananyag,&c,sizeof(c))){
if (c != ' ') {
if (i < 79) {
str[i] = c;
i++;
}
}
else
{
str[i] = '\0';
printf("%s string length: %zu\n",str,strlen(str));
printf("%s string compared to bla string: %d \n",str, strcmp(str,"bla"));
i = 0;
}
}
I added an important check to avoid buffer overflows. You can't write beyond the size of your buffer. With this code, any excess characters in a huge word will be ignored.
NOTE: As good practice rules demand, you should make that 80
a defined constant.