DATA FILE HANDLING. Contents to be covered in class  Concept of files & streams  Stream class hierarchy  File handling functions  Text V/S Binary.

Slides:



Advertisements
Similar presentations
Chapter 10 Input/Output Streams
Advertisements

By Sumit Kumar Gupta PGT(CS) KV NO.1, 1st Shift, BBSR.
VINAY ALEXANDER PGT(CS) KV,SECL,JHAGRAKHAND. FILE :A file itself is a bunch of bytes stored on some storage device like tape, or magnetic disk etc. STREAM.
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.
1 Lecture 31 Handling Selected Topics Again!! File I/O Special Lectures.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
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.
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.
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.
Dale Roberts 1 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
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 &
FILE HANDLING IN C++.
Topics 1.File Basics 2.Output Formatting 3.Passing File Stream Objects to Functions 4.More Detailed Error Testing 5.Member Functions for Reading and 6.Writing.
Programming Principles II Lecture Notes 7 Files Andreas Savva.
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.
Protectedly Inherited Base Class  When the access specifier of the base class in the derived class definition is protected, the base class is protectedly.
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.
Starting Out with C++, 3 rd Edition Basic file operations in C++ Dr. Ahmed Telba.
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 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.
Data File Handling in C++ Data File Handling in C++ Paritosh Srivastava PGT (Comp. Science) Kendriya VidyalayaJoshimath.
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++
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.
 Data Streams  Numeric Output  Numeric Input  Multiple Numeric Output  Multiple Numeric Input  Character Output  Character Input  String Output.
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 member functions,
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 ++
ifstreams and ofstreams
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Basic file operations in C++
Tutorial4us.com File Handling in C++ Tutorial4us.com.
Basic Input and Output Operations
FILE HANDLING IN C++.
File I/O with Records Lesson xx
Basic File I/O and Stream Objects
files Dr. Bhargavi Goswami Department of Computer Science
Sequential input and output Operations in file
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
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Sindhu S PGT Comp.Sc. K V Kollam
File I/O in C++ I.
File I/O.
Data File Handling in C++
C++ Programming: chapter 6 – iostream
Data File Handling RITIKA SHARMA.
ifstreams and ofstreams
File I/O in C++ II.
File I/O in C++ I.
Input/Output Streams, Part 2
Presentation transcript:

DATA FILE HANDLING

Contents to be covered in class  Concept of files & streams  Stream class hierarchy  File handling functions  Text V/S Binary Data Files  Opening & Closing Files  File Modes  Some Programming Examples  Searching in Binary Files

FILE HANDLING IN C++ Most computer program work with files. This is because files helps in storing Data permanently. A file is a collection of related data stored on a storage device. In C++ file operations make use of streams which provides Interface between programs and the files.

STREAM A Stream is general name given to a flow of data. Different streams are used to represent different kinds of data flow. Each stream is associated with particular Class.

Input Stream The stream that supplies data to the program is known as Input Stream. It reads data from file and hand over data to the program.

Output Stream The stream that receives data from the program is known as output Stream. It writes the received data to the file.

Each stream is associated with a particular class. CLASSPURPOSE ifstreamIt is input file stream, it provides input operations for file. ofstreamIt is output file stream, it provides output operations for file. fstreamIt is input and output file stream. It provides support simultaneous Input and Output operations.

TYPES OF FILES IN C++ TEXT FILES BINARY FILES

TEXT FILES TEXT FILES : A text files stores data in ascii format. In text file each line is terminated with a special character known as EOL character. In text files some Internal translation takes place when this EOL character is read and written.

BINARY FILES BINARY FILES : A binary file stores data in binary format and no delimiters is used for a line. Also no Translation occurs in binary files. As a result, binary files are faster and easier for a program to read and write than the text files.

STEPS TO PROCESS A FILE 1. Determine the type of link required. 2. Declare a stream for the desired types of link. 3. Attach the desired file to the declared the stream. 4. Processing a file. 5. Close the file link with stream.

Opening & Closing Files  Two Methods for opening files  (i) Using Constructors ( When Single file is to be used ) Eg. ifstream ip_file(“xyz.dat”) – For reading from file ip_file>>ch; ofstream op_file(“pqr.dat”) – For writing to file op_file.put(ch);  (ii) Using Open() function (For Multiple files) ifstream fin; fin.open(“abc.dat”); fin.close(); fin.open(“xyz.dat”); --Multiple Files fin.close(); 

File Modes  These are used to access a file as required Eg. Ios::in, ios::out, ios::ate, ios::app, ios::binary etc. Syntax: fout.open(“abc.dat”, ios::in) To concatenate two or more modes: ios::app|ios::nocreate|ios::binary)

iostream.h file fstream.h file IOS ISTREAM STREAMBUF OSTREAM IFSTREAM IOSTREAM FSTREAM OFSTREAM FSTREAMBASE FILEBUF Stream Class Hierarchy

/* program to write data in a text file */ #include void main() { char ans='y'; char name[20],add[20]; ofstream fout("student.dat"); clrscr(); while(ans=='y') { cout<<"Enter name and address:"<<endl; cin>>name>>add; fout<<name<<add<<endl; cout<<"Do you want to add another record:"; cin>>ans; } fout.close(); }

/* program to read data from a file */ #include void main() { char name[20],add[20]; ifstream fin("student.dat"); clrscr(); while(1) { fin>>name>>add; cout<<name<<add<<endl; if(fin.eof()) break; } fin.close(); }

/* program to write and read data from a file */ #include void main() { char string[80],ch; int len; fstream file; file.open(“string.dat”,ios::out| ios::in); clrscr(); cout<<“Please enter the String:”; gets(string); len=strlen(string); // logic to write data in a file for(int i=0,i<=len-1;i++) { file.put(string[i]); }

// logic to read data from a file file.seekg(0); while(1) { file.get(ch); if(file.eof()) break; cout<<ch; } file.close(); getch(); }

The read() & write() functions using structures #include struct customer {char name[]; float balance; }; void main() { clrscr(); customer c1; strcpy(c1.name, “Ajay Jain”); c1.balance= ; ofstream fout; fout.open(“account”,ios::out| ios::binary); if(!fout) { cout<<“\n File can’t be open”; return 1;} fout.write((char *) & c1, sizeof(customer)); fout.close(); ifstream fin; fin.open(“account”,ios::out| ios::binary); fin.read((char *) & c1, sizeof(customer)); cout<<c1.name<<“ has the balance amount of Rs. ”<< c1.balance; fout.close(); }

Reading & writing class objects #include class Student {char name[10]; float marks; public: void getdata() {char ch; cin.get(ch); cout<<“\n Enter name: ”; cin.getline(name, 40); cout >marks; cout<<endl; } void display() { cout<< name <<“\t”<<marks<<“\n”; } };

void main() { clrscr(); Student S[2]; fstream file; file.open(“student.dat”,ios::in| ios::out); if(!file) { cout<<“\n File can’t be open”; return 1;} cout<<“\n Enter details of students \n ”; for(int i=0;i<2;i++) {s[i].getdata(); file.write((char *) & S1[i], sizeof(S[i])); } file.seekg(); cout<<“\n The contents of student.dat are : \n”; for(int i=0;i<2;i++) {file.read((char *) & S1[i], sizeof(S[i])); s[i].display(); } fout.close(); }

THANKS & HAVE A NICE DAY