I'm relatively new to C, so any help understanding what's going on would be awesome!!!
I have a struct called Token
that is as follows:
//Token struct
struct Token {
char type[16];
char value[1024];
};
I am trying to read from a file and append characters read from the file into Token.value
like so:
struct Token newToken;
char ch;
ch = fgetc(file);
strncat(newToken.value, &ch, 1);
THIS WORKS!
My problem is that Token.value
begins with several values I don't understand, preceding the characters that I appended. When I print the result of newToken.value
to the console, I get @�����TheCharactersIWantedToAppend
. I could probably figure out a band-aid solution to retroactively remove or work around these characters, but I'd rather not if I don't have to.
In analyzing the � characters, I see them as (in order from index 1-5): \330, \377, \377, \377, \177
. I read that \377
is a special character for EOF
in C, but also 255 in decimal? Do these values make up a memory address? Am I adding the address to newToken.value
by using &ch
in strncat
? If so, how can I keep them from getting into newToken.value
?
Note: I get a segmentation fault if I use strncat(newToken.value, ch, 1)
instead of strncat(newToken.value, &ch, 1)
(ch vs. &ch).