Character Input and Output

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Introduction to C Programming
Week 5 Part I Kyle Dewey. Overview Exam will be back Thursday New office hour More on functions File I/O Project #2.
UNIT 15 File Processing.
UNIT 12 UNIX I/O Redirection.
Lesson 2 Building a programming repertoire. Announcements  Homework due Monday  Will get considerably more difficult, so take advantage of relative.
1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.
Engineering Computing I Chapter 1 – Part B A Tutorial Introduction continued.
1 Homework Assignments Turn in HW1 (If not done yet, catch up!) Questions about HW1? Anyone still stuck on apply / UNIX account? Everyone have the books?
CS1061 C Programming Lecture 17: Steams and Character I/O A. O’Riordan, 2004.
Lecture 2: Character Input/Output in C
Character Input and Output C and Data Structures Baojian Hua
1 Review of Class on Oct Outline of Chapter 4  How to write a function?  Function Prototypes  Function Invocation  Function Definition  The.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
Imperative Programming Prof. Béat Hirsbrunner Amine Tafat, PhD Student Matthias Buchs and Raphaël Lesceux, Graduate Students Department of Informatics.
Pointers and Arrays C and Data Structures Baojian Hua
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.
Files Programs and data are stored on disk in structures called files Examples Turbo C++ - binary file Word binary file lab1.c - text file lab1.data.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
Computers and programming The 3 rd lecture Jiří Šebesta.
Lecture 13. Outline Standard Input and Output Standard Input and Output (I/O)– Review & more Buffered/unbuffered input Character I/O Formatted I/O Redirecting.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Outline Symbolic Constants Character Input and Output if … else switch…case.
Computer programming Lecture 4. Lecture 4: Outline Making Decisions [chap 6 – Kochan] –The if Statement –The if-else Construct –Logical Operators –Boolean.
File Handling Spring 2013Programming and Data Structure1.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
File IO and command line input CSE 2451 Rong Shi.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
Solution to Midterm Exam
CMSC 104, Version 9/011 The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading Section 4.7,
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
C Homework Write mypaste.c and mycomm.c – mypaste is like paste & mycomm is like comm – Both take two files as arguments – Paste reads a line from each.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.
Sudeshna Sarkar, IIT Kharagpur 1 I/O in C + Misc Lecture –
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
C is a high level language (HLL)
CPT: Chars/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to look at how C processes characters 4. Character.
1 Character Input/Output in C COS 217
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
I/O in C + Misc Lecture Sudeshna Sarkar, IIT Kharagpur.
CSE1320 Files in C Dr. Sajib Datta
CSE1320 Files in C Dr. Sajib Datta
The C “switch” Statement
CSE1320 Files in C Dr. Sajib Datta
File I/O We are used to reading from and writing to the terminal:
The C “switch” Statement
Structured Programming (Top Down Step Refinement)
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Today’s Lecture I/O Streams Tools for File I/O
The switch Statement Topics Multiple Selection switch Statement
The switch Statement Topics Multiple Selection switch Statement
CS150 Introduction to Computer Science 1
The switch Statement Topics Multiple Selection switch Statement
EECE.2160 ECE Application Programming
Characters and Strings Functions
EECE.2160 ECE Application Programming
File I/O We are used to reading from and writing to the terminal:
Presentation transcript:

Character Input and Output C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Overview We have talked about: This lecture: Basic data types and C control structures This lecture: Basic character input and output Three examples: echo input directly to output character counting line counting

Char IO Including the Standard Input/Output (stdio) library #include <stdio.h> Makes names of functions, variables, and macros available Defining procedure main() Starting point of the program, a standard boilerplate Read a single character Returns a single character from the text stream “standard in” (stdin) char c = getchar(); Write a single character Writes a single character to “standard out” (stdout) putchar (c);

The Code #include <stdio.h> int main(void) { int c; c = getchar(); putchar(c); return 0; }

Read Ten Chars #include <stdio.h> int main(void) { int c; int i; for (i=0; i<10; i++) c = getchar(); putchar(c); } return 0; self-increment operator

Infinite IO #include <stdio.h> // or a while version int main(void) { int c; int i; for ( ; ; ) c = getchar(); putchar(c); } return 0; // or a while version #include <stdio.h> int main(void) { int c; int i; while (1) c = getchar(); putchar(c); } return 0;

Conditional IO #include <stdio.h> int main(void) { int c; int i; c = getchar(); while (c != ‘a’) putchar(c); } return 0;

Character Counting #include <stdio.h> /* count characters in input */ int main() { long nc; nc = 0; while (getchar() != EOF) ++nc; } printf("%ld\n", nc); return 0;

Line Counting #include <stdio.h> /* count lines in input */ int main() { long numLines; numLines = 0; char c; while ((c=getchar()) != EOF) if (c == ‘\n’) ++numLines; } printf("%ld\n", numLines); return 0;

Arrays Thus far, we have seen: Characters are just small integers (0-255) More operations ++i, i++, ==, != Control structures Nested controls Next, we consider how to count the number of characters ‘0’ to ‘9’

A First Try #include <stdio.h> int main() { long num0, num1, …, num9; num0 = num1 = … = num9 = 0; char c; while ((c=getchar()) != EOF) { if (c == ‘0’) ++num0; else if (c == ‘1’) ++num1; …; } printf("%ld\n", …); return 0;

Using Arrays #include <stdio.h> int main() { long num[10]; char c; int i; for (i=0; i<10; i++) num[i] = 0; while ((c=getchar()) != EOF) if ((c >= ‘0’) && (c <= ‘9’)) ++num[c-’0’]; } return 0;

The Essence of Array An array variable a is just a pointer pointing to the first array element a[0] So when we pass an array to other functions, or we operate on the array variable, we are really operating on a pointer, not on array elements More on this later

An Example #include <stdio.h> void foo(long[] a) { a[0] = 999; return; } int main() long num[5]; num[0] = 0; foo (num); printf(“%ld\n”, num[0]); return 0; ? ? num ? ? ?

An Example #include <stdio.h> void foo(long[] a) { a[0] = 999; return; } int main() long num[5]; num[0] = 0; foo (num); printf(“%ld\n”, num[0]); return 0; ? num ? ? ?

An Example #include <stdio.h> void foo(long[] a) { a[0] = 999; return; } int main() long num[5]; num[0] = 0; foo (num); printf(“%ld\n”, num[0]); return 0; a ? num ? ? ?

An Example #include <stdio.h> void foo(long[] a) { a[0] = 999; return; } int main() long num[5]; num[0] = 0; foo (num); printf(“%ld\n”, num[0]); return 0; a 999 ? num ? ? ?

An Example #include <stdio.h> void foo(long[] a) { a[0] = 999; return; } int main() long num[5]; num[0] = 0; foo (num); printf(“%ld\n”, num[0]); return 0; 999 ? num ? ? ?