Solved

scanf scan fields

Posted on 1998-08-13
10
333 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL handling single and double quotes 3 96
First character of input string truncated with scanf 3 99
Using ANSI C how to Read a .csv file 10 81
What is atomic operation? 6 59
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

803 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