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
Solved

scanf scan fields

Posted on 1998-08-13
10
334 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

829 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