Lecture 2 Fundamental File: Processing Operations

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
January 13, Csci 2111: Data and File Structures Week1, Lecture 2 Basic File Processing Operations.
CMSC 2021 C++ I/O and Other Topics. CMSC 2022 Using C++ Stream I/O Default input stream is called cin Default output stream is called cout Use the extraction.
January 13, Files – Chapter 2 Basic File Processing Operations.
CPS120: Introduction to Computer Science Lecture 15 B Data Files.
CS-212 C++ I/O Dick Steflik. C++ I/O Modeled after UNIX’s concept of a “stream” –conceptionally a stream is a continuous flow of characters/bytes from.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
Chapter 15.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
Tutorial4us.com. File A file is a collection of related data stored in a particular area on the disk. The data is stored in disk using the concept of.
Program Input and the Software Design Process ROBERT REAVES.
計算機程式語言 Lecture 8-1 國立臺灣大學生物機電系 8 8 I/O File Streams and Data Files.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
File Systems (1). Readings r Reading: Disks, disk scheduling (3.7 of textbook; “How Stuff Works”) r Reading: File System Implementation ( of textbook)
Chapter 7 File I/O 1. File, Record & Field 2 The file is just a chunk of disk space set aside for data and given a name. The computer has no idea what.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
Operating Systems COMP 4850/CISG 5550 File Systems Files Dr. James Money.
Chapter 9 I/O Streams and Data Files
1 CS161 Introduction to Computer Science Topic #13.
FILE HANDLING IN C++.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
File Organization Lecture 1
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
File I/O in C++ II. Open() function Open() is a member function in each classes ( fstream, ifstream, ofstream) Void fstream :: open ( const char *filename,
FILE I/O IN C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
C++ Programming: chapter 6 – iostream 2014, Spring Pusan National University Ki-Joune Li 1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 12 Advanced File Operations.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
1 CSC241: Object Oriented Programming Lecture No 32.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Chapter 11 File Systems and Directories. 2 File Systems (Chapter 11.1) File: 1. A named collection of related data. 2.smallest amount of information that.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
C++ Programming: chapter 6 – iostream 2015, Spring Pusan National University Ki-Joune Li 1.
Learners Support Publications Working with Files.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Lecture 14 Arguments, Classes and Files. Arguments.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
Advanced File Operations Chapter File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to.
Ms N Nashandi Dr SH Nggada 2016/01/03Ms N Nashandi and Dr SH Nggada1 Week 6 -File input and output in C++
Basic file operations in C++ Dr. Ahmed Telba 1. 2 Learning Objectives §C++ I/O streams. §Reading and writing sequential files. §Reading and writing random.
Computer Programming II Lecture 9. Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
CS212: Object Oriented Analysis and Design
Chapter 14: Sequential Access Files
Programming with ANSI C ++
17 File Processing.
Welcome to ….. File Organization.
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Introduction to Programming
Tutorial4us.com File Handling in C++ Tutorial4us.com.
Chapter 7 Text Input/Output Objectives
What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
File I/O.
C ++ MULTIPLE CHOICE QUESTION
COMP 2710 Software Construction File I/O
FILE HANDLING IN C++.
files Dr. Bhargavi Goswami Department of Computer Science
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Advanced UNIX progamming
C++ Programming: chapter 6 – iostream
C Language B. DHIVYA 17PCA140 II MCA.
File I/O in C++ II.
Presentation transcript:

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?