How can you use scanf in c to read string between brackets? for example how can I get "myString" if text in a file is "text a 123 (myString) text"?
Asked
Active
Viewed 250 times
-1
-
2This question should be asked in a programming site like stackoverflow or [programmers](http://programmers.stackexchange.com) – Albert Dec 31 '14 at 10:15
-
This question appears to be off-topic because it is about programming in C (and really has nothing to do with Unix or Linux). Please consider [so]. – derobert Dec 31 '14 at 10:27
-
Instead of trying to squeeze `scanf()` to the limit of what it can do, and risk creating a bug, consider using a regexp library. – potrzebie Dec 31 '14 at 14:43
4 Answers
1
You can use %[]
as a poor man's regexp :
#include <stdio.h>
int main(){
const char data[] = "text a 123 (myString) text";
char str[20];
int cr = sscanf( data, "%*[^(](%[^)])", str ); /* skip until '(' and read up to ')' */
if (cr == 1) {
printf(">%s<\n", str);
}
else {
printf("NOT FOUND\n");
}
return 0;
}
above program correctly outputs :
>myString<

Serge Ballesta
- 143,923
- 11
- 122
- 252
-
Likely good enough for OP, but this does not insure a final `')'` existed nor the subsequent "text". – chux - Reinstate Monica Dec 31 '14 at 22:47
0
Just to read your line you can have something like
char a[50];
fscanf(stdin,"%*s %*s %*d (%[^)]) %*s",a);

Gopi
- 19,784
- 4
- 24
- 36
-
This answer is wrong. `a` is a char array. The `fscanf()` call should take `a` as argument, not `&a`. – user12205 Dec 31 '14 at 13:27
-
Quoting from [this SO answer](http://stackoverflow.com/a/5407121/3488231), "it's wrong, and it could fail on some platforms". They are pointers to different types. If you compile with gcc, this shows up: `warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[50]’ [-Wformat=]` – user12205 Dec 31 '14 at 13:32
-
@ace Very hard why this might fail and as the answer says who knows on which platform this fails – Gopi Dec 31 '14 at 13:42
-
1I suspect there is an error: `s` after `[` is not a format specifier, but expected next character. – kestasx Dec 31 '14 at 13:54
-
-
-
@Gopi, in Your format string `...(%[^)]s...` character `s` is not a format specifier for string. Format string as You wrote matches string until `)`, then instead of consuming this `)` it expects `s`, and only after it `)`. – kestasx Dec 31 '14 at 14:38
0
Full example:
#include <stdio.h>
int main(){
const char data[] = "text a 123 (myString) text";
char str[20];
sscanf( data, "%*s %*c %*d (%[^)]", str );
puts( str );
return 0;
}
Updated:
You can replace sscanf line with the following (ignore avrytihing until (
and then read until )
:
sscanf( data, "%*[^(](%[^)]", str );
Note: I'm not sure how portable such programming practice is, but it works at least on Ubuntu 12.04.

kestasx
- 1,091
- 8
- 15
0
Example :
#include <stdio.h>
#include <stdlib.h>
int main(){
char mystring[1025];
FILE * fp = NULL;
fp= fopen("dd.txt","r");
if(!fp){
}else{
fscanf(fp, "%*s %*c %*d (%[^)] %*s",mystring);
printf("%s\n",mystring);
}
fclose(fp);
}

Invicnaper
- 21
- 4