Solved

scanf scan fields

Posted on 1998-08-13
10
332 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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

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

Do email signature updates give you a headache?

Do you feel like you are constantly making changes to email signatures? Are the images not formatting how you want them to? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Statment to match two tables in c# 6 77
Directory does exist 19 144
Beginner Probleme with my IDE Code::Blocks C++ 5 233
How to organize data in excel ? 2 112
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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

932 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

11 Experts available now in Live!

Get 1:1 Help Now