sscanf()- string scan function

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
A file reminder Files are used to store data and information They are manipulated through a pointer to FILE (FILE *) This pointer is returned when opening.
Dale Roberts Basic I/O – scanf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Command-line arguments CS 201 Fundamental Structures of Computer Science.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
1 CSE1301 Computer Programming: Lecture 9 Input/Output.
Image representation methods. Bitmap graphic method of a black-and-white image.
CS1061 C Programming Lecture 15: More on Characters and Strings A. O’Riordan, 2004.
CS 240: Data Structures Supplemental: Command Line Input.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
Binary Search Tree For a node: The left subtree contains nodes with keys less than the node's key. The right subtree contains nodes with keys greater than.
Strlen() implementation /* strlen : return length of string s */ int strlen(char *s) { int n; for (n = 0 ; s[n] != ‘\0’ ; n++) ; return n; } /* strlen.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
Adv. UNIX:io/91 Advanced UNIX v Objectives of these slides: –look in some detail at standard input and output in C Special Topics in Comp.
File IO and command line input CSE 2451 Rong Shi.
Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
1 Homework HW6 On line – due next class Starting K&R Chapter 7 and Appendix B Also, UNIX – Various chapters in Glass.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Fundamental Data Types, Operators and Expressions Kernighan/Ritchie: Kelley/Pohl: Chapter 2 Chapter 2, 3.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
CSE1301 Computer Programming: Lecture 6 Input/Output.
Cryptography.
C Primer Session – 1/25/01 Outline Hello World Command Line Arguments Bit-wise Operators Dynamic Memory / Pointers Function Parameters Structures.
IO revisited CSE 2451 Rong Shi. stdio.h Functions – printf – scanf(normally stops at whitespace) – fgets – sscanf Standard streams – stdin(defaults to.
Lecture 6: More Decisions & Arrays B Burlingame 9 March 2016.
CSC 2400 Computer Systems I Lecture 6 Strings. 2 Characters The char type is an 8-bit byte containing ASCII code values (e.g., ‘A’ = 65, ‘B’ = 66,...)
As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets! Alphanumeric.
Chapter 9 - Formatted Input/Output
Stack and Heap Memory Stack resident variables include:
Command Line Arguments
Command Line Arguments
Understand argc and argv
Day 02 Introduction to C.
Structure of C Programs
Command Line Arguments
File I/O.
Input / Output functions in C
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
C Programming:Part 3 Characters and Strings File Processing Exercise.
Lecture 10: Strings B Burlingame 4 April 2018.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Command-line Arguments
Command Line Arguments
Computer Science 210 Computer Organization
Input / Output functions in C
Files I/O, Streams, I/O Redirection, Reading with fscanf
Yung-Hsiang Lu Purdue University
CSI 121 Structured Programming Language Lecture 7: Input/Output
Command Line Arguments
C Stuff CS 2308.
רשימות מקושרות עבודה עם קבצים דוגמה
Strings.
CSE1320 Strings Dr. Sajib Datta
Chapter 9 - Formatted Input/Output
Command Line Parameters
File Handling.
Department of Computer and Information Science
C Input / Output Prabhat Kumar Padhy
Strings Adapted from Dr. Mary Eberlein, UT Austin.
CSCE 206 Lab Structured Programming in C
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Intro to the Shell with Fork, Exec, Wait
I/O CS580U - Fall 2018.
Presentation transcript:

sscanf()- string scan function 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.

sscanf()- string scan function General format: int sscanf(char *str, const char* format_str, …);

sscanf()- string scan function #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { char buffer[80]; char month[20]; char greet[15]; char str[90] = "Hello 50 75 30"; int day; int year; int nextYear; int width; int height; int area; int val[5];

sscanf()- string scan function /* Using sscanf to format command line arguments */ if(argc < 3) { fprintf(stdout, "Expected: %s width height\n", argv[0]); exit(1); } sscanf(argv[1], "%d", &width); sscanf(argv[2], "%d", &height); area = width * height; fprintf(stdout, "\ndimensions: %d x %d, area: %d\n", width, height, area);

sscanf()- string scan function /** 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. **/ fprintf(stdout, "Enter date (mm-string dd yyyy): "); 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);

sscanf()- string scan function /** 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, &val[0], &val[1], &val[2]); fprintf(stdout, "\n%s\n", greet); fprintf(stdout, "val[0]: %d\n", val[0]); fprintf(stdout, "val[1]: %d\n", val[1]); fprintf(stdout, "val[2]: %d\n\n", val[2]); return 0; }