Presentation On Use of Files. Programming
Presentation On Use of Files. Programming
Use of files
Programming
Bachelor in Aerospace Engineering
Course 2018-2019
1
INTRODUCTION
2
Files
Introduction
5
DEFINITION
6
Files
Definition
File
Data stored in a permanent read and write computer
peripheral
Usually, files are stored on a hard disk
Sequence of bytes
Position in the file
7
Files
Definition
File
By itself, a file is nothing more than a series of related
bytes of data on a disk, one after the other.
9
Files
Binary and Text files
File
Binary files
Store a sequence of bits corresponding to the binary
representation of data values
E.g.: an integer value is represented with 4 bytes
135 >> 00000000 00000000 00000000 10000111
390 >> 00000000 00000000 00000001 10000110
10
Files
Binary and Text files
Text files
Store a sequence of bits corresponding to the textual
representation of a data value
E.g.: an integer value is represented with as many characters as
necessary (each character is 2 bytes)
135 >> '1' '3' '5' >> ASCII 49, ASCII 51, ASCII 53
00000000 00110001 00000000 00110011 00000000 00110101
390 >> '3' '9' '0' >> ASCII 51, ASCII 57, ASCII 48
00000000 00110011 00000000 00111001 00000000 00110000
11
Files
Binary and Text files
12
Files
Binary and Text files
13
Files
Binary and Text files
Binary files
Are shorter than text files (with the same data)
It is not necessary to translate data from a text-based
representation to the internal byte-based representation (this is
a time-consuming task, in particular with float and double
values)
Text files
Are easy to interpret
Can be edited with external tools
14
Files
Binary and Text files
BINARY FILES
Lowest level of data storage is bits (binary digits) : 0s and 1s -- bits
Bits grouped together into bytes, and bytes grouped into characters
16
Files
Binary and Text files
17
Files
Binary and Text files
18
Files
Binary and Text files
Both binary and text files contain data stored as a series of bits,
however:
19
BINARY AND TEXT FILES
BINARY FILES
20
Files
Binary files
Binary files
Binary files contain a sequence of bytes. When creating a
custom file format for a program, a programmer arranges
these bytes into a format that stores the necessary
information for the application.
21
Files
Binary files
Binary files
Image viewer Text editor
vader_01.jpg
22
Files
Binary files
Image: all binary? No, a few formats are … plain text files as ppm
Text editor
IrfanView
vader_01.ppm
23
Files
Text files
Text files
A file that holds a human-readable sequence of characters.
Text files are more restrictive than binary files since they
can only contain textual data.
24
Files
Text files
25
Files
Text files
26
Files
Text files
Operations
> Open the file
> Read from file
> Write to file
> Close the file
27
Files
Text files
1. OPEN
3. CLOSE
28
Files
Text files
29
Files
Text files
open
fid = fopen(<file_name> [, <permission>]);
write
fprintf(fid,'%d %7.4f %s',a,b,name);
read
…
Close
result = fclose(<file_id_var>);
30
Files
Text files
31
Files
Text files
The function reads data from an open text interpreting values in the file
according to the format specified, and stores such data in the array
variable (with size size) . The function reapplies the format throughout
the entire file and positions the file pointer at the end-of-file marker.
If fscanf cannot match the format to the data, it reads only the portion
that matches and stops processing.
Tip: When including the * symbol into a format descriptor the program will
only match that data, without reading it. It is very useful to represent
pieces of string we want to skip, using the descriptor %*s
32
Files
Text files
33
Files
Text files
34
Files
Text files
11/24/2018 35
Files
Text files
not an error
11/24/2018 36
Files
Text files
11/24/2018 37
Files
Text files
11/24/2018 38
Files
Text files
11/24/2018 39
Files
Text files
11/24/2018 40
Files
Text files
11/24/2018 41
Files
Text files
Exercise
The next text file represents a series of records with three elements
each: time, date and matrix. Write a MATLAB program to read the
whole file and store all the data in a vector of structures. The length of
the file (and therefore the number of records) is unknown.
myFile.txt
12:00:00
01-Jan-1977
4.21 6.55 6.78 6.55
9.15 0.35 7.57 7.15
7.92 8.49 7.43 7.06
9.59 9.33 3.92 0.31
09:10:02
23-Aug-1990
2.76 6.94 4.38 1.86
0.46 3.17 3.11 4.89
0.97 9.50 7.65 4.45
8.23 0.34 7.95 6.46
42
OTHER KIND OF FILES: DATABASES
44
External data storage
Databases
45
External data storage
Databases
46
External data storage
Databases
47
External data storage
Databases
48