1 CSC241: Object Oriented Programming Lecture No 32.

Slides:



Advertisements
Similar presentations
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 13 Advanced.
Advertisements

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.
 2006 Pearson Education, Inc. All rights reserved File Processing.
Copyright © 2012 Pearson Education, Inc. Chapter 12: Advanced File Operations.
 2006 Pearson Education, Inc. All rights reserved File Processing.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
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.
1 File I/O In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
CSE Lectures 22 – Huffman codes
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.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
1 CS161 Introduction to Computer Science Topic #13.
FILE HANDLING IN C++.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 12: Advanced File Operations.
Programming Principles II Lecture Notes 7 Files Andreas Savva.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
File handling in C++ BCA Sem III K.I.R.A.S. Using Input/Output Files Files in C++ are interpreted as a sequence of bytes stored on some storage media.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Stack/Queue - File Processing Lecture 10 March 29, 2005.
 2008 Pearson Education, Inc. All rights reserved File Processing.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
“After a day spent staring at a computer monitor, think of a book as a kind of screen saver for your brain” “One good reason why computers can do more.
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.
C++ FILE I/O.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 12 Advanced File Operations.
DCT1063 Programming 2 CHAPTER 3 FILE INPUT AND FILE OUTPUT Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Streams, and File I/O Review. STREAMS Review STREAMS Streams are sequences of bytes. C++ I/0 occurs in streams Input – bytes flow from device to memory.
UNIT VI Streams & Files in C++: Stream classes, stream errors, disk file I/O with streams, File pointers, Error handling in file I/O. File I/O with members.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
1 CSC103: Introduction to Computer and Programming Lecture No 27.
Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams  IOStreams are part of the Standard C++ library.
Learners Support Publications Working with Files.
CSCI 333 Data Structures I/O with C++ 20 October, 2003.
1 Object Oriented Data Structures CSIS2020 File I/O in C++
Lecture 14 Arguments, Classes and Files. Arguments.
FILE HANDLING(WORKING WITH FILES) FILE- A file is a collection of related data stored in a particular area on the disk. STREAMS- interface between the.
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)
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Starting Out with C++ 1 Chapter 12: File Operations What is a File? A file is a collection on information, usually stored on some electronic medium. Information.
KBD Program with file I/O Operation File Disk Program with file I/O Operation Screen Cin Cout.
Ms N Nashandi Dr SH Nggada 2016/01/03Ms N Nashandi and Dr SH Nggada1 Week 6 -File input and output in C++
Lecture 2 Fundamental File: Processing Operations
CS212: Object Oriented Analysis and Design
Chapter 14: Sequential Access Files
Programming with ANSI C ++
17 File Processing.
Chapter 13: Advanced File and I/O Operations
Tutorial4us.com File Handling in C++ Tutorial4us.com.
C ++ MULTIPLE CHOICE QUESTION
CPSC 231 D.H. C++ File Processing
FILE HANDLING IN C++.
17 File Processing.
17 File Processing.
آشنایی با ساختارها و کار با فایلها
files Dr. Bhargavi Goswami Department of Computer Science
Chapter 13: Advanced File and I/O Operations
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.
Topics Input and Output Streams More Detailed Error Testing
Chapter 12: Advanced File Operations.
Data File Handling in C++
C++ Programming: chapter 6 – iostream
Data File Handling RITIKA SHARMA.
C++ Programming Lecture 8 File Processing
Presentation transcript:

1 CSC241: Object Oriented Programming Lecture No 32

2 Previous Lecture Stream and classes Input and Output stream iostream class ios class – Formatting Flags – Error-Status Bits – Formatted File I/O » Reading data » Writing data

3 Today’s Lecture File operations Formatted File I/O Reading data Writing data Character I/O Binary I/O Object I/O

4 Character I/O put() and get() functions, can be used to output and input single characters These are member of ostream and istream, respectively Go to program Another way is to use rdbuf() function ifstream infile(“TEST.TXT”); cout << infile.rdbuf();

5 Binary I/O If a large amount of numerical data is to be stored then it’s efficient to use binary I/O, In binary mode, – number are stored as they are in RAM, rather than as strings of characters – int is stored in 4 bytes, whereas its text version might be “12345”, requiring 5 bytes. – a float is always stored in 4 bytes, while its formatted version might be “ e13”, requiring 10 bytes

6 binary input and output with integers Opening file in write mode Writing whole array from buff to os object Opening file in read mode Reading whole array in buff from is object ofstream os(“edata.dat”, ios::binary); os.write( reinterpret_cast (buff), MAX*sizeof(int) ); ifstream is(“edata.dat”, ios::binary); is.read( reinterpret_cast (buff), MAX*sizeof(int) );

7 reinterpret_cast operator It is used to make it possible for a buffer of type int to look to the read() and write() functions like a buffer of type char It changes the type of a section of memory Go to program

8 Object I/O An object’s values can be written in and read back from a file Generally binary mode is use for Object I/O – This writes the same bit configuration to disk that was stored in memory, and – ensures that numerical data contained in objects is handled properly Example program – Input value from user in an object of class person – writes this object to the disk file

9 Writing/reading an Object to Disk class person { protected: char name[40]; int age; public: void getData() { cout > name; cout > age; } }; main() { person pers; pers.getData(); ofstream outfile(“PERSON.DAT”, ios::binary); outfile.write(reinterpret_cast (&pers), sizeof(pers)); } Go to program

10 Compatible Data Structures Reading and writing object can – belong to same class of objects – or if classes are different then they must have same data members (without virtual functions) Person class object occupy = 44 bytes Make sure a class used to read an object is identical to the class used to write it. Also do not attempt disk I/O with objects that have pointer data members Go to program

11 I/O with Multiple Objects class person { protected: char name[40]; int age; public: void getData() { cout >name; cout > age; } void showData() { cout << “\n Name: “ << name; cout << “\n Age: “ << age; } }; main() function: do { cout <<“Enter data:”; // input values in object // writing object in file cout<<“Enter another(y/n)“; cin >> ch; } while(ch==’y’); file.seekg(0); Go to program

12 The Mode Bits It defined in ios, specify various aspects of how a stream object will be opened Mode BitResult inOpen for reading (default for ifstream) outOpen for writing (default for ofstream) ateStart reading or writing at end of file (AT End) appStart writing at end of file (APPend) truncTruncate file to zero length if it exists (TRUNCate) nocreateError when opening if file does not already exist noreplaceError when opening for output if file already exists, unless ate or app is set binaryOpen file in binary (not text) mode

13 File Pointers Each file object has associated with two integer values called the get pointer and the put pointer. These are also called the current get position and the current put position, Various modes bits can be used to set file pointer position There are times when it is required to perform read and write operation from an arbitrary location in file

14 Cont. seekg() and seekp() are used to set the position of get pointer (position of reading from file) and put pointer (position of writing in file) tellg() and tellp() are used to know/examine the get pointer and put pointer position in file

15 Example: Modify record First ask the user which record he intends to modify. E.g. ask to input name of the employee whose record is to be modified On modifying the record, the existing record gets overwritten by the new record. – We want to modify cgpa of Ali Ahmad from 3.85 to 4.00 Fatima Tariq Physics Ali Ahmad Computer science Fahad Hamid Computer science Tahir Khan Bioinformatics Ali Ahmad Computer science

16 Seek function It can be used in two forms – Specifying the Position (single argument) – Specifying the Offset (two arguments) Specifying the Position – It is used to position the get pointer – seekg(0) : set file pointer to the beginning of the file so that reading would start there

17 Cont. Specifying the Offset Two arguments – First argument represents an offset from a particular location in the file, – Second specifies the particular location – There are three possibilities for the second argument: beg is the beginning of the file, cur is the current pointer position, and end is the end of the file – seekp(-10, ios::end); – seekp(-sizeof(pers), ios::cur);

18

19 main() { person pers; ifstream infile; infile.open(“GROUP.DAT”, ios::in | ios::binary); } Example program infile.seekg(0, ios::end); int endposition = infile.tellg(); cout << “\nThere are “ << n << “ persons in file”; cout << “\nEnter person number: “; cin >> n; int position = (n-1) * sizeof(person); infile.seekg(position); infile.read( reinterpret_cast (&pers), sizeof(pers) ); pers.showData(); int n = endposition / sizeof(person); Go to program

20 File I/O with Member Functions Member function of a class can perform read and write operation in file Go to program

21