Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 2 Fundamental File: Processing Operations

Similar presentations


Presentation on theme: "Lecture 2 Fundamental File: Processing Operations"— Presentation transcript:

1 Lecture 2 Fundamental File: Processing Operations
File Organization

2 Previous Lecture

3 The heart of file structure design.
Lecture 1 Introduction to the Design and Specification of File Structures The heart of file structure design. A short history of file structure design. A conceptual toolkit: File structure literacy. An object-oriented toolkit: Making file structure usable.

4 Today Lecture

5 Lecture Contents Physical and logical file. Opening files.
Closing files. Reading and writing. Seeking. Special Characters in files. Physical devices and logical files.

6 Lecture Objectives Describe the process of linking a logical file within a program to an actual physical file or device. Describe the procedures used to create, open and close files. Introduce the C++ input and output classes. Explain the use of overloading in C++. Introduce the concept of position within a file and describe procedures for seeking different positions.

7 Section 2.1 Physical Files and Logical Files

8 Physical and Logical Files Definitions
Physical File A collection of bytes stored on a disk or tape. Logical File A “Channel” (like a telephone line) that hides the details of the file’s location and physical format to the program.

9 Physical and Logical Files Illustration

10 Physical and Logical Files Differences
When a program wants to use a particular file, “myfile.dat”: the operating system must find the physical file called “myfile.dat”, make the hookup by assigning a logical file to it. This logical file has a logical name which is what is used inside the program.

11 Physical and Logical Files Example
select inp_file assign to “myfile.dat” This statement asks the OS to find the physical file name myfile.dat, assign a logical file through the variable inp_file. Example An office has 6 telephone line. A call comes from through telephone line no. 3 ≈ myfile.dat 3 ≈ inp_file

12 Section 2.2 Opening Files

13 Opening Files Once we have a logical file identifier hooked up to a physical file or device, we need to declare what we intend to do with the file: Open an existing file Create a new file (deleting and existing contents) That makes the file ready to be used by the program. We are positioned at the beginning of the file and are ready to read or write.

14 Opening Files in C++ Function: open in fstream object
Header file: fstream void open (const char* filename, ios_base::openmode mode = ios_base::in | ios_base::out);

15 Function “Open” Flags ios::
member opening mode app (append) Set the stream's position indicator to the end of the stream before each output operation. ate (at end) Set the stream's position indicator to the end of the stream on opening. binary (binary) Consider stream as binary rather than text. in (input) Allow input operations on the stream. out (output) Allow output operations on the stream. trunc (truncate) Any current content is discarded, assuming a length of zero on opening.

16 example ofstream myfile ("example.bin", ios::out | ios::app | ios::binary)

17 the value of flags cout<<"ios::out "<<ios::out<<endl; cout<<"ios::app "<<ios::app<<endl; cout<<"ios::binary "<<ios::binary<<endl; int format=ios::out|ios::app|ios::binary; cout<<"ios::out|ios::app|ios::binary "<<format<<endl;

18 Check for openning error
fstream file; file.open("b1.txt",ios::in); if (file.is_open()){ cout<<"The file is opened"<<endl; } else{ cout<<"ERROR: The file cannot be opened"<<endl;

19 Section 2.3 Closing Files

20 Closing Files Makes the logical file name available for another physical file (it’s like hanging up the telephone after a call). Ensures that everything has been written to the file [since data is written to a buffer prior to the file]. Files are usually closed automatically by the operating system (unless the program is abnormally interrupted).

21 example file.colse() flush()

22 Section 2.4 Reading and Writing

23 Assume that (see file ExampleForFutureWork.cpp)
Person P[3]; strcpy(P[0].name,"Ahme d"); P[0].age=20; strcpy(P[1].name,"Mona" ); P[1].age=30; strcpy(P[2].name,"Ali"); P[2].age=25; // binary file example struct Person{ char name[50]; int age; };

24 you want to write P in File
fstream ofile; ofile.open("DATA.mye",ios::out|ios::binary); ofile.write((char *)P,sizeof(P));

25 Reading ifile.read((char *)IP,3*sizeof(Person)); Person IP[3];
fstream ifile; ifile.open("DATA.mye",ios::in|ios::binary); ifile.read((char *)IP,3*sizeof(Person));

26 display IP cout<<IP[0].name<<" "<<IP[0].age<<endl; cout<<IP[1].name<<" "<<IP[1].age<<endl; cout<<IP[2].name<<" "<<IP[2].age<<endl;

27 Section 2.5 Seeking

28 Seeking A program does not necessarily have to read through a file sequentially It can jump to specific locations in the file or to the end of file so as to append to it. The action of moving directly to a certain position in a file is often called seeking.

29 Special Characters in Files
Section 2.6 Special Characters in Files

30 Special Characters in Files
Sometimes, the operating system attempts to make “regular” user’s life easier by automatically adding or deleting characters for them. These modifications, however, make the life of programmers building sophisticated file structures.

31 Special Characters in Files Examples
Control-Z is added at the end of all files (MS- DOS). This is to signal an end-of-file. <Carriage-Return> + <Line-Feed> are added to the end of each line (again, MS-DOS).

32 Physical Devices and Logical Files
Section 2.8 Physical Devices and Logical Files

33 Physical Devices as Files
Magnetic disks or tapes can be thought of as files and so can the keyboard and the console. No matter what the physical form of a file, it is represented in the same way.

34 Next Lectures

35 Secondary Storage and System Software
Disks Magnetic Tape Disk versus Tape Introduction to CD-ROM Physical Organization of CD-ROM Storage as a Hierarchy A Journey of a Byte Buffer Management

36 Questions?


Download ppt "Lecture 2 Fundamental File: Processing Operations"

Similar presentations


Ads by Google