Object Oriented Code for a Relative File. Design Questions Private Data the I/O file Record Count Private Methods Conversion from RRN to File Address.

Slides:



Advertisements
Similar presentations
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Advertisements

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.
CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
Functions, Projects, and C++ I/O Streams Dr. Nancy Warter-Perez June 4, 2003.
CPSC 231 D.H. C++ File Processing 1 Learning Objectives §C++ I/O streams. §Reading and writing sequential files. §Reading and writing random access files.
計算機概論實習 Files and Streams C++ views file as sequence of bytes Ends with end-of-file marker n-1 end-of-file marker 67 This is.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
Introduction to Object- Oriented Programming Prof. Shie-Jue Lee Dept. of Electrical Engineering National Sun Yat-sen University.
Program Input and the Software Design Process ROBERT REAVES.
File input and output in C++ Dr. Ahmed Telba. CSE202: Lecture 9The Ohio State University2 Reading & Writing to Files.
Programming Initializing Data of Struct Type. COMP102 Prog. Fundamentals: initialize struct type/ Slide 2 Ex. 10: Initialize Data of struct Type l By.
Chapter 8 Data File Basics.
File I/O ifstreams and ofstreams Sections 11.1 &
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Stack/Queue - File Processing Lecture 10 March 29, 2005.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Computer Programming TCP1224 Chapter 13 Sequential File Access.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
File I/O. fstream files File: similar to vector of elements Used for input and output Elements of file can be –character (text)struct –object (non-text.
I/O in C++ October 7, Junaed Sattar. Stream I/O a stream is a flow of bytes/characters/ints or any type of data input streams: to the program output.
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.
Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input.
File I/O Version 1.0. Topics I/O Streams File I/O Formatting Text Files Handling Stream Errors File Pointers.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Starting Out with C++, 3 rd Edition Basic file operations in C++ Dr. Ahmed Telba.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
STARTING OUT WITH STARTING OUT WITH Class 16 Honors.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
GE 211 Programming in C ++ Dr. Ahmed Telba Room :Ac -134
C++ Programming: chapter 6 – iostream 2015, Spring Pusan National University Ki-Joune Li 1.
1 Today’s Objectives  Announcements Homework #5 is due next Monday, July 24. Since this date is so close to the end of the semester, no late assignments.
CSCI 333 Data Structures I/O with C++ 20 October, 2003.
Lecture 14 Arguments, Classes and Files. Arguments.
Binary Files. Text Files vs. Binary Files Text files: A way to store data in a secondary storage device using Text representation (e.g., ASCII characters)
 Working with multiple files  Copying files  Updating files.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
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.
File I/O. Files allow permanent storage of programs and data. ifstream and ofstream objects –For file I/O the inclusion of is required. –Objects for file.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
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.
2008YeungNam Univ. SE Lab. 1 C++ views each file as a sequence of bytes terminated by EOF-marker. Header files Files are opened by creating objects of.
File Processing.
MT262A Review.
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Basic file operations in C++
C ++ MULTIPLE CHOICE QUESTION
CPSC 231 D.H. C++ File Processing
Student Data Score First Name Last Name ID GPA DOB Phone ...
Dynamic Memory Allocation Reference Variables
CS 1430: Programming in C++.
Basic File I/O and Stream Objects
آشنایی با ساختارها و کار با فایلها
Strings A collection of characters taken as a set:
Today’s Lecture I/O Streams Tools for File I/O
Sequential input and output Operations in file
C++ FileIO pepper.
Standard Input/Output Stream
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
CS150 Introduction to Computer Science 1
File I/O.
C++ Programming: chapter 6 – iostream
Reading from and Writing to Files
File I/O in C++ II.
C++ data types.
Reading from and Writing to Files
Presentation transcript:

Object Oriented Code for a Relative File

Design Questions Private Data the I/O file Record Count Private Methods Conversion from RRN to File Address Public Methods Constructor Find / Retrieve Change Record Add Record Delete Record Clean Up the File Parameters?

Class Declaration class FacultyOfficeClass { public: FacultyOfficeClass(char *filename); int Find_by_LName (char *LName); int Retrieve (int RRN, char *LName, char *FName, int &phone,int &office, char *dept); int Set (int RRN, char *LName, char *FName, int phone,int office, char *dept); private: fstream myfile; // input AND output file int Num_Recs; // number of records in the file struct faculty_struct { char fname[15],lname[15]; int phone, office; char dept[5]; };

Constructor FacultyOfficeClass::FacultyOfficeClass (char *filename) { /**** open the file ****/ myfile.open (filename, ios::in | ios::out); if (!myfile) { cerr << "Error opening input file:" << filename << endl; exit (1); } /**** read the number of records *****/ myfile.seekg (0, ios::beg); myfile.read(reinterpret_cast (&Num_Recs), sizeof(int)); }

Change Record int FacultyOfficeClass::Set (int RRN,char *LName, char *FName, int phone,int office,char *dept) { int byte_location; struct faculty_struct new_rec; /**** check for valid RRN ****/ if (RRN Num_Recs-1) return -1; /**** set all the fields ****/ strcpy (new_rec.lname, LName); strcpy (new_rec.fname, FName); new_rec.phone = phone; new_rec.office = office; strcpy (new_rec.dept, dept); /**** overwrite the correct record ****/ byte_location = sizeof(int) + (RRN * sizeof(struct faculty_struct)); myfile.seekp (byte_location, ios::beg); myfile.write(reinterpret_cast (&new_rec), sizeof(struct faculty_struct)); /**** return success ****/ return 1; }

Retrieve int FacultyOfficeClass::Retrieve (int RRN,char *LName, char *FName, int &phone,int &office,char *dept) { int byte_location; struct faculty_struct next_rec; /**** check for valid RRN ****/ if (RRN Num_Recs-1) return -1; /**** go get the right record ****/ byte_location = sizeof(int) + (RRN * sizeof(struct faculty_struct)); myfile.seekg (byte_location, ios::beg); myfile.read(reinterpret_cast (&next_rec), sizeof(struct faculty_struct)); /**** set all the parameters ****/ strcpy (LName, next_rec.lname); strcpy (FName, next_rec.fname); phone = next_rec.phone; office = next_rec.office; strcpy (dept, next_rec.dept); /**** return success ****/ return 1; }

main() - change a name case 2 : cout << "Current Last Name: "; cin >> lastname; RecNum = offices.Find_by_LName (lastname); if (RecNum == -1) { cout << "========================\n"; cout << " Not Found\n"; cout << "========================\n"; break; } else { offices.Retrieve (RecNum, lastname,firstname,… cout << "New Last Name: "; cin >> lastname; cout << "New First Name: "; cin >> firstname; offices.Set (RecNum, lastname,firstname,… } break;