File organization Slides 2.pdf
Document Details
Full Transcript
Lecture Contents Sample programs for file manipulation Physical files and logical files Opening and closing files Reading from files and writing into files How these operations are done in C and C++ Standard input/output and redirection What is a file? A file i...
Lecture Contents Sample programs for file manipulation Physical files and logical files Opening and closing files Reading from files and writing into files How these operations are done in C and C++ Standard input/output and redirection What is a file? A file is : A collection of data is placed under permanent or non-volatile storage media. Examples: Anything that you can store in a disk: hard drive, tape, optical media, and any other media which doesn’t lose the information when the power is turned off. Where do file structure fit in computer science? Physical file and logical files Physical file: ❑ Physically exists on secondary storage. ❑ known by the operating system. ❑ Appears in its file directory. Logical file: ❑ What your program actually uses. ❑ A ‘pipe’ through, which information can be extracted, or sent. Operating system: ❑ Get instructions from program or command line. ❑ Link logical file with physical file or device. Physical File vs. Logical File File Input and Output In general, there are two types of files: ❖Text File: Treated as sequence of characters e.g: C program source code Can be viewed, edited with text editor ❖Binary File: Treated as sequence of bytes e.g: Movie, music files Access requires specialized program Files can be input files or output files C++ Classes/Objects C++ is an object-oriented programming language. Everything in C++ is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake. Attributes and methods are basically variables and functions that belongs to the class. These are often referred to as "class members". A class is a user-defined data type that we can use in our program, and it works as an object constructor, or a "blueprint" for creating objects. Opening and Closing Files: Opening Files: ❖Opening a file makes it ready for use. ❖There are two options: Open an existing file. Create a new file. ❖When opening a file, we are positioned at the beginning of the file. Opening and Closing Files: Opening Files in C++: Logical name r (reading) w (writing) a (append) r + w+ a+ rb wb ab FILE *outFile; rb+ wb+ ab+ outFile = fopen Method(“A.txt”, to “w”); Physical The mode open file file Opening and Closing Files: Closing Files: ❖Makes the logical file name available for another physical file (it’s like hanging up the telephone after a call). After closing, the logical name may be used again with another physical file Opening and Closing Files: Closing Files: ❖If the files is not closed in the program, the operating system closes it at the end of the program execution. However, if the program terminates abnormally, data may be lost. Physical File And Logical Files Physical File And Logical Files Opening A File Opening A File Opening file (modes) Opening A File Closing A File Closing A File Closing A File Reading files in C++ Writing to files in C++ Detecting End of file #include #include using namespace std; void main() { char ch; fstream file; char fileName; cout > fileName; file.open(fileName, ios::in); file.unsetf(ios::skipws); while (1) //while(!file.fail()) { file >> ch; // Step 4a if (file.fail()) break; cout