C - Data Structures+IO
C - Data Structures+IO
1
Content
• 4.1. Enum
• 4.2. Struct
• 4.3. Union and Bit Fields
2
4. 1. Enumeration type
3
4. 1. Enumeration type
• Enumeration variables
• Declaration and usage
• enum dayInWeek {Mo, Tu, We, Th, Fr, Sa, Su};
• enum dayInWeek dv;
• dv = We;
• enum dayInWeek {Mo, Tu, We, Th, Fr, Sa, Su} dv;
• dv = We;
• enum dayInWeek {Mo, Tu, We, Th, Fr, Sa, Su} dv;
• dv = We;
4
4. 1. Enumeration type
• Enumeration variables
• Corresponding Values
• by default: integer constants: 0, 1, 2, ….
• can be explicitly set when defining.
5
Example
0 1 2 3 4 5 6 7 8 9 10 11
1, 0, 0
6
Example
1 2 5 6 10 11 12
7
4.2. Struct type
• Example:
• struct Student {
char name[20];
Do not initialize
int dob; components here!
int major;
};
8
4.2. Struct type
• Example:
• typedef struct {
char name[20];
int dob; Do not initialize
int major; components here!
} Student;
9
4.2. Struct type
struct Student {
char name[20];
int dob;
int major;
} sv1, sv2;
10
4.2. Struct type
struct Student {
char name[20]; int dob; int major; //OK
} sv1 = {"Le Duc Tho", 1984, 56};
11
4.2. Struct type
12
Example
13
Combination
typedef struct {
char name[20];
unsigned int age;
enum {Nam, Nu} gender;
struct {
char city[20];
char street[20];
int number;
} address;
} Student;
14
4.3. Union and Bit Fields
union char_and_ascii
{
char ch;
unsigned short ascii_val;
};
15
4.3. Union and Bit Fields
• Union structure:
union SOME_TYPES {
char character;
short shortinteger;
int integer;
} my_space;
my_space.integer = 0x12345678; // little endian: 78563412
// my_space.character = ? // = 0x78
// my_space.shortinteger = ? // = 0x5678
union
{
int integer; h.integer = 0x12345678;
struct h.Bytes.firstByte = ?
{ h.Bytes.secondByte = ?
char firstByte;
char secondByte;
// = 0x78
char thirdByte;
// = 0x56
char fourthByte;
} Bytes;
} h ;
16
4.3. Union and Bit Fields
• Bit field structure: is a struct, but fields can declare storage size up to
the bit level (1 or more bits) to reduce memory consumption (speed is
slow however)
• Type for bit field: unsigned int, signed int, or int
• address-of operator (&), sizeof cannot be applied to bit-field components
17
Input/Output
18
Concept
19
Concept
20
Concept
• The program also works with files on the computer: .TXT, .EXE,
.JPG, .DOC, .PPT, …
• File:
• A collection of information (data) organized in a certain form (format)
with a definite name.
• A continuous byte sequence (from a storage perspective).
• Stored in external storage devices such as floppy disks, hard disks, USB...
• Persists when the program ends.
• Unlimited size (depending on storage device)
• Program allows to read data (input device) and write data (output
device) on the file.
21
Concept
22
Work with stdout/stdin
23
Work with files
24
Open a file
25
Open a file
• The file opening may fail and return NULL need to check the return value
of fopen()
• Possible reasons for file opening failure:
• Open the file to read but the file does not exist
• The current user has no permissions
• The file is being opened in restricted mode by a certain program
• There are too many open files at the same time (limitation of OS)
26
Open files and limit reopening
shflag Meaning
_SH_DENYNO Unlimited
_SH_DENYRD re-opened with reading mode
_SH_DENYWR re-opened with writing mode
_SH_DENYRW re-opened with reading mode and writing mode
27
Read/write a file
28
Read/write a file
• Because reading/writing files uses a buffer, it is often necessary to use the fflush()
function to “clean” the buffer before switching from write to read (if opening the
file in read and write mode simultaneously)
• int fflush(FILE* file);
29
Read/write a file
30
Work with Stream
31
Example
32
Example
33
Example
while(!feof(fs)) {
num = fread(buf, 1, sizeof(buf), fs);
fwrite(buf, 1, num, fd);
}
fclose(fs); fclose(fd);
return 0;
}
34
Location of file pointer
35
Example
#include <stdio.h>
int main () {
FILE *fp;
fp = fopen("file.txt","w+");
fputs("This is tutorialspoint.com", fp);
return(0);
}
36
stdin, stdout, stderr
37
Read/write on memory
• Write:
• sprintf(char* buffer, const char* format, ...);
• Read:
• sscanf(const char* buffer, const char* format,
...);
• Similar to fprintf() and fscanf(), but the data is stored in a specified
memory area in the buffer parameter.
• Example:
• char s[50];
sprintf(s, "sin(pi/3) = %.3f", sin(3.14/3));
• Result: s will contain string "sin(pi/3) = 0.866"
38
Bài tập
1. Khai báo một kiểu dữ liệu cấu trúc miêu tả các thông tin của một chiếc ôtô có
các thuộc tính: model, khối lượng, màu sơn, 4 bánh trong đó mỗi bánh có
thuộc tính: chủng loại, bán kính, khối lượng. Cho phép người dùng nhập dữ
liệu cho 1 chiếc ô tô và in thông tin chiếc ô tô ra màn hình.
2. Định nghĩa mảng chứa các hàm tính sin, cos, tan của một góc, sử dụng cấu trúc
struct MenuItem {Tiêu đề, Hàm xử lý }. In ra màn hình các tiêu đề tương ứng
của các hàm, nhận lựa chọn của người dùng và thực hiện gọi hàm tương ứng
3. Định nghĩa kiểu struct Shape rồi viết hàm tính diện tích, chu vi của hình tuỳ
theo dạng của nó. Dùng 2 cách: switch và con trỏ hàm
39
Bài tập xử lý File
1. Viết chương trình nối một file vào sau một file hiện tại
2. Viết chương trình in ra dòng thứ 10 của một file
3. Viết chương trình chèn một dòng vào dòng thứ 10 của một file (ghi nội
dung sau chèn ra 1 file mới)
4. Viết một hàm trả về kích thước của một file
40
Exercises
41
Exercises on File processing
42