-
Actually, I had to repost.
Code:
int foo, bar;
char string[BUFSIZ], matchme[]="10,4,\"Foo'd up beyond all repair\",h\n";
sscanf(matchme, "%d,%d,%512[^\n]", &foo, &bar, string);
printf("%d %d %s\n", foo, bar, string);
The little asterisk trick doesn't seem to work with brackets, so just use the number that BUFSIZ equals.
EDIT: OK, did it again. :o I mistyped the nul character. Sorry. That really does work, if you don't want to read to a flag character instead.
-
You can use an extra variable to read anything past the last
character then if you don't a return code of 4 there was an error.
I think thats the simplest way to do it.
PHP Code:
#include <stdio.h>
#include <string.h>
main(argc,argv)
int argc;
char *argv[];
{
int num, num2;
char matchme[]="1,5,\"some text\",h\n";
char str[40];
char ch[3], endchars[100];
if (sscanf(matchme, "%d,%d,%[^,],%1[hv]%[^\n]", &num,
&num2, str, ch, endchars ) != 4)
printf("Error reading string 1.\n");
else
printf("%d %d %s %s\n", num, num2, str, ch);
}/*main */
-
> If only there was perl compatible regular expressions included in the standard C.
Do you have some objection to linking with a library which does the same?
http://www.pcre.org/
> 1,5,"some text",h
Is this also an example (comma in the quotes)?
1,5,"maybe this, maybe that",h