?
Solved

scanf scan fields

Posted on 1998-08-13
10
Medium Priority
?
339 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:gil_mo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 3

Expert Comment

by:Norbert
ID: 1252107
As I know there is no chance to use scanf for that but here is a little bit code that does the work

char* Extract(char* CompleteString)
{
     char *Start,*Stop,*Result;
     int length;
     Start=strchr(CompleteString.'[');
     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(length);
      strncpy
      for(i=0;i<length;i++)
      {
          result[i]=Start[i];
      }
      return result;
}
0
 

Author Comment

by:gil_mo
ID: 1252108
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.
0
 
LVL 2

Expert Comment

by:bernfarr
ID: 1252109
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Expert Comment

by:bernfarr
ID: 1252110
gil_mo did what I suggest cover it or not?
0
 
LVL 1

Expert Comment

by:newexpert
ID: 1252111
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. */
0
 

Author Comment

by:gil_mo
ID: 1252112
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1252113
sscanf(" abc [[WSA ] ","%*[^[]%[^]]%[]]",sz1,sz2);
printf("%s%s\n",sz1,sz2);
0
 

Author Comment

by:gil_mo
ID: 1252114
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).
0
 
LVL 84

Expert Comment

by:ozo
ID: 1252115
sscanf(" abc [[WSA ]] ","%*[^[]%[^]]%1[]]",sz1,sz2)
0
 
LVL 2

Accepted Solution

by:
bernfarr earned 60 total points
ID: 1252116
gil_mo

Here's that first part again, as an answer. Sorry I didn't have time to experiment with the scanf to give a better answer to the second part.

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 [ ?

Bernard
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question