Solved

scanf scan fields

Posted on 1998-08-13
10
331 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
  • 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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 20 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now