sscanf PowerPoint PPT Presentation

presentation player overlay
1 / 7
About This Presentation
Transcript and Presenter's Notes

Title: sscanf


1
sscanf()
2
sscanf
  • String scan function
  • sscanf( ) consumes data from a memory resident
    buffer instead of consuming data from a file.
  • Requires a format string to provide the
    formatting parameters for the data.
  • sscanf( ) returns the number of items it
    successfully consumed from the buffer.

3
sscanf
  • General format
  • int sscanf(char str, const char format_str,
    )
  • sscanf( ) consumes data from a memory resident
    buffer instead of consuming data from a file.
  • Requires a format string to provide the
    formatting parameters for the data.
  • sscanf( ) returns the number of items it
    successfully consumed from the buffer.

4
sscanf()
  • / demonstrates the use of sscanf /
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • int main(int argc, char argv)
  • char buffer80
  • char month20
  • char greet15
  • char str90 "Hello 50 75 30"
  • int day
  • int year
  • int nextYear
  • int width
  • int height
  • int area
  • int val5

5
sscanf()
  • / demonstrates the use of sscanf /
  • // Using sscanf to format command line
    arguments
  • if(argc lt 3)
  • fprintf(stdout, "Expected s width
    height\n", argv0)
  • exit(1)
  • sscanf(argv1, "d", width)
  • sscanf(argv2, "d", height)
  • area width height
  • fprintf(stdout, "\ndimensions d x d, area
    d\n",
  • width, height, area)

6
sscanf()
  • // Using sscanf to parse an input string .
  • // fgets stores input in a character string.
  • // must convert numeric characters to decimal
    values
  • // before using in a computation. sscanf can
    do the
  • // conversion.
  • fgets(buffer, 80, stdin)
  • sscanf(buffer, "s d d", month, day,
    year)
  • nextYear year 1 // performs arithmetic
    on year
  • fprintf(stdout, "\nDates\n")
  • fprintf(stdout, "s d, d\n", month, day,
    year)
  • fprintf(stdout, "s d, d\n", month, day,
    nextYear)

7
sscanf()
  • // Using sscanf to parse a string
  • // str is initialized above. sscanf converts
    the
  • // numeric data to decimal values using d
  • sscanf(str, "s d d d",
  • greet, val0, val1, val2)
  • fprintf(stdout, "\ns\nval0 d, val1 d,
    val2 d\n\n", greet, val0, val1, val2)
  • return 0
Write a Comment
User Comments (0)
About PowerShow.com