Ms N Nashandi Dr SH Nggada 2016/01/03Ms N Nashandi and Dr SH Nggada1 Week 6 -File input and output in C++

Slides:



Advertisements
Similar presentations
By Sumit Kumar Gupta PGT(CS) KV NO.1, 1st Shift, BBSR.
Advertisements

File I/O in C++. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk);is stored on a secondary storage device.
FILE OPEN MODES File open modes n ios::app n ios::ate n ios::binary n ios::in n ios::out n ios::trunc n ios::nocreate n ios::noreplace.
Copyright © 2012 Pearson Education, Inc. Chapter 12: Advanced File Operations.
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.
DATA FILE HANDLING. Contents to be covered in class  Concept of files & streams  Stream class hierarchy  File handling functions  Text V/S Binary.
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.
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 stream.
Darbas su failais Arnas Terekas IT 1gr. Vilniaus universitetas Matematikos ir informatikos fakultetas.
Dale Roberts 1 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
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.
FILE HANDLING IN C++.
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.
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.
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.
C++ FILE I/O.
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.
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.
File Handling. Read data from file and display it on screen #include int main() { /* fin object is created with parameterzied constructor */ ifstream.
1 CSC241: Object Oriented Programming Lecture No 32.
File Handling in C++.
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.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
Learners Support Publications Working with Files.
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)
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 in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
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.
Data File Handling in C++ Data File Handling in C++ Paritosh Srivastava PGT (Comp. Science) Kendriya VidyalayaJoshimath.
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.
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.
Tutorial4us.com File Handling in C++ Tutorial4us.com.
Data File Handling in C++
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.
C ++ MULTIPLE CHOICE QUESTION
FILE HANDLING IN C++.
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.
Sindhu S PGT Comp.Sc. K V Kollam
Data File Handling in C++
CHAPTER 4 File Processing.
Data File Handling RITIKA SHARMA.
File I/O in C++ II.
Input/Output Streams, Part 2
Presentation transcript:

Ms N Nashandi Dr SH Nggada 2016/01/03Ms N Nashandi and Dr SH Nggada1 Week 6 -File input and output in C++

Outline This week we will be covering: Introduction to data file handling in C++ Classes for file stream operation Opening a file Closing File ERROR HANDLING FUNCTION File pointers and their manipulation 2016/01/03Ms N Nashandi and Dr SH Nggada2

Introduction to data file handling in C++ File -The information / data stored under a specific name on a storage device, is called a file. Stream- refers to a sequence of bytes. Text file - is a file that stores information in ASCII characters. In text files, each line of text is terminated with a special character known as EOL (End of Line) character or delimiter character. When this EOL character is read or written, certain internal translations take place. Binary file -is a file that contains information in the same format as it is held in memory. In binary files, no delimiters are used for a line and no translations occur here. 2016/01/03Ms N Nashandi and Dr SH Nggada3

Classes for file stream operation ofstream: Stream class to write on files ifstream: Stream class to read from files fstream: Stream class to both read and write from/to files. NB: To perform file processing in C++, header files and must be included in your C++ source file. 2016/01/03Ms N Nashandi and Dr SH Nggada4

Opening a file in C++ To open a file one could either use a constructor or an open function. OPENING FILE USING CONSTRUCTOR ofstream outFile("sample.txt"); //output only ifstream inFile(“sample.txt”); //input only 2016/01/03Ms N Nashandi and Dr SH Nggada5

Opening a file using open() function open() function is a member of fstream, ifstream, and ofstream objects. standard syntax for open() function: Stream-object.open(“filename”, mode); The first argument specifies the name and location of the file to be opened and the second argument of the open() member function defines the mode in which the file should be opened. 2016/01/03Ms N Nashandi and Dr SH Nggada6

Examples using open() function o ofstream outFile; outFile.open("sample.txt"); o ifstream inFile; inFile.open("sample.txt"); 2016/01/03Ms N Nashandi and Dr SH Nggada7

File could be open in different modes specified by open() function File mode parameterMeaning ios::appAppend to end of file ios::ate go to end of file on opening ios::binaryfile open in binary mode ios::inopen file for reading only ios::outopen file for writing only ios::nocreate open fails if the file does not exist ios::noreplace open fails if the file already exist ios::trunc delete the contents of the file if it exist 2016/01/03Ms N Nashandi and Dr SH Nggada8 All these flags can be combined using the bitwise operator OR (|). For example, if we want to open the file example.bin in binary mode to add data we could do it by the following call to member function open(): fstream file; file.open("example.bin", ios::out | ios::app | ios::binary);

Closing file When a C++ program terminates it automatically closes flushes all the streams, release all the allocated memory and close all the opened files. It is a good practice that a programmer should close all the opened files before program termination. void close() - standard syntax function, which is a member of fstream, ifstream, and ofstream objects. E:g  outFile.close();  inFile.close(); 2016/01/03Ms N Nashandi and Dr SH Nggada9

Input and Output Operation: put() and get() function put() -writes a single character to the associated stream. get() - reads a single character form the associated stream. Examples: file.get(ch); file.put(ch); 2016/01/03Ms N Nashandi and Dr SH Nggada10

Write() and Read() Function write() and read() functions write and read blocks of binary data. example: file.read((char *)&obj, sizeof(obj)); file.write((char *)&obj, sizeof(obj)); 2016/01/03Ms N Nashandi and Dr SH Nggada11

ERROR HANDLING FUNCTION FUNCTIONRETURN VALUE AND MEANING eof() returns true (non zero) if end of file is encountered while reading; otherwise return false(zero) fail() return true when an input or output operation has failed bad() returns true if an invalid operation is attempted or any unrecoverable error has occurred. good()returns true if no error has occurred. 2016/01/03Ms N Nashandi and Dr SH Nggada12

File Pointers And Their Manipulation All i/o streams objects have, at least, one internal stream pointer: ifstream, like istream, has a pointer known as the get pointer that points to the element to be read in the next input operation. ofstream, like ostream, has a pointer known as the put pointer that points to the location where the next element has to be written. fstream, inherits both, the get and the put pointers 2016/01/03Ms N Nashandi and Dr SH Nggada13

File Pointers(Cont.) These internal stream pointers that point to the reading or writing locations within a stream can be manipulated using the following member functions: 2016/01/03Ms N Nashandi and Dr SH Nggada14 seekg() moves get pointer(input) to a specified location seekp() moves put pointer (output) to a specified location tellg() gives the current position of the get pointer tellp() gives the current position of the put pointer

Home Work Write a C++ program to count the total occurrence of words ”he” in a text file ”story.txt”(ignore the case of the words) 2016/01/03Ms N Nashandi and Dr SH Nggada15

END OF THE LECTURE THANK YOU!! 2016/01/03Ms N Nashandi and Dr SH Nggada16