I try to read a file with the following pattern:
"a,b,c,d,e,f\n"
"a,b,c,d,e,f\n"
...
a = 0-8 characters should be saved in buffer[8]
b = unsigned long long (0-17 characters, I don't need this later)
c = unsigned long long (0-17 characters, I don't need this later)
d = unsigned char (1 character)
e = unsigned char (2 characters)
f = unsigned char (1 character)
I would like to read line for line and save a in a char buffer[8], d in unsigned char move, e in unsigned char left and f in unsigned char state.
Can that be done with fscanf? I just can't manage to create a matching const char* format for fscanf.
The only other unfortunate thing is that ISO C does not support the hh modifier, so you cannot scan unsigned chars directly. You must scan them in as an unsigned short and convert to char.
(Remember, signed char and unsigned char both represent byte-sized numbers, not ASCII characters.)
[edit] Oh, yeah, you might want to google "scanf scanset specifiers", but the thing I specified is "a string of maximum length eight, composed of any characters except , space tab newline and carriage-return.
Thank you very much. What I was looking for was the not operator for scanf even though I didn't know that there is one.
The correct patter in my case is now: "%[^,], %*u, %*u, %hu, %hu, %*hu\n"
Width isn't even necessary, because there are never more than 8 bytes before the first comma.
Width isn't even necessary, because there are never more than 8 bytes before the first comma.
Width is always necessary. That is one of the reasons that scanf() is so dangerous --omitting the width is a security hole.
Just because input is always supposed to be formatted in a specific way doesn't mean that it always is in actuallity. Don't trust security concerns to those using the program (even if it is yourself).
BTW. You don't need to specify a size modifier (h) if you are using assignment suppression (*).