Professional Documents
Culture Documents
Introduction to Programming
File
HCM City 1
Content
1 File types
2 Input-Output System
2
Introduction
• The programming with the file aims to store
the program's data into the secondary
memory and retrieve this data back when
necessary.
• Usually data storage is the file on disk.
• Two main file types are considered:
– Text File
– Binary file
3
Text File
• This is the most simple and common structured
text file, which can be viewed and repaired
using the operating system commands or
simple text editor programs.
• Is normally stored on disk as .txt.
• Most of the program's source code is stored on
disk as a text file.
• Content composed of 8-bit characters
– These characters are found to have code from 0x20
or higher.
– The control characters have a code that is less than
0x20
4
Expanded text file
• Unicode characters or multi-byte characters
can be saved.
• The two most common expanded crude text
structures are:
– Unicode text: Saves UTF-16 characters.
– UTF-8: Saves the character length fluctuations
from 1 to 4 bytes.
5
Binary files
• Are unstructured files such as raw text files.
• Each file consists of a range of data bytes,
including two types:
– Sequential bytes are not mutually relevant in
terms of organizational structure files.
– Is structured according to the convention of the
file creation software.
6
Steps to working with file
• Includes 3 main steps:
– Step 1. Open the file, the programmer needs to
include the correct path and file name.
– Step 2. Use the file (after successfully opening
the file).
Read data from the file inserted into the memory
variable in the program.
Write data from the memory variable in the program
File.
– Step 3. Close the file (after completing the
necessary tasks).
7
Open File function
8
Arguments in openning file (mode)
Arguments Meaning
b Open binary file type
t Open the text file (default)
r Open the file only to read data from the file. Returns NULL
if file not found.
w Open the file just to write the data to the file. The file will
be created if it is not already, the previous data is erased.
a Open the file only to add (append) data to the end of the
file. The file will be created if it is not already.
r+ Like the R mode and add the data recording feature and
the file will be created if not already.
w+ Same mode W and additional read feature.
a+ Same mode A and additional reading features.
9
Reading and writing data (stdio.h)
• Perform reading/writing data in the following
ways:
• Import/Export as Format
– Function: fscanf, fprintf
– Use only with text files.
• Import/export each character or line up file
– Function : getc, fgetc, fgets, putc, fputs
– Should only be used with text styles.
• Read/write data directly from memory-file
– Function : fread, fwrite
– Only used with binary file type.
10
Output with format
11
Input with format
12
Read a character
13
Read a string
14
Write a character
int putc(int ch, FILE *fp) và int fputc(in ch, FILE *fp)
15
Write a string
16
Write data in byte
17
Read data in byte
18
Close file
Success: return 0.
Fail: return EOF.
19
Close all stream
int fcloseall()
20
Position indicator
• Position indicator
– Created automatically when opening the file.
– Determines where the reading / writing takes
place in the file.
• Position
– When the file is not open: the position indicator
will be at the beginning of the file (value 0).
– When the file is open :
At the end of file when open for inserting (mode a or
a+)
At the beginning of file (or value 0) when opening
with other modes (w, w+, r, r+).
21
Sequentially vs random access
• Sequentially access
– Data must be read / written from pointer
indicator to position n-1 before reading data at
position n.
– The position indicator is neglected because the
position indicator automatically moves to the
next position after a read / write operation.
• Random access
– Can read / write at any position in the file
without having to read / write all data
beforehand => using the position indicator.
22
Reset the position indicator
None
23
Change the position indicator
Success: return 0.
Fail: returns a nonzero value.
24
Determines the position of the position indicator
25
Read Introduction to Programming Book (Page 323-331)
26
The End