Professional Documents
Culture Documents
Lect05 File Stream
Lect05 File Stream
2023
Contents
2. File in C
4. Workshop
Introduction
Endianness,
Alignment &
Padding
File in C
Workshop
File
3
Why files are needed?
Endianness,
Alignment &
Padding
File in C
Workshop
4
Types of Files
Endianness,
Alignment &
Padding
File in C
5
Endianness, Alignment & Padding
Endianness
Endianness,
Alignment &
Padding
File in C
...
...
...
7
Data Alignment
Endianness,
Alignment &
Padding
File in C
8
Data structure padding
Endianness,
Alignment &
Padding
File in C
struct Type {
int i;
double d;
char c;
};
9
Data structure padding (cont.)
Endianness,
Alignment &
Padding
File in C
Workshop
10
Endianness,
Alignment &
Padding
#pragma pack
File in C
11
#pragma pack (cont.)
Endianness,
Alignment &
Padding
File in C
Workshop
#pragma pack(4)
i i i i d d d d d d d d c
#pragma pack(8)
i i i i d d d d d d d d c
12
Bit Fields
Endianness,
Alignment &
Padding
File in C
struct Date {
unsigned short nWeekDay : 3; // 0..7 (3 bits)
unsigned short nMonthDay : 6; // 0..31 (6 bits)
unsigned short nMonth : 5; // 0..12 (5 bits)
unsigned short nYear : 8; // 0..100 (8 bits)
};
nWeekDay nMonth
nMonthDay nYear
13
Fixed-width integers
Endianness,
Alignment &
Padding
File in C
Workshop
std::int8_t signed 8 bit
std::uint8_t unsigned 8 bit
std::int16_t signed 16 bit
std::uint16_t unsigned 16 bit
std::int32_t signed 32 bit
std::uint32_t unsigned 32 bit
std::int64_t signed 64 bit
std::uint64_t unsigned 64 bit
14
File in C
File Operations
Endianness,
Alignment &
Padding
File in C
16
Opening or Creating a File
Endianness,
Alignment &
Padding
File in C
17
Closing a File
Endianness,
Alignment &
Padding
File in C
18
Reading and writing to a text file
Endianness,
Alignment &
Padding
File in C
Workshop
// Open a file in writing mode
fptr = fopen("data.txt", "w");
19
Reading and writing to a binary file
Endianness,
Alignment &
Padding
File in C
20
File & Stream in C++
• Text Files
• Error Testing
• Binary Files
• Random-Access Files
File & Stream
Endianness,
Alignment &
Padding
File in C
Program
Q W E R T Y U I O P
A S D F G H J K L
Z X C V B N M
.?123 space return
Monitor
Keyboard
File 22
Stream Hierachy
Endianness,
Alignment &
Padding
File in C
Workshop
istream ostream
fstream
23
File Stream Data Types
Endianness,
Alignment &
Padding
File in C
24
Using the fstream Data Type
Endianness,
Alignment &
Padding
File in C
Workshop
File Access Flag Meaning
ios::app Append mode. If the file already exists, its contents are preserved and all output is written to the end of the file.
By default, this flag causes the file to be created if it does not exist.
ios::ate If the file already exists, the program goes directly to the end of it. Output may be written anywhere in the file.
ios::binary Binary mode. When a file is opened in binary mode, data are written to or read from it in pure binary format.
ios::in Input mode. Data will be read from the file. If the file does not exist, it will not be created, and the open
ios::out Output mode. Data will be written to the file. By default, the file’s contents will be deleted if it already exists.
ios::trunc If the file already exists, its contents will be deleted (truncated). This is the default mode used by ios::out.
25
Opening and Closing a File
Endianness,
Alignment &
Padding
File in C
26
Writing to a File
Endianness,
Alignment &
Padding
File in C
27
Writing to a File (cont.)
Endianness,
Alignment &
Padding
File in C
28
Appending to a File
Endianness,
Alignment &
Padding
File in C
29
Appending to a File (cont.)
Endianness,
Alignment &
Padding
File in C
Workshop
Appending
30
File Output Formatting
Endianness,
Alignment &
Padding
File in C
File in C
32
The End-of-Line Puzzle
Endianness,
Alignment &
Padding
File in C
33
Reading from a File
Endianness,
Alignment &
Padding
File in C
34
Reading from a File (cont.)
Endianness,
Alignment &
Padding
File in C
dataFile This is the name of the file stream object. It specifies the
Binary Files
Random-Access Files
35
Reading from a File (cont.)
Endianness,
Alignment &
Padding
File in C
36
Stream Bits
Endianness,
Alignment &
Padding
File in C
37
Functions to Test the State of Stream Bits
Endianness,
Alignment &
Padding
File in C
38
Example
Endianness,
Alignment &
Padding
File in C
39
Text Files vs. Binary Files
Endianness,
Alignment &
Padding
File in C
Workshop
40
The Write and Read Member Functions
Endianness,
Alignment &
Padding
File in C
41
The Write and Read Member Functions (cont.)
Endianness,
Alignment &
Padding
File in C
Workshop • address is the starting address of the section of memory where the data
being read from the file is to be stored. This is expected to be the
address of a char (or a pointer to a char).
• size is the number of bytes of memory to read from the file. This
argument must be an integer value.
42
Example
Endianness,
Alignment &
Padding
File in C
Concept 7
BITMAPV5HEADER for the BITMAPV5HEADER
Text Files DIB Header Size
Image Width (w)
Workshop
Red channel bitmask
Green channel bitmask
Blue channel bitmask
Alpha channel bitmask
Pixel Format
Color Definition (index n) is substituted for BITMAPV5HEADER.
( optional )
GAP1
Pad row size to a multiple of 4 Bytes
Image Data
PixelArray[x,y]
Pixel[0,h-1] Pixel[1,h-1] Pixel[2,h-1] … Pixel[w-1,h-1] Padding
Pixel[0,h-2] Pixel[1,h-2] Pixel[2,h-2] … Pixel[w-1,h-2] Padding
( optional )
GAP2
ICC Color Profile (optional) Note: The ICC Color Profile may be present only when
Embedded, variable size the BITMAPV5HEADER is used.
ICC Color Profile Data.
( or path to a linked file ( This diagram wrongly suggests that the size of
containing Color Profile must be a multiple of 4 Bytes.
Byte offset:
ICC Color Profile Data )
0 1 2 3
It is drawn in that manner only to save vertical space.)
4 5 6 7 8 9 10 11 44
Creating Records with Structures
Endianness,
Alignment &
Padding
File in C
45
Store a Record to a File
Endianness,
Alignment &
Padding
File in C
46
Store a Record to a File (cont.)
Endianness,
Alignment &
Padding
File in C
47
Read a Record from a File
Endianness,
Alignment &
Padding
File in C
48
Read a Record from a File (cont.)
Endianness,
Alignment &
Padding
File in C
49
Design Structure for Format
Endianness,
Alignment &
Padding
File in C
50
Random-Access
Endianness,
Alignment &
Padding
File in C
Workshop
51
Read/Write Position
Endianness,
Alignment &
Padding
File in C
write position
52
The seekp and seekg Member Functions
Endianness,
Alignment &
Padding
File in C
53
The seekp and seekg Member Functions (cont.)
Endianness,
Alignment &
Padding
File in C
• Warning: If a program has read to the end of a file, we must call the file
stream object’s clear member function before calling seekg or seekp. This
clears the file stream object’s eof flag. Otherwise, the seekg or seekp
function will not work.
54
The tellp and tellg Member Functions
Endianness,
Alignment &
Padding
File in C
55
Workshop
� Quiz
Endianness,
Alignment &
Padding
File in C
57
Ï Exercises
Endianness,
Alignment &
Padding
File in C
Workshop
58
References
Deitel, P. (2016).
C++: How to program.
Pearson.
Gaddis, T. (2014).
Starting Out with C++ from Control Structures to Objects.
Addison-Wesley Professional, 8th edition.
Jones, B. (2014).
Sams teach yourself C++ in one hour a day.
Sams.