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.

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 I/O. COMP104 File I/O / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
計算機概論實習 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.
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.
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.
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++.
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.
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.
“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.
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.
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
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.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
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.
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
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)
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
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.
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++
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.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
CS212: Object Oriented Analysis and Design
Programming with ANSI C ++
Tutorial4us.com File Handling in C++ Tutorial4us.com.
C ++ MULTIPLE CHOICE QUESTION
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
Today’s Lecture I/O Streams Tools for File I/O
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++
Data File Handling RITIKA SHARMA.
File I/O in C++ II.
File I/O in C++ I.
Input/Output Streams, Part 2
Presentation transcript:

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 The bytes are transferred from main memory to a device such as (display screen, printer, disk, n/w…)

3 Files Stream C++ contains different pre defined stream that automatically opened when a program begins its execution. Such as cin, cout …. Cin represents input stream connect to input devices (object of istream) Cout represents output stream connect to output devices (object of ostream)

4 Files Opening and Closing a File In C++, to open a file by linking it to a stream. There are three types of streams: input, output, and input/output. To create an input stream, declare the stream to be of class ifstream. To create an output stream, declare it as class ofstream. Streams that will be performing both input and output operations must be declared as class fstream. For example, this fragment creates one input stream, one output stream, and one stream capable of both input and output: ifstream in; // input ofstream out; // output fstream io; // input and output

5 Files A file can be opened in two ways 1)Using constructor function 2)Using the member function open() First method is useful when we se only one file Second method used to manage multiple files

6 Files File opening using constructor Constructor -> used to initialized an object. Here filename is used to initialize the file stream This can be done 1)Create a file stream object to manage the stream (ie ofstream or from ifstream) 2)Initialize the file object with filename Eg: ofstream o (“popo.txt”); open a file named popo.txt for output

7 Files ofstream o (“popo.txt”); This create o as an ofstream object, to manage output stream. This statement opene the file popo.txt an attaches to output stream Input stream example Ifstream i(“popo.txt”);

8 Files To close the file o.close(); i.close();

9 Files File opening using constructor #include void main() { ofstream o("popo.txt"); cout<<"Enter"; char name[20]; cin>>name; o<<name; o.close(); ifstream i("popo.txt"); i>>name; cout<<name; i.close(); }

10 Files 2) Using the member function open() Open() fun is used to open multiple files Syn File_stream_class stream object; Stream object.open(“file name”); Eg Ofstream outfile; Outfile.open(“popo.txt”); Outfile.close(); Outfile.open(“check.txt”); Outfile.close();

11 Files getline( ) Function that performs input is getline(). It is a member of each input stream class. getline(char array, streamsize num); getline(char array, streamsize num, char delim); The first form reads characters into the array characters have been read, a newline character has been found, or the end of the file has been encountered. The second form reads characters into the array the character specified by delim has been found, or the end of the file has been encountered. If the delimiter character is encountered in the input stream, it is extracted, but is not put into array.

12 Files #include void main() { ofstream fout; fout.open("popo.txt"); fout<<"popo"; fout<<"peepe"; fout.close(); fout.open("check.txt"); fout<<"looky"; fout<<"zappy"; fout.close(); ifstream fin; fin.open("popo.txt"); char s[20]; while(!fin.eof()) { fin.getline(s,20); cout<<s<<"\n"; } fin.close(); fin.open(“check.txt"); while(!fin.eof()) { fin.getline(s,20); cout<<s<<"\n"; } fin.close(); getch(); }

13 Files Copy one file to another #include void main() { ofstream fout; ifstream fin; fout.open("popo.txt"); char s[20]; int i; for(i=1;i<=10;i++) { fout<<"popo"<<i<<"\n"; } fout.close(); fout.open("check.txt"); fin.open("popo.txt"); while(!fin.eof()) { fin.getline(s,20); fout<<s<<"\n"; } fin.close(); fout.close(); getch(); }

14 Files The open() function  to create new file/ open existing file Open() fun can take two arguments file name and file mode Open(“filename”,mode); filename is the name of the file; it can include a path specifier. The value of mode determines how the file is opened. It must be one or more of the following ios::app ios::ate ios::binary ios::in ios::out ios::trunc combine two or more of these values by ORing them together

15 Files ofstream out; out.open("test", ios::out); ParameterMeaning iios::appAppend to EOF iios::ategoto EOF on opening iios::binaryBinary file iios::inopen file for reading only iios::outopen file for writing only ios::truncdelete the contents of the file if it exist iios::nocreateopen fails if the file does not exist iios::noreplaceopen fails if the file already exist

16 Files File pointers Each files having 2 pointers 1)Input pointer (get pointer) 2)Output pointer (put pointer) Get pointer  used to reading contents of given file location Output pointer  for writing to a given file location Both pointer increments automatically while reading and writing

17 Files When a file open in read mode  get pointer points to BOF When a file open in write mode  put pointer points to BOF and delete existing contents When a file open in append mode  put pointer points to the EOF The file stream class support different functions to control the movements of the get and put pointers

18 Files Ofstream fout; Fout.open(“check.txt”,ios::app); Cout<<“put pointer position”<<fout.tellp(); Seekg()& Seekp() Seekg()Moves get pointer to a specified location Seekp()Moves put pointer to a specified location Tellg()Gives the current position of get pointer Tellp()Gives the current position of put pointer Seekg(offset,position); Seekp(offset,position);

19 Files Offset  no of bytes to move Position takes 3 values Eg: fout.seekg(0,ios::beg);  Goto Start Fout.seekg(2,ios::cur);  move 2 bytes forward from current position Fout.seekg(-2,ios::end);  move 2 bytes backword from EOF ios::begfrom BOF ios::curFrom current position ios::endFrom EOF

20 Files Seekg() #include void main() { ofstream fout; ifstream fin; fout.open("popo.txt",ios::out); char s[20]; int i; for(i=1;i<=10;i++) fout<<"popo"<<i<<"\n"; fout.close(); fin.open("popo.txt",ios::in); while(!fin.eof()) { fin.seekg(2,ios::cur); fin.getline(s,20); cout<<s<<"\n"; } fin.close(); getch(); }

21 Files put( ) and get( ) get() willread a character and put() will write a character. #include void main() { fstream file; file.open("p.txt",ios::out|ios::in); int i;char s[20]="popo and peepe",c; for(i=0;i<=10;i++) file.put(s[i]); file.seekg(0,ios::beg); while(!file.eof()) {file.get(c); cout<<c; } file.close(); getch(); }

22 Files Read() and write() For reading and writing block of data Syn Infile.read((char *) & v,sizeof(v)); outfile.write((char *) & v,sizeof(v));

23 Files #include class check { char name[20]; int m1,m2,m3,tot; public: void getdata() { cout<<"Enter name and 3 marks"; cin>>name>>m1>>m2>>m3; tot=m1+m2+m3; } void putdata() { cout<<"\n"<<name<<" "<<m1<<" "<<m2<<" "<<m3; } }; void main() { clrscr(); check c[20]; int i,n; cout >n; fstream file("popo.txt",ios::in|ios::out); for(i=0;i<n;i++) { c[i].getdata(); file.write((char*)& c[i],sizeof(c[i])); } file.seekg(0,ios::beg); for(i=0;i<n;i++) { file.read((char *) & c[i],sizeof(c[i])); c[i].putdata(); } file.close(); getch(); }