Chapter 7 Text Input/Output Objectives

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
CSE1301 Computer Programming: Lecture 19 File I/O
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
V-1 University of Washington Computer Programming I File Input/Output © 2000 UW CSE.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
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.
Chapter 18 I/O in C.
C Programming Lecture 5 : Basic standard I/O Lecture notes : courtesy of Ohio Supercomputing Center, science and technolgy support.
Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have.
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:
Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have.
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
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:
1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
CS 1704 Introduction to Data Structures and Software Engineering.
Files A collection of related data treated as a unit. Two types Text
CSCI N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
1 Computer Programming Lecture 15 Text File I/O Assist. Prof Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
Chapter 12 Text and Binary File Processing Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs.
Chapter 18 I/O in C Original slides from Gregory Byrd, North Carolina State University Modified slides by C. Wilcox, Y. Malaiya Colorado State University.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Chapter 7 Text Input/Output Objectives
Topics discussed in this section:
Introduction to the C Language
Chapter 2 - Introduction to C Programming
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.
University of Washington Computer Programming I
Chapter 7 Text Input/Output Objectives
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Chapter 18 I/O in C.
File I/O.
Basic Input and Output Operations
Chapter 2 - Introduction to C Programming
Topics discussed in this section:
CS111 Computer Programming
File Input/Output.
Programming in C Input / Output.
Programming in C Input / Output.
Introduction to the C Language
Files I/O, Streams, I/O Redirection, Reading with fscanf
Binary Files.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Functions, Part 2 of 3 Topics Functions That Return a Value
Chapter 18 I/O in C.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Chapter 2 - Introduction to C Programming
Chapter 18 I/O in C.
Programming in C Input / Output.
ECE 103 Engineering Programming Chapter 51 Random Numbers
Chapter 2 - Introduction to C Programming
Chapter 12: File I/O.
Module 12 Input and Output
Introduction to C Programming
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Topics discussed in this section:
Chapter 18 I/O in C.
Presentation transcript:

Chapter 7 Text Input/Output Objectives ❏ To understand the basic properties and characteristics of external files ❏ To understand the C implementation of file I/O using streams ❏ To write programs that read and write text files using the formatting functions ❏ To write programs that read and write text files using the C character I/O functions ❏ To write programs that handle simple I/O errors ❏ To understand and implement basic data validation concepts Computer Science: A Structured Programming Approach Using C

Files A file is an external collection of related data treated as a unit. The primary purpose of a file is to keep a record of data. Since the contents of primary memory are lost when the computer is shut down, we need files to store our data in a more permanent form. Computer Science: A Structured Programming Approach Using C

Streams As we already discussed, data is input to and output from a stream. A stream can be associated with a physical device, such as a terminal, or with a file stored in auxiliary memory. Computer Science: A Structured Programming Approach Using C

Computer Science: A Structured Programming Approach Using C Streams

Notes Standard stream names have already been declared in the stdio.h header file and cannot be declared again in our program. There is no need to open and close the standard streams. It is done automatically by the operating system. Computer Science: A Structured Programming Approach Using C

Standard Library Input/Output Functions The stdio.h header file contains several different input/output function declarations. They are grouped into eight different categories. The first three will be discussed in the following sections. Later on we will discuss those in the shaded boxes. Computer Science: A Structured Programming Approach Using C

Categories of Standard Input/Output Functions Computer Science: A Structured Programming Approach Using C Categories of Standard Input/Output Functions

Computer Science: A Structured Programming Approach Using C File Open Results

Computer Science: A Structured Programming Approach Using C Table 7-1 Text File Modes

Computer Science: A Structured Programming Approach Using C File-Opening Modes

Testing for Open and Close Errors PROGRAM 7-1 Testing for Open and Close Errors Computer Science: A Structured Programming Approach Using C

Testing for Open and Close Errors PROGRAM 7-1 Testing for Open and Close Errors Computer Science: A Structured Programming Approach Using C

Formatting Input/Output Functions Earlier we introduced two formatting input/output functions, scanf and printf. These two functions can be used only with the keyboard and monitor. The C library defines two more general functions, fscanf and fprintf, that can be used with any text stream. Computer Science: A Structured Programming Approach Using C

Table 7-2 Formatting Functions Computer Science: A Structured Programming Approach Using C Table 7-2 Formatting Functions

Notes A whitespace character in an input format string causes leading whitespace characters in the input to be discarded. A whitespace character in an output format string is copied to the output stream. The number, order, and type of the conversion specifications must match the number, order, and type of the parameters in the list. Otherwise, the result will be unpredictable and may terminate the input/output function. Computer Science: A Structured Programming Approach Using C

Conversion Specifications Computer Science: A Structured Programming Approach Using C Conversion Specifications

scanf reads from stdin; fscanf reads from a user-specified stream. Note scanf reads from stdin; fscanf reads from a user-specified stream. Computer Science: A Structured Programming Approach Using C

Sizes and Conversion Code for scanf Family Computer Science: A Structured Programming Approach Using C Sizes and Conversion Code for scanf Family

Sizes and Conversion Code for scanf Family Computer Science: A Structured Programming Approach Using C Sizes and Conversion Code for scanf Family

Side Effect and Value of scanf and fscanf Computer Science: A Structured Programming Approach Using C Side Effect and Value of scanf and fscanf

FIGURE 7-7 Another Common Error Computer Science: A Structured Programming Approach Using C FIGURE 7-7 Another Common Error

Checking scanf Results PROGRAM 7-2 Checking scanf Results Computer Science: A Structured Programming Approach Using C

Checking scanf Results PROGRAM 7-2 Checking scanf Results Computer Science: A Structured Programming Approach Using C

Missing Address Operator in scanf Computer Science: A Structured Programming Approach Using C Missing Address Operator in scanf

Computer Science: A Structured Programming Approach Using C Data Type Conflict

Computer Science: A Structured Programming Approach Using C fscanf Examples

Note Discarding the return character is different from consuming it. Discarding can be done by whitespaces in the control string; consuming can only be done by reading the return character with a %c.

Flags, Sizes, and Conversion Codes for printf Family Computer Science: A Structured Programming Approach Using C Flags, Sizes, and Conversion Codes for printf Family

Flags, Sizes, and Conversion Codes for printf Family Computer Science: A Structured Programming Approach Using C Flags, Sizes, and Conversion Codes for printf Family

Flag Formatting Options Computer Science: A Structured Programming Approach Using C Flag Formatting Options

Side Effect and Value of printf Computer Science: A Structured Programming Approach Using C Side Effect and Value of printf

Read and Print Text File of Integers Computer Science: A Structured Programming Approach Using C

Read and Print Text File of Integers Computer Science: A Structured Programming Approach Using C

Copy Text File of Integers Computer Science: A Structured Programming Approach Using C

Copy Text File of Integers Computer Science: A Structured Programming Approach Using C

Copy Text File of Integers Computer Science: A Structured Programming Approach Using C

Append Data to File Computer Science: A Structured Programming Approach Using C

Append Data to File Computer Science: A Structured Programming Approach Using C

Append Data to File Computer Science: A Structured Programming Approach Using C

Student Grades Computer Science: A Structured Programming Approach Using C

Student Grades Computer Science: A Structured Programming Approach Using C

Student Grades Computer Science: A Structured Programming Approach Using C

Student Grades Computer Science: A Structured Programming Approach Using C

Student Grades Computer Science: A Structured Programming Approach Using C

Student Grades Computer Science: A Structured Programming Approach Using C

Student Grades Computer Science: A Structured Programming Approach Using C

Student Grades Computer Science: A Structured Programming Approach Using C

Character Input/Output Functions Character input functions read one character at a time from a text stream. Character output functions write one character at the time to a text stream.

Character Input/Output Functions Computer Science: A Structured Programming Approach Using C Character Input/Output Functions

Create Text File Computer Science: A Structured Programming Approach Using C

Create Text File Computer Science: A Structured Programming Approach Using C

Create Text File Computer Science: A Structured Programming Approach Using C

Copy Text File Computer Science: A Structured Programming Approach Using C

Copy Text File Computer Science: A Structured Programming Approach Using C

Count Characters and Lines Computer Science: A Structured Programming Approach Using C

Count Characters and Lines PROGRAM 7-9 Count Characters and Lines Computer Science: A Structured Programming Approach Using C

Count Words Computer Science: A Structured Programming Approach Using C

Count Words Computer Science: A Structured Programming Approach Using C

Software Engineering In this section, we discuss some software engineering issues related to files.

Handling Errors—the Right Way PROGRAM 7-11 Handling Errors—the Right Way Computer Science: A Structured Programming Approach Using C

Handling Errors with Explanations Computer Science: A Structured Programming Approach Using C

Handling Errors with Explanations Computer Science: A Structured Programming Approach Using C

Ten Western States (2000 Census) Computer Science: A Structured Programming Approach Using C Ten Western States (2000 Census)