File Handling. Read data from file and display it on screen #include int main() { /* fin object is created with parameterzied constructor */ ifstream.

Slides:



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

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.
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.
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.
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.
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.
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.
File input and output in C++ Dr. Ahmed Telba. CSE202: Lecture 9The Ohio State University2 Reading & Writing to Files.
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.
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.
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
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Member Functions for Reading and Writing Files 12.5.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Starting Out with C++, 3 rd Edition Basic file operations in C++ Dr. Ahmed Telba.
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.
1 CSC241: Object Oriented Programming Lecture No 32.
File Handling in C++.
 Random access vs. sequential access  Current position pointer (CP)  Record numbers  seekg command  tellg command  Program using random access.
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.
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)
KBD Program with file I/O Operation File Disk Program with file I/O Operation Screen Cin Cout.
File Handling. Read data from file and display it on screen #include int main() { /* fin object is created with parameterzied constructor */ ifstream.
Ms N Nashandi Dr SH Nggada 2016/01/03Ms N Nashandi and Dr SH Nggada1 Week 6 -File input and output in C++
CS212: Object Oriented Analysis and Design
Chapter 14: Sequential Access Files
Programming with ANSI C ++
17 File Processing.
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Chapter 13: Advanced File and I/O Operations
Tutorial4us.com File Handling in C++ Tutorial4us.com.
C ++ MULTIPLE CHOICE QUESTION
CISC/CMPE320 - Prof. McLeod
CPSC 231 D.H. C++ File Processing
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? how is.
FILE HANDLING IN C++.
آشنایی با ساختارها و کار با فایلها
files Dr. Bhargavi Goswami Department of Computer Science
Chapter 13: Advanced File and I/O Operations
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.
C++ FileIO pepper.
Topics Input and Output Streams More Detailed Error Testing
Chapter 12: Advanced File Operations.
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.
Data File Handling in C++
C++ Programming: chapter 6 – iostream
Data File Handling RITIKA SHARMA.
C++ Programming Lecture 8 File Processing
Presentation transcript:

File Handling

Read data from file and display it on screen #include int main() { /* fin object is created with parameterzied constructor */ ifstream fin("in.txt"); //file will be searched in current directory char ch; while(fin.eof() != 0) { fin>>ch; cout<<ch; } return 0; }

Read data from file and display it on screen #include int main() { ifstream fin(“c:\\in.txt"); //give full path of file char ch; while(fin.eof() != 0) { fin>>ch; cout<<ch; } return 0; }

Read data from file and display it on screen #include int main() { /* fin is created with default constructor so use open() to open file */ ifstream fin; fin.open(“in.txt”); char ch; while(fin.eof() != 0) { fin>>ch; cout<<ch; } return 0; }

Read data from one file and save it on two different file depending on condition #include int main() { ofstream fout1,fout2; fout1.open("alpha.txt"); fout2.open("digits.txt"); ifstream fin; fin.open("in.txt"); char ch; while(fin) { fin>>ch; if(ch >=65 && ch <=124) fout1<<ch; else if(ch >=48 && ch <=57) fout2<<ch; cout<<ch; } fin.close(); fout1.close(); fout2.close(); return 0; }

Take data from user store it in file again read data from file and display it on screen. #include int main() { fstream f; f.open("item.txt",ios::out); char name[20]; float cost; cout<<"\n Enter item name"; cin>>name; f<<name<<"\n"; cout<<"\n Enter cost"; cin>>cost; f<<cost<<"\n"; f.close(); f.open("item.txt",ios::in); f>>name; f>>cost; cout<<"\n Item name :"; cout<<name; cout<<"\n Cost :"; cout<<cost; f.close(); return 0; }

Use of getline function #include int main() { ifstream fin("item.txt"); char line[80]; while(fin) { fin.getline(line,80); cout<<endl<<line; } return 0; }

Functions for manipulation of file pointers seekg() Moves get pointer to a specified location seekp() Moves put pointer to a specified location. tellg() Gives the current position of the get pointer tellp() Gives the current position of the put pointer E.g. fin.seekg(10); int p = fout.tellp();

Example of seekg() int main() { fstream f; f.open("item.txt",ios::in | ios::out); char name[20]; float cost; cout<<"\n Enter item name"; cin>>name; f<<name<<"\n"; cout<<"\n Enter cost"; cin>>cost; f<<cost<<"\n"; /*moves get pointer to 1 st location*/ f.seekg(0); f>>name; f>>cost; cout<<"\n Item name :"; cout<<name; cout<<"\n Cost :"; cout<<cost; return 0; }

Example of seekg() int main() { fstream f; f.open("item.txt",ios::in | ios::out); char name[20]; float cost; cout<<"\n Enter item name"; cin>>name; f<<name<<"\n"; cout<<"\n Enter cost"; cin>>cost; f<<cost<<"\n"; /*moves get pointer to 3 rd location*/ f.seekg(3); f>>name; f>>cost; cout<<"\n Item name :"; cout<<name; cout<<"\n Cost :"; cout<<cost; return 0; }

Functions with offset seekg(offset,refposition) Seekp(offset,refposition) Offset represents number of bytes the file pointer is to be moved from the location specified by the parameter refposiotion. Refposition takes one of the form specified in ios class (constant) ios::beg beginning of file ios::cur cureent position of the pointer ios::end end of the file

Pointer offset calls Seek callsAction fout.seekg(0,ios::beg)Go to start Fout.seekg(0,ios::cur)Stay at the current position Fout.seekg(0,ios::end)Go to end of the file fout,.seekg(m,ios::beg)Moves to (m+1) th byte in the file Fout.seekg(m,ios::cur)Go forward by m bytes from current posiotion Fout.seekg(-m,ios::cur)Go backward by m bytes from the current position Fout.seekg(-m,ios::end)Go backward by m bytes from end of the file

Reading and Writing Class object read() is used to read object data from file Syntax read( (char *) &obj_name, sizeof(obj_name)) write() is used to write object data to the file Syntax write((char *) &obj_name, sizeof(obj_name))

Example #include class item { char name[20]; float cost; public: void readdata(); void writedata(); }; void item::readdata() { cout >name; cout >cost; } void item::writedata() { cout<<setiosflags(ios::left)<<setw(10)<<name; cout<<endl<<setprecision(2)<<cost; } int main() { item ob[3]; fstream f; f.open("stock.dat",ios::in | ios::out); cout<<"\n enter details for items"; for(int i=0;i<3;i++) { ob[i].readdata(); f.write( (char *) &ob[i],sizeof(ob[i])); } cout<<"\n Output : "; for(i=0;i<3;i++) { f.read((char*) &ob[i], sizeof(ob[i])); ob[i].writedata(); } return 0; }

Error handling during file operations eof() Returns true if end of file encountered fail() Returns true when input or output operation has failed bad() Returns true invalid option is attempted(used for fatal errors) good() Returns true if no error has occurred.

Command line Argument