gil_mo
asked on
scanf scan fields
1. What is the correct syntax of the scan field for scanning all characters that are NOT an open-bracket character ('[')? Is there any documentation about how to use the '[' character within the delimiter brackets of scanf?
2. I'd like to scan a line and read into a string, from the first bracket-open char to the first bracket-close (including the brackets). How can this be done with a single sscanf call?
2. I'd like to scan a line and read into a string, from the first bracket-open char to the first bracket-close (including the brackets). How can this be done with a single sscanf call?
ASKER
Norbert, thanks so much for your effort in supplying the code.
However, I am looking for EXACT answers to both (1) and (2), not ANYTHING else.
However, I am looking for EXACT answers to both (1) and (2), not ANYTHING else.
I think this is reasonably well documented in the book:
C: A Reference Manual
Samuel Harbison and Guy Steele
(this is the C Bible as far as I'm concerned)
Given that you can read a single [ character with "%[[]", have you tried "%[^[]" as a way of reading any character that is not a [ ?
I think your second part can be solved by reading three strings and combining them following the scanf call. Something like:
char sz1[8], sz2[300], sz3[8];
char szAll[316];
scanf( "%[[]%[^]]%[]]", sz1, sz2, sz3 );
sprintf( szAll, "%s%s%s", sz1, sz2, sz3 );
The sprintf() is expensive, you could strcpy and strcat the strings instead.
C: A Reference Manual
Samuel Harbison and Guy Steele
(this is the C Bible as far as I'm concerned)
Given that you can read a single [ character with "%[[]", have you tried "%[^[]" as a way of reading any character that is not a [ ?
I think your second part can be solved by reading three strings and combining them following the scanf call. Something like:
char sz1[8], sz2[300], sz3[8];
char szAll[316];
scanf( "%[[]%[^]]%[]]", sz1, sz2, sz3 );
sprintf( szAll, "%s%s%s", sz1, sz2, sz3 );
The sprintf() is expensive, you could strcpy and strcat the strings instead.
gil_mo did what I suggest cover it or not?
How about
#define MAX_CHARS_PER_LINE (number here)
fgets(line, MAX_CHARS_PER_LINE, stdin);
sscanf(line, " %*[[]%[^]]%*[]] ", your_string);
/* in the sscanf string. the first space tells scanf to ignore any white space (\n, space, tab). %*[[] read and discard [.
%[^]] read until ] is reached (not read though). %*[]] read and discard ], and now you have your_string. */
#define MAX_CHARS_PER_LINE (number here)
fgets(line, MAX_CHARS_PER_LINE, stdin);
sscanf(line, " %*[[]%[^]]%*[]] ", your_string);
/* in the sscanf string. the first space tells scanf to ignore any white space (\n, space, tab). %*[[] read and discard [.
%[^]] read until ] is reached (not read though). %*[]] read and discard ], and now you have your_string. */
ASKER
bernfar, newexpert:
Your suggested format string would not work for lines like:
" abc [[WSA ] " . The expected output of such a line is:
"[[WSA ]" (Please read my question carefully!)
I guess that reading once into a string would not be possible.
Your suggested format string would not work for lines like:
" abc [[WSA ] " . The expected output of such a line is:
"[[WSA ]" (Please read my question carefully!)
I guess that reading once into a string would not be possible.
sscanf(" abc [[WSA ] ","%*[^[]%[^]]%[]]",sz1,sz 2);
printf("%s%s\n",sz1,sz2);
printf("%s%s\n",sz1,sz2);
ASKER
ozo, that would scan all closing brackets. I would replace the last format specifier with "%*c" to skip the first closing bracket and then concatenate the sz1 with "]".
bernfarr, since you had answered the question about the syntax, I'd accept this answer again (drop the second part).
bernfarr, since you had answered the question about the syntax, I'd accept this answer again (drop the second part).
sscanf(" abc [[WSA ]] ","%*[^[]%[^]]%1[]]",sz1,s z2)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
char* Extract(char* CompleteString)
{
char *Start,*Stop,*Result;
int length;
Start=strchr(CompleteStrin
if(!Start)
{
printf("no \"[ \" within the string %s",CompleteString);
return NULL;
}
else
{
Stop=strchr(CompleteString
if(!Stop)
{
printf("no \"] \" within the string %s",CompleteString);
return NULL;
}
}
length=Stop-Start;
result=(char*)malloc(lengt
strncpy
for(i=0;i<length;i++)
{
result[i]=Start[i];
}
return result;
}