0% found this document useful (0 votes)
11 views89 pages

Introduction To Program Design Lecture 11

Uploaded by

d11030825
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views89 pages

Introduction To Program Design Lecture 11

Uploaded by

d11030825
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 89

INTRODUCTION TO PROGRAM DESIGN

Lecture 11: Stream Input/Output and


File Processing
Yun-Shan Hsieh (謝昀珊),
Department of Computer Science and Information Engineering, NCKU

Acknowledgement: Slides template by https://slidesgo.com


char* Variables

2
Using Member Functions eof, get, and put

3
Using Member Functions eof, get, and put
(Cont.)

4
Comparing cin and cin.get

5
Comparing cin and cin.get (Cont.)

6
Using Member Function getline

7
Unformatted I/O Using read, write and
gcount

8
Integral Stream Base: dec, oct, hex and
setbase

9
Integral Stream Base: dec, oct, hex and
setbase (Cont.)

10
Floating-Point Precision

11
Floating-Point Precision (Cont.)

12
Floating-Point Precision (Cont.)

13
Field Width

14
Field Width (Cont.)

15
User-Defined Output Stream Manipulators

16
User-Defined Output Stream Manipulators
(Cont.)

17
User-Defined Output Stream Manipulators
(Cont.)

18
Stream Format States and Stream
Manipulators

19
Stream Format States and Stream
Manipulators (Cont.)

20
Trailing Zeros and Decimal Points

21
Trailing Zeros and Decimal Points (Cont.)

22
Justification

23
Justification (Cont.)

24
Justification (Cont.)

25
Padding

26
Padding (Cont.)

27
Padding (Cont.)

28
Integral Stream Base

29
Floating-Point Numbers; Scientific and
Fixed Notation

30
Floating-Point Numbers; Scientific and
Fixed Notation (Cont.)

31
Specifying Boolean Format

32
Specifying Boolean Format (Cont.)

33
Setting and Resetting the Format State via
Member Function flags

34
Setting and Resetting the Format State via
Member Function flags (Cont.)

35
Setting and Resetting the Format State via
Member Function flags (Cont.)

36
Stream Error States

37
Stream Error States (Cont.)

38
Stream Error States (Cont.)

39
File Processing

40
Files and Streams

41
Creating a Sequential File

42
Creating a Sequential File (Cont.)

43
Opening a File

44
Opening a File via the open Member
Function
• An ofstream object without opening a specific file—in this case, a
file can be attached to the object later. For example, the statement
ofstream outClientFile;
• The ofstream member function open opens a file and attaches it to
an existing ofstream object as follows:
outClientFile.open("clients.txt", ios::out);

45
End-of-file Key Combinations

46
Closing a File
• Once the user enters the end-of-file indicator, main terminates.
This implicitly invokes outClientFile’s destructor, which closes
the clients.txt file. The ofstream object can be closed explicitly.
outClientFile.close();

47
Reading Data from a Sequential File

48
Reading Data from a Sequential File (Cont.)

49
Reading Data from a Sequential File (Cont.)

50
Case Study: Credit Inquiry Program

51
Case Study: Credit Inquiry Program (Cont.)

52
Case Study: Credit Inquiry Program (Cont.)

53
Case Study: Credit Inquiry Program (Cont.)

54
Case Study: Credit Inquiry Program (Cont.)

55
Case Study: Credit Inquiry Program (Cont.)

56
Case Study: Credit Inquiry Program (Cont.)

57
Random-Access Files

58
Case Study: Credit-Processing Program
• Using random-access file-processing techniques, create a credit-
processing program capable of storing at most 100 fixed-length
records for a company that can have up to 100 customers.
• Each record should consist of an account number that acts as the
record key, a last name, a first name and a balance.
• The program should be able to update an account, insert a new
account, delete an account and insert all the account records into a
formatted text file for printing.

59
Case Study: Credit-Processing Program (Cont.)

60
Case Study: Credit-Processing Program (Cont.)

61
Case Study: Credit-Processing Program (Cont.)

62
Case Study: Credit-Processing Program (Cont.)

63
Case Study: Credit-Processing Program (Cont.)

64
Case Study: Credit-Processing Program (Cont.)

65
Case Study: Credit-Processing Program (Cont.)

66
Case Study: Credit-Processing Program (Cont.)

67
Case Study: Credit-Processing Program (Cont.)

68
Case Study: Credit-Processing Program (Cont.)

69
Case Study: Credit-Processing Program (Cont.)

70
Reading from a Random-Access File
Sequentially

71
Reading from a Random-Access File
Sequentially (Cont.)

72
Reading from a Random-Access File
Sequentially (Cont.)

73
Case Study: A Transaction-Processing
Program

74
Case Study: A Transaction-Processing
Program (Cont.)

75
Case Study: A Transaction-Processing
Program (Cont.)

76
Case Study: A Transaction-Processing
Program (Cont.)

77
Case Study: A Transaction-Processing
Program (Cont.)

78
Case Study: A Transaction-Processing
Program (Cont.)

79
Case Study: A Transaction-Processing
Program (Cont.)

80
Case Study: A Transaction-Processing
Program (Cont.)

81
Case Study: A Transaction-Processing
Program (Cont.)

82
Case Study: A Transaction-Processing
Program (Cont.)

83
Case Study: A Transaction-Processing
Program (Cont.)

84
Case Study: A Transaction-Processing
Program (Cont.)

85
Case Study: A Transaction-Processing
Program (Cont.)

86
Case Study: A Transaction-Processing
Program (Cont.)

87
References
• https://en.cppreference.com/w/cpp/header/cmath
• https://en.cppreference.com/w/cpp/chrono/c/time

88
Thank You Very Much!
Questions & Answers?

89

You might also like