Chapter 4 Basic Input and Output - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 4 Basic Input and Output

Description:

Reads to values typed at keyboard (separated by whitespace) ... Double quotes surround file_name. Two backslashes needed escape sequence for one ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 17
Provided by: RalphFTo5
Category:

less

Transcript and Presenter's Notes

Title: Chapter 4 Basic Input and Output


1
Chapter 4 Basic Input and Output
2
Reading Data
  • Keyboard input
  • cin object
  • gtgt called extraction operator
  • Syntax cin gtgt variable
  • Example

cin gtgt income gtgt expense
Reads to values typed at keyboard (separated by
whitespace) to memory locations names,
income, and expense
Lesson 4.1
3
Prompt
  • Message sent from program to screen
  • Informs user what data to enter
  • Precedes cin statement in program

int age cout ltlt Please enter your age.ltlt
endl cin gtgt age
Lesson 4.1
4
Input and Output Streams
  • Series of bytes in sequence
  • Flow from device to device
  • Objects are regions of storage in memory
  • Object used determines device stream comes from
    or goes to
  • cin and cout are identifiers for objects defined
    by iostream (screen and keyboard)

Lesson 4.1
5
Writing Output to a File
  • Similar to writing to screen
  • Use object connected to output file
  • Need the fstream header
  • include ltfstreamgt
  • Open file for writing
  • Declare object of ofstream class

Lesson 4.2
6
Opening Files
  • General form
  • ofstream object_name (file_name)
  • Choose object_name like variable name
  • object_name is object of class ofstream
  • Filename is where output will be stored
  • Can use full pathname C\\salary.out

Lesson 4.2
7
Writing to Files
  • General form
  • object_name ltlt variable_name
  • Use ofstream object to write to file like cout
    was used
  • ofstream outfile(C\\salary.out)
  • outfile ltlt Salary for week was ltlt money
  • Additional writing appended to file

Lesson 4.2
8
Closing Files (input and output)
  • General form
  • object_name.close ( )
  • Use C library function close
  • Use both object and function name separated by a
    period
  • Example outfile.close( )

Lesson 4.2
9
Open File for Reading
  • Need fstream header
  • include ltfstreamgt
  • Declare object of ifstream
  • ifstream object_name(file_name)
  • Use ifstream object to read file like cin

Lesson 4.3
10
Reading From a File
  • Use ifstream object to read file like cin used
    for keyboard
  • ifstream infile(C\\money.dat)
  • infile gtgt salary1 gtgt salary2
  • C looks for whitespace between numbers
  • Newline character treated as whitespace
  • Additional reading continues sequentially

Lesson 4.3
11
Reading From a File
s1
s2
s3
money.dat
35 12 2 3.5 6.18 34
12
2
35
infile gtgt s1 gtgt s2 gtgt s3
3.5
infile gtgt s4
Note The number of data entries and their data
types of variables should read should
match the number and order within the
file!
Lesson 4.3
12
Reading Character Data
  • Can use cin
  • cin gtgt c1 gtgt c2 gtgt c3
  • Reads next three characters typed at keyboard
  • Whitespace is NOT needed for separating character
    data
  • if used, whitespace is ignored

Lesson 4.4
13
Examples
Keyboard Input
char c1,c2,c3,c4,c5,c6,c7,c8
cin gtgt c1 gtgt c2 gtgt c3 cin gtgt c4 gtgt c5 cin gtgt
c6 cin gtgt c7 gtgt c8
af d32(enter)
hj(tab)w (enter)
k
a
2
Note All whitespacecharacters are ignored!
f
h
d
j
3
w
Lesson 4.4
14
Input Buffer
  • Region of memory used for temporary storage of
    information being transferred between devices
  • Accessed sequentially
  • Position indicator keeps track of point to which
    information has been read

Lesson 4.4
15
Input Buffer
Keyboard entry
af d32(enter)
hj(tab)w (enter)
a f d 3 2 (enter)
h j (tab) w (enter)
cin gtgt c1 gtgt c2 gtgt c3 gtgt c4 gtgt c5 gtgt c6
a
f
d
3
2
h
Lesson 4.4
16
Summary
Learned how to
  • Read input from keyboard
  • Write output to file
  • Read data from file
  • Read and work with character data
  • Work with the input buffer
Write a Comment
User Comments (0)
About PowerShow.com