FILE HANDLING IN C++.

Slides:



Advertisements
Similar presentations
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.
Advertisements

File I/O. Stream Objects cin – the standard input stream - an object of the istream class, cout – the standard output stream – an object of the ostream.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
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.
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.
Chapter 3: Input/Output
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.
Program Input and the Software Design Process ROBERT REAVES.
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.
Fundamental Programming Command Line Parameters and File I/O.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
File I/O ifstreams and ofstreams Sections 11.1 &
Chapter 9 I/O Streams and Data Files
1 CS161 Introduction to Computer Science Topic #13.
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 1 ifstreams and ofstreams Sections 11.1 & 11.2.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
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.
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.
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.
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.
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.
Data File Handling in C++ Data File Handling in C++ Paritosh Srivastava PGT (Comp. Science) Kendriya VidyalayaJoshimath.
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.
Ms N Nashandi Dr SH Nggada 2016/01/03Ms N Nashandi and Dr SH Nggada1 Week 6 -File input and output in C++
Computer Programming II Lecture 9. Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني I/O and File management Concept of streams. cin and cout objects. C++stream classes. Unformatted I/O.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
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.
C ++ MULTIPLE CHOICE QUESTION
Basic Input and Output Operations
FILE HANDLING IN C++.
Basic File I/O and Stream Objects
17 File Processing.
files Dr. Bhargavi Goswami Department of Computer Science
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
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
File I/O in C++ I.
Data File Handling in C++
File I/O in C++ I.
Presentation transcript:

FILE HANDLING IN C++

Files (Streams) Files are used to store data in a relatively permanent form, on floppy disk, hard disk, tape or other form of secondary storage. Files can hold huge amounts of data if need be. Ordinary variables (even records and arrays) are kept in main memory which is temporary and rather limited in size. The following is a comparison of the two types of storage:

Main memory Secondary memory 1. Made up of RAM chips. 1. Usually a disk drive (or magnetic tape). 2. Used to hold a program when it is running, including the values of its variables (whether integer, char, an array, etc.) 2. Used to hold files (where a file can contain data, a program, text, etc.) 3. Can only hold relatively small amounts of data. 3. Can hold rather large amounts of data. 4. Is temporary (as soon as the program is done or the power goes out all of these values are gone). 4. Is fairly permanent. (A file remains even if the power goes out. It will last until you erase it, as long as the disk isn't damaged, at least.) 5. Gives fast access to the data (all electronic). 5. Access to the data is considerably slower (due to moving parts).

C++ STREAMS A Stream is a general name given to flow of data. Different streams are used to represent different kinds of data flow. Each stream is associated with a particular class, which contains member functions and definitions for dealing with that particular kind of data flow.

Flow of Data…. Data PROGRAM Input Stream Output >> Stream << DEVICES OR FILES (Extraction operator) (Insertion operator) Data istream class ostream class

The following classes in C++ have access to file input and output functions: ifstream ofstream fstream

C++ Stream classes Class Name Contents ios (general input/out stream classes) Contains basic facilities that are used by all other input and output classes.It also contains a pointer to a buffer object(streambuf object). istream (input stream) Inherits the properties of ios. Declares input functions such as get(). getline(), read(). Contains overloaded extraction operator >>. ostream (Output Stream) Inherits the properties of IOS. Declares output function such as put() and write(). Contains overloaded insertion operator <<. iostream (Input/Output stream) Inherits the properties of IOS stream and ostream through multiple inheritance and thus contains all the input and output functions. streambuf Provides an interface to physical devices through buffers.It acts as a base forfilebuf class used in files.

The Stream Class Hierarchy NOTE : UPWARD ARROWS INDICATE THE BASE CLASS ios ostream put() write() << istream get() getline() read() >> fstreambase iostream Ofstream Open() Tellp() Seekp() Ifstream Open() Tellg() Seekg() fstream

DIFFERENT FILE OPERATIONS OPENING A FILE CLOSING A FILE READING FROM A FILE WRITING ON A FILE CHECKING FOR END OF FILE

OPENING A FILE 1. By using the CONSTRUCTOR of the stream class. (Associating a stream with a file) 1. By using the CONSTRUCTOR of the stream class. ifstream transaction(“sales.dly”); ofstream result(“result.02”); 2. By using the open() function of the stream class ifstream transaction; transaction.open(“sales.dly”);

File Mode Parameters PARAMETER MEANING Ios::app Append to end-of file Ios::ate goto end of file on opening Ios::binary binary file Ios::in Open existing file for reading Ios::nocreate open fails if file doesn’t exist Ios::noreplace open fails if file already exists Ios::out creates new file for writing on Ios::trunc Deletes contents if it exists The mode can combine two or more modes using bit wise or ( | )

Checking For Successful File Opening ifstream transaction(“sales.dly”); if (transcation == NULL) { cout<<“unable to open sales.dly”; cin.get(); // waits for the operator to press any key exit(1); }

Closing of File Stream_name.close(); e.g., transaction.close();

Types of Files . The two basic types are text and binary. A text file consists of readable characters separated into lines by newline characters. (On most PCs, the newline character is actually represented by the two-character sequence of carriage return (ASCII 13), line feed (ASCII 10).

A binary file stores data to disk in the same form in which it is represented in main memory. If you ever try to edit a binary file containing numbers you will see that the numbers appear as nonsense characters. Not having to translate numbers into a readable form makes binary files somewhat more efficient. Binary files also do not normally use anything to separate the data into lines. Such a file is just a stream of data with nothing in particular to separate components.

When using a binary file we write whole record data to the file at once. When using a text file, we write out separately each of the pieces of data about a given record. The text file will be readable by an editor, but the numbers in the binary file will not be readable in this way. The programs to create the data files will differ in how they open the file and in how they write to the file.

For the binary file we will use write to write to the file, whereas for the text file we will use the usual output operator(<<) and will output each of the pieces of the record separately. With the binary file we will use the read function to read a whole record, but with the text file we will read each of the pieces of record from the file separately, using the usual input operator(>>)

EXAMPLES Creation of a text file

Types of File Access : Sequential access. With this type of file access one must read the data in order, much like with a tape, whether the data is really stored on tape or not. Random access (or direct access). This type of file access lets you jump to any location in the file, then to any other, etc., all in a reasonable amount of time.

FILE POINTERS

FILE POINTERS Each file object has two integer values associated with it : get pointer put pointer These values specify the byte number in the file where reading or writing will take place.

File pointers….. By default reading pointer is set at the beginning and writing pointer is set at the end (when you open file in ios::app mode) There are times when you must take control of the file pointers yourself so that you can read from and write to an arbitrary location in the file.

Functions associated with file pointers : The seekg() and tellg() functions allow you to set and examine the get pointer. The seekp() and tellp() functions allow you to set and examine the put pointer.

seekg() function : With one argument : seekg(k) where k is absolute position from the beginning. The start of the file is byte 0 End File Begin k bytes ^ File pointer The seekg() function with one argument

The seekg() function with two argument With two arguments : the first argument represents an offset from a particular location in the file. the second specifies the location from which the offset is measured. Begin End ^ Offset from Begin The seekg() function with two argument

The seekg() function with two argument With two arguments : Begin End ^ Offset from Begin ^ Offset from end ^ Offset from current position The seekg() function with two argument

// #include <fstream.h> #include <conio.h> #include <stdio.h> void main() { //clrscr(); char c,d,ans; char str[80]; ofstream outfl("try.txt"),out("cod.dat"); ifstream infl; do { cout<<"please give the string : "; gets(str); outfl<<str; cout <<"do you want to write more...<y/n> : "; ans=getch(); } while(ans=='y'); outfl<<'\0'; outfl.close(); //clrscr(); getch(); cout <<"reading from created file \n"; infl.open("try.txt"); out.open("cod.dat"); //********************************** c=infl.get(); do { d=c+1; cout<<c<<d<<'\n'; out.put(d); c= infl.get(); } while (c!='\0'); out<<'\0'; infl.close(); outfl.close(); //*********************************