scanf right square bracket

P

prodziedzic

I want use scanf to read from input everything till right square
bracket ']'.

It seems to be something like that:
scanf("%[^\]]) but that's not working.

Any ideas?
 
V

Victor Bazarov

I want use scanf to read from input everything till right square
bracket ']'.

It seems to be something like that:
scanf("%[^\]]) but that's not working.

Any ideas?

A couple. First, 'comp.lang.c' is a [marginally] better place
to ask about the functionality of C standard library functions.
Second, have you tried using 'getline' and parse the input line
yourself? 'string' has plenty of functionality to locate your ']'
and trim the input. Third, the C standard says that the right
bracket has to be the first in your sequence to be considered in
it, IOW, the sequence has to be "%[^]]" if you want to read any
character up to the bracket.

#include <cstdio>
int main()
{
char c[10] = {}, s[] = "123]321";
int i = sscanf(s, "%[^]]", c);
printf("scanned %d fields, c = <%s>\n", i, c);
}

Should print
scanned 1 fields, c = <123>

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top