Lecture 2 Fundamental File: Processing Operations File Organization
Previous Lecture
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.
Today Lecture
Lecture Contents Physical and logical file. Opening files. Closing files. Reading and writing. Seeking. Special Characters in files. Physical devices and logical files.
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.
Section 2.1 Physical Files and Logical Files
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.
Physical and Logical Files Illustration
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.
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 918-123-4567 through telephone line no. 3 918-123-4567 ≈ myfile.dat 3 ≈ inp_file
Section 2.2 Opening Files
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.
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);
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.
example ofstream myfile ("example.bin", ios::out | ios::app | ios::binary)
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;
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;
Section 2.3 Closing Files
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).
example file.colse() flush()
Section 2.4 Reading and Writing
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; };
you want to write P in File fstream ofile; ofile.open("DATA.mye",ios::out|ios::binary); ofile.write((char *)P,sizeof(P));
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));
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;
Section 2.5 Seeking
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.
Special Characters in Files Section 2.6 Special Characters in Files
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.
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).
Physical Devices and Logical Files Section 2.8 Physical Devices and Logical Files
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.
Next Lectures
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
Questions?