Read carefully the documentation of fscanf(3).
You might try something like
char str1[80];
char str2[80];
memset (str1, 0, sizeof(str1));
memset (str2, 0, sizeof(str2));
int n3 = 0, n4 = 0;
int pos = -1;
if (scanf ("\\ %79[A-Za-z0-9], %79[A-Za-z0-9], %d, %d /%n",
str1, str2, &n3, &n4, &pos) >= 4
&& pos > 0) {
// be happy with your input
}
else {
// input failure
}
That won't work if you have a wider notion of letters, like French é
or Russian Ы
; both are single letters existing in UTF-8 but represented in several bytes.
I added some spaces (mostly for readability) in the format string (but scanf
is often skipping spaces anyway, e.g. for %d
). If you don't accept spaces -like an input line such as \AB3T, C54x, 234, 65/
, read each line with getline(3) or fgets(3) and parse it manually (perhaps with the help of sscanf
and strtol
...). Notice that %d
is skipping spaces! I also am clearing the variables to get more deterministic behavior. Notice that %n
gives you the amount of read characters (actually, bytes!) and that scanf
returns the number of scanned items.