Chuong11, File - Array - String
Chuong11, File - Array - String
1
Overview
2
Open a file
3
Open a file
4
Closing a file
5
Text files
• ios::binary flag is not included in their opening mode
• Writing operations on text files are performed in the same way we
operated with cout.
6
Text files
• Reading from a file can also be performed in the same way that we
did with cin
7
Checking state flags
8
Get and put stream position
N bytes dữ liệu của một file ở mức mô hình
1 2 3 N
… EOF
Sau khi mở tập tin thành công, thẻ đánh dấu tự động chì đến byte đầu tiên của tập tin
9
Get and put stream position
10
Get and put stream position
• tellg() and tellp(): return a value of the member type streampos,
which is a type representing the current get position (in the case of
tellg) or the put position (in the case of tellp)
• seekg() and seekp(): change the location of the get and put positions
• seekg ( position ) and seekp ( position ): the stream pointer is changed to the
absolute position position (counting from the beginning of the file)
• seekg ( offset, direction ) and seekp ( offset, direction ):
11
Get and put stream position
12
Binary files
13
Array
14
Overview
• An array is a collection of data belonging to the same datatype and
category, stored in contiguous memory locations. The size of the array
remains fixed once declared. For example, If we need to manage the
marks of numerous students, arrays simplify the task by allowing us to
store and organize data for a large number of students efficiently,
eliminating the need for multiple individual variables
15
Properties of Array
• Arrays store data of the same data type in a contiguous memory
location.
• Indexing begins at 0, with the first element at the 0th index.
• Elements are accessed using their indices.
• Array size remains constant once declared.
• Arrays can have multiple dimensions for complex data structures.
• The sizeof operator determines the number of elements in an array.
• Size of elements in an array can be found by subtracting adjacent
addresses
16
Declaring an array
17
Initializing Arrays
18
Initializing Arrays
19
Accessing Array Elements
• Array elements are accessed using indexes. In C++, array indexing
starts from 0, which implies that the first element in the array is
placed at zeroth index. If an array has five elements, then indexing will
be done from 0 to 4.
20
Accessing Array Elements
21
Array With Empty Members
We can also initialize C++ Arrays with fewer elements than the size of
the array. The provided elements get stored in the array, and the
remaining elements get initialized with 0.
22
Passing Array to Function
23
Multidimensional Arrays
24
Multidimensional Arrays
25
Multidimensional Arrays
26
Multidimensional Arrays
27
Multidimensional Arrays
28
String
29
Overview
• A string variable contains a collection of characters surrounded by
double quotes.
30
String Concatenation
31
String Length
32
Access Strings
33
Special Characters
34
User Input Strings
35
Functions
s.substr(x, y)
36
Functions
s.find(r)
37
Functions
pop_back
38
Functions
s.erase(x, n)
39
Functions
s.replace(x, n, str)
40
Functions
s1.compare(s2)
41
Functions
s1.swap(s2)
42
Functions
s1.insert(index, s2)
43
Functions
front(), back()
44