Course organization. Course introduction ( Week 1)

Similar documents
CSC 1107: Structured Programming

CSC 1107: Structured Programming

Input / Output Functions

C mini reference. 5 Binary numbers 12

211: Computer Architecture Summer 2016

Introduction to string

C Libraries. Bart Childs Complementary to the text(s)

Chapter 8: Character & String. In this chapter, you ll learn about;

8. Characters, Strings and Files

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

by Pearson Education, Inc. All Rights Reserved.

Strings. Arrays of characters. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY

File I/O. Arash Rafiey. November 7, 2017

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Files. Programs and data are stored on disk in structures called files Examples. a.out binary file lab1.c - text file term-paper.

C programming basics T3-1 -

Chapter 8 - Characters and Strings

File Access. FILE * fopen(const char *name, const char * mode);

File IO and command line input CSE 2451

Standard C Library Functions

Scientific Programming in C V. Strings

Strings. Daily Puzzle

Contents. A Review of C language. Visual C Visual C++ 6.0

File Handling in C. EECS 2031 Fall October 27, 2014

Chapter 8 C Characters and Strings

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

ONE DIMENSIONAL ARRAYS

Standard File Pointers

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

LESSON 4. The DATA TYPE char

Advanced C Programming Topics

C: How to Program. Week /May/28

Course organization. Course introduction ( Week 1)

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

CS240: Programming in C

EECS2031. Modifiers. Data Types. Lecture 2 Data types. signed (unsigned) int long int long long int int may be omitted sizeof()

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

EM108 Software Development for Engineers

Characters and Strings

Programming in C Quick Start! Biostatistics 615 Lecture 4

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

Character Arrays. strlen("hello, world"); /* string constant */ strlen(array); /* char array[100]; */ strlen(ptr); /* char *ptr; */

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

Fundamentals of Programming. Lecture 11: C Characters and Strings

Input/Output: Advanced Concepts

System Software Experiment 1 Lecture 7

CSC 270 Survey of Programming Languages. Input and Output

Computer Programming Unit v

Unit 4. Input/Output Functions

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

C Programming Language Review and Dissection III

CSC 270 Survey of Programming Languages. C-String Values

Stream Model of I/O. Basic I/O in C

Strings and Library Functions

C PROGRAMMING. Characters and Strings File Processing Exercise

Introduction to C/C++ Lecture 5 - String & its Manipulation

UNIT-I Input/ Output functions and other library functions

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

THE FUNDAMENTAL DATA TYPES

File (1A) Young Won Lim 11/25/16

2009 S2 COMP File Operations

Create a Program in C (Last Class)

Fundamentals of Programming & Procedural Programming

Chapter 8 Character Arrays and Strings

Simple Output and Input. see chapter 7

Course organization. Course introduction ( Week 1)

Using Character Arrays. What is a String? Using Character Arrays. Using Strings Life is simpler with strings. #include <stdio.

Text Output and Input; Redirection

UNIT IV-2. The I/O library functions can be classified into two broad categories:

Should you know scanf and printf?

Computers Programming Course 11. Iulian Năstac

Arrays, Strings, & Pointers

Input/Output and the Operating Systems

ECE551 Midterm Version 1

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

Multiple Choice Questions ( 1 mark)

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Computer Science & Engineering 150A Problem Solving Using Computers

Characters, Character Strings, and string-manipulation functions in C

PRINCIPLES OF OPERATING SYSTEMS

How do we define pointers? Memory allocation. Syntax. Notes. Pointers to variables. Pointers to structures. Pointers to functions. Notes.

CSE 374 Midterm Exam 11/2/15. Name Id #

Fundamentals of Programming

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Fundamental of Programming (C)

Chapter 5, Standard I/O. Not UNIX... C standard (library) Why? UNIX programmed in C stdio is very UNIX based

Strings. Chuan-Ming Liu. Computer Science & Information Engineering National Taipei University of Technology Taiwan

C Characters and Strings

Course organization. Part I: Introduction to C programming language (Week 1-12) Chapter 1: Overall Introduction (Week 1-4)

Transcription:

Course organization Course introduction ( Week 1) Code editor: Emacs Part I: Introduction to C programming language (Week 2-9) Chapter 1: Overall Introduction (Week 1-3) Chapter 2: Types, operators and expressions (Week 4) Chapter 3: Control flow (Week 5) Chapter 4: Functions and program structure (Week 6, 7) Chapter 5: Pointers and arrays (Week 8) Chapter 6: Structures (Week 9) Chapter 7: Input and Output (Week 10) Part II: Skills others than programming languages (Week 11-12) Debugging tools(week 11) Keeping projects documented and manageable (Week 12) Source code managing (Week 12) Part III: Reports from the battle field (student forum) (week 12 16) 1

Chapter 7 Input and Output Chaochun Wei Shanghai Jiao Tong University Spring 2013

Contents 7.1 Standard input and output 7.2 Formatted output -- printf 7.3 Variable-length argument lists 7.4 Formatted input -- scanf 7.5 File access 7.6 Error handling -- Stderr and Exit 7.7 Line input and output 7.8 Miscellaneous Functions

Input and output Not part of the C language itself They are part of the standard library functions of C Standard library functions include Input, output string handling, storage management Mathematical routines They are specified in header files, including <stdio.h> <string.h> <ctype.h>

7.1 Standard input and output Input Read from standard input ( keyboard) int getchar(void) Read characters from an file infile. prog < infile Take input from other program otherprog Otherprog prog

7.1 Standard input and output Output output to standard output ( screen) int putchar(int) Output to a file outfile Prog > outfile Output to other program otherprog prog anotherprog More details see hands-on example 7.1

7.2 Formatted output --printf printf syntax of printf int printf(char *format, arg1, arg2, ) Format string Normal characters Conversion characters ( begins with a %) A width or precision may be specified as * E.g., to print at most max characters from a string s: printf( %.*s, max, s); More details see hands-on example 7.2

Format string (%) Character 7.2 Formatted output --printf Argument type; printed as d, i Int; decimal number. o Unsigned int; unsigned octal number (without a leading zero) X, x Unsigned int; unsigned hexadecima number (without a leading 0x or 0X), using abcdef or ABCDEF for 10,11, 12, 13, 14 and 15. u c s f Unsigned int; unsigned decimal number Int; single character. Char *; print a string, until a \0 or the number of characters given by the precision Double; [-]m.dddddd, where the number of d s is given by the precision (default 6) e, E Double; [-]m.dddddd e±xx or [-]m.dddddd E±xx, where the number of d s is given by the precision (default is 6) p Void *; pointer (implementation-dependent representation) % No argument is converted; print a %

7.3 Variable-length argument lists The declaration for printf is int printf(char *fmt, ) the declaration means variable-length argument list. can only appear at the end of an argument list. More details see hands-on example 7.3

7.4 Formatted input --scanf Scanf: read characters from the standard input Interpret them according to the format string Store the results in the remaining arguments syntax of scanf, sscanf int scanf(char *format, ) Int sscanf(char *string, char *format, arg1, arg2, ) Format string Blanks or tabs, which are ignored Normal characters (not %) Conversion characters ( begins with a %) More details see hands-on example 7.4

Format string (%) Character Input data; argument type d decimal integer; int * i 7.4 Formatted input --scanf Integer; int *. The integer may be in octal (leading 0) or hexadecimal (leading 0x or 0X) o Octal integer( with or without a leading zero); unsigned int * U unsigned decimal integer; unsigned int * x Hexadecimal integer (with or without leading 0x or 0X); unsigned int * c s Characters; char *. The next input characters (default 1) are placed at the indicated spot. The normal skip over white space is suppressed; to read the next non-white space character, use %1s. Character string (not quoted); char *, pointing to an array of characters large enough for the string and a terminating \0 that will be added e,f,g Floating-point number with optional sign, optional decimal point and optional exponent; float * % No argument is converted; print a %

7.4 Formatted input --scanf The arguments must be pointers in scanf, sscanf int scanf(char *format, arg1, arg2, ) Int sscanf(char *string, char *format, arg1, arg2, ) More details see hands-on example 7.4

7.5 File access Read, write, append Open a file FILE *fp; FILE *fopen(char *name, char *mode); Mode r : read w : write a : append b : binary files

7.5 File access Open a file FILE *fp; FILE *fopen(char *name, char *mode); Read If a file does not exist, it s an error Write If a file does not exist, it will be created If a file exits, the old content will be discarded Append If a file exits, the old content will be preserved If there is an error, fopen returns NULL.

7.5 File access After a file is open Read the next character from a file int getc (FILE *fp); Write a character to a file int putc (int c, FILE *fp); Close a file after the file access is over int fclose(file *fp); See more details in hands-on experiment 7.5

7.5 File access Formatted input or output of files int fscanf(file *fp, char *format, ) int fprintf(file *fp, char *format, ) See more details in hands-on experiment 7.6

7.6 Error handling stderr and exit When a file can t be accessed for some reason Stderr: Output the error message on the screen Exit: Terminate the program (exit the program) Terminate the program Close all open output files and Flush out buffered output See more details in hands-on experiment 7.6

7.7 Line input and output Line input char *fgets( char *line, int maxline, FILE *fp); Reads the next input line from file fp into line; at most maxline 1 characters will be read Line output int fputs( char *line, FILE *fp); Writes a string to a file

7.8 Miscellaneous Functions String operations : <string.h> strcat(s, t) concatenate t to end of s strncat(s, t, n) concatenate n characters of to end of s strcmp(s, t) return negative, zero, or positive for s < t, s == t, or s > t strncmp(s, t, n) same as strcmp but only in first n chars strcpy(s, t) copy t to s strncpy(s, t, n) copy at most n characters of t to s strlen(s) return length of s strchr(s, c) return pointer to first c in s, or NULL if not present strrchr(s, c) return pointer to last c in s, or NULL if not present

7.8 Miscellaneous Functions Character class testing and conversion isalpha(c) non-zero if c is alphabetic, 0 if not isupper(c) non-zero if c is upper case, 0 if not islower(c) non-zero if c is lower case, 0 if not isdigit(c) non-zero if c is digit, 0 if not isalnum(c) non-zero if isalpha(c) or isdigit(c), 0 if not isspace(c) non-zero if c is blank, tab, newline, return toupper(c) return c converted to upper case tolower(c) return c converted to lower case

7.8 Miscellaneous Functions Ungetc int ungetc(int c, FILE *fp); Pushes the character c back into file fp, and returns either c, or EOF for an error.

7.8 Miscellaneous Functions Command execution system(char *s); Executes the command contained in string s. Returns the exit value of command s.

7.8 Miscellaneous Functions Storage management void *malloc(size_t n); Returns a pointer to n bytes of uninitialized storage, or NULL if the request can not be satisfied void *calloc (size_t n, size_t size) Returns a pointer to an array of n objects of the specified size, or NULL if failed. void *realloc(void *p, size_t size); Changes the size of the object pointed by p to size. Returns a pointer to the new space or NULL if the request can not be satisfied, in which case *p is unchanged More details see hands-on experiment 7.8

7.8 Miscellaneous Functions Storage management Type conversion: convert to proper type int *ip; ip = (int *) calloc (n, sizeof (int)); free(p): frees the space pointed to by p, which is obtained by a call to malloc, calloc or realloc More details see hands-on experiment 7.8