SlideShare a Scribd company logo
1 of 6
Download to read offline
4/19/2011
1
File and Streams
W14L26
CHAPTER REVIEW
 The largest part of this chapter is devoted to showing
how to perform file-related activities using C++
streams.
 We’ll show how to read and write data to files in a
variety of ways, how to handle errors, and how files
and OOP are related.
 C programmers may wonder what advantages there
are to using the stream classes for I/O, instead
 of traditional C functions such as printf() and scanf(),
and—for files—fprintf(), fscanf(), and so on.
STREAM CLASSES
 A stream is a general name given to a flow of data. In
C++ a stream is represented by an object of a particular
class. So far we’ve used the cin and cout stream objects.
 Different streams are used to represent different kinds
of data flow. For example, the ifstream class represents
data flow from input disk files.
 Advantages of Streams
 Simplicity: no %d formatting character since each
object already knows how to display itself.
 Another reason is that you can overload existing
operators and functions, such as the insertion (<<) and
extraction (>>) operators, to work with classes that you
create.
STREAM I/O LIBRARY HEADER FILES
 C++ views each files as a sequence of bytes.
 Each file ends with an end-of-file marker.
 When a file is opened, an object is created and a
stream is associated with the object.
 iostream -- contains basic information required for all
stream I/O operations
 iomanip -- contains information useful for performing
formatted I/O with parameterized stream
manipulators
 fstream -- contains information for performing file I/O
operations
 strstream -- contains information for performing in-
memory I/O operations (i.e., into or from strings in
memory)
THE STREAM CLASS HIERARCHY
THE STREAM CLASS HIERARCHY
 We’ve already made extensive use of some stream
classes.
 The extraction operator >> is a member of the istream
class, and the insertion operator << is a member of the
ostream class.
 Both of these classes are derived from the ios class.
 The cout object, representing the standard output
stream, which is usually directed to the video display, is
a predefined object of the ostream_withassign class,
which is derived from the ostream class.
 Similarly cin is an object of the istream_withassign
class, which is derived from istream.
 The classes used for input and output to the video
display and keyboard are declared in the header file
IOSTREAM,
4/19/2011
2
IN SHORT
 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.
 These classes are derived directly or indirectly from the classes istream,
and ostream.
 We have already used objects whose types were these classes: cin is an
object of class istream and cout is an object of class ostream.
 Therfore, we have already been using classes that are related to our
file streams. And in fact, we can use our file streams the same way we
are already used to use cin and cout, with the only difference that we
have to associate these streams with physical files. Let's see an
example:
SIMPLE PROGRAM
 // basic file operations
 #include <iostream>
 #include <fstream>
 using namespace std;
 int main () {
 ofstream myfile;
 myfile.open ("example.txt");
 myfile << "Writing this to a file.n";
myfile.close();
 return 0; }
THE IOS CLASS
 The ios class is the granddaddy of all the stream classes, and
contains the majority of the features you need to operate C++
streams. The three most mportant features are,
 Formatting Flags
 Formatting flags are a set of enum definitions in ios. They act as
on/off switches that specify choices for various aspects of input and
output format and operation.
 cout.setf(ios::left); // left justify output text
 cout >> “This text is left-justified”;
 cout.unsetf(ios::left); // return to default (right justified)
Formatting Flags
 Flag Meaning
 skipws Skip (ignore) whitespace on input
 left Left adjust output [12.34 ]
 right Right adjust output [ 12.34]
 internal Use padding between sign or base indicator and number [+
12.34]
 dec Convert to decimal
 oct Convert to octal
 Hex Convert to hexadecimal
 boolalpha Convert bool to “true” or “false” strings
 showbase Use base indicator on output (0 for octal, 0x for hex)
 showpoint Show decimal point on output
 Uppercase Use uppercase X, E, and hex output letters (ABCDEF)—the default is lowercase
 showpos Display + before positive integers
 scientific Use exponential format on floating-point output [9.1234E2]
 fixed Use fixed format on floating-point output [912.34]
 unitbuf Flush all streams after insertion
 stdio Flush stdout, stderror after insertion
MANIPULATORS
 cout << “To each his own.” << endl;
 cout << setiosflags(ios::fixed) // use fixed decimal point
 << setiosflags(ios::showpoint) // always show decimal point
 No-Argument ios Manipulators
 Ws, dec,oct, hex, endl ,ends, flush, lock, unlock
 cout << hex << var;
 ios Manipulators with Arguments
 Manipulator Argument
 setw() field width (int)
 setfill() fill character (int)
 setprecision() precision (int)
 setiosflags() formatting flags (long)
 resetiosflags() formatting flags (long)
FUNCTIONS
 ch = fill(); fill(ch); p = precision(); precision(p); w = width();
 width(w); setf(flags); unsetf(flags); setf(flags, field);
 cout.width(14); cout.fill(‘*’);
4/19/2011
3
THE ISTREAM CLASS (SEE YOUR BOOK)
 The istream class, which is derived from ios, performs input-
specific activities, or extraction.
 Istrea
 Function
 >>
 get(ch);
 get(str)
 get(str, MAX)
 get(str, DELIM)
 get(str, MAX, DELIM)
 getline(str, MAX, DELIM)
 putback(ch)
 ignore(MAX,
 peek(ch)m Functions
THE OSTREAM CLASS
 The ostream class handles output or insertion
activities.
 Function
 <<
 put(ch)
 flush()
 write(str, SIZE)
 seekp(position)
 seekp(position, seek_dir)
 pos = tellp()
STREAM ERRORS
 cin >> var; What happens if a user enters the string
“nine” instead of the integer 9, or pushes the [Enter] key
without entering anything? Or what happens if there’s a
hardware failure?
 Some tables are given in your book please see to
understand different types of error.
DISK FILE I/O WITH STREAMS
 Most programs need to save data to disk files and read it back in.
Working with disk files requires another set of classes: ifstream for
input, fstream for both input and output, and ofstream for output.
 Objects of these classes can be associated with disk files, and we can
use their member functions to read and write to the files.
 Formatted File I/O
 In formatted I/O, numbers are stored on disk as a series of
characters. Thus 6.02, rather than being stored as a 4-byte type
float or an 8-byte type double, is stored as the characters ‘6’, ‘.’, ‘0’, and
‘2’. This can be inefficient for numbers with many digits, but it’s
appropriate in many situations and easy to implement.
Characters and strings are stored more or less normally.
WRITING DATA
 #include <fstream.h> //for file I/O
 #include <iostream.h>
 #include<stdio.h>
 #include<conio.h>
 int main()
 {
 char ch = 'x';
 int j = 77;
 double d = 6.02;
 char str1[] = “Amar"; //strings without
 char str2[] = “Sara";
 ofstream outfile("fdata.txt"); //create ofstream object
 outfile << ch //insert (write) data
 << j
 << ' ' //needs space between numbers
 << d
 << str1
 << ' ' //needs spaces between strings
 << str2;
 cout << "File writtenn";
 getch();
 }
READING DATA
 #include <fstream.h> //for file I/O
 #include <iostream.h>
 #include<stdio.h>
 #include<conio.h>
 int main()
 {
 char ch;
 int j;
 double d;
 string str1;
 string str2;
 ifstream infile(“fdata.txt”); //create ifstream object //extract (read) data from it
 infile >> ch >> j >> d >> str1 >> str2;
 cout << ch << endl //display the data
 << j << endl
 << d << endl
 << str1 << endl
 << str2 << endl;
 return 0;
 }
4/19/2011
4
STRINGS WITH EMBEDDED BLANKS
 The technique of our last examples won’t work with char* strings
containing embedded blanks. To handle such strings, you need to write a
specific delimiter character after each string, and use the getline() function,
rather than the extraction operator, to read them in.
 #include <fstream> //for file I/O
 int main()
 {
 ofstream outfile(“TEST.TXT”); //create file for output send text to file
 outfile << “I fear you cant understand it without practice!n”;
 outfile << “so you should give some timen”;
 outfile << “to become a master,n”;
 outfile << “to ruled over OOP.n”;
 return 0;
 }
READING WITH EMBEDDED SPACE
 #include <iostream>
 using namespace std;
 int main()
 {
 const int MAX = 80; //size of buffer
 char buffer[MAX]; //character buffer
 ifstream infile(“TEST.TXT”); //create file for input
 while( !infile.eof() ) //until end-of-file
 {
 infile.getline(buffer, MAX); //read a line of text
 cout << buffer << endl; //display it
 }
 return 0;
 }
CHARACTER I/O
 The put() and get() functions, which are members of ostream and istream,
respectively, can be used to output and input single characters
 #include <fstream> //for file functions
 #include <iostream>
 #include <string>
 int main()
 {
 string str = “Time is a great teacher, but unfortunately it kills all its pupils. ”;
 ofstream outfile(“TEST.TXT”); //create file for output
 for(int j=0; j<str.size(); j++) //for each character,
 outfile.put( str[j] ); //write it to file
 cout << “File writtenn”;
 return 0;
 }
READING FROM THE FILE
 #include <fstream> //for file functions
 #include <iostream>
 int main()
 {
 char ch; //character to read
 ifstream infile(“TEST.TXT”); //create file for input
 while( infile ) //read until EOF or error
 {
 infile.get(ch); //read character
 cout << ch; //display it
 }
 cout << endl;
 return 0;
 }
BINARY I/O
 // binary input and output with integers
 const int MAX = 100; //size of buffer
 int buff[MAX]; //buffer for integers
 int main()
 {
 for(int j=0; j<MAX; j++) //fill buffer with data
 buff[j] = j; //(0, 1, 2, ...)
 ofstream os(“edata.dat”, ios::binary);
 os.write( reinterpret_cast<char*>(buff), MAX*sizeof(int) ); //write to it
 os.close(); //must close it
 for(j=0; j<MAX; j++) //erase buffer
 buff[j] = 0;
 ifstream is(“edata.dat”, ios::binary); //create input stream
 is.read( reinterpret_cast<char*>(buff), MAX*sizeof(int) ); //read from it
 for(j=0; j<MAX; j++) //check data
 if( buff[j] != j )
 { cerr << “Data is incorrectn”; return 1; }
 cout << “Data is correctn”;
 }
BINARY I/O
 We use two new functions: write(), a member of ofstream; and
read(), a member of ifstream.
 These functions think about data in terms of bytes (type char).
 They don’t care how the data is formatted, they simply transfer a
buffer full of bytes from and to a disk file.
 The parameters to write() and read() are the address of the data
buffer and its length.
 The address must be cast, using reinterpret_cast, to type char*,
and the length is the length in bytes (characters), not the number
of data items in the buffer.
 You must use the ios::binary argument in the second parameter to
write() and read() when working with binary data.
 The ios::binary argument is an example of a mode bit.
4/19/2011
5
WRITING AN OBJECT TO DISK
 When writing an object we generally want to use binary mode. This writes
the same bit configuration to disk that was stored in memory, and ensures
that numerical data contained in objects is handled properly.
 class person //class of persons
 {
 protected:
 char name[80]; //person’s name
 short age; //person’s age
 public:
 void getData() //get person’s data
 {
 cout << “Enter name: “; cin >> name;
 cout << “Enter age: “; cin >> age;
 }
 };
int main()
{
person pers; //create a person
pers.getData(); //get data for person
//create ofstream object
ofstream outfile(“PERSON.DAT”,
ios::binary);
//write to it
outfile.write(reinterpret_cast<char*>(&
pers), sizeof(pers));
return 0;
}
READING AN OBJECT FROM DISK
 class person //class of persons
 {
 protected:
 char name[80]; //person’s name
 short age; //person’s age
 public:
 void showData() //display person’s data
 {
 cout << “Name: “ << name << endl;
 cout << “Age: “ << age << endl;
 }
 };
 int main()
 {
 person pers; //create person variable
 ifstream infile(“PERSON.DAT”, ios::binary); //create stream
 //read stream
 infile.read(reinterpret_cast<char*>(&pers), sizeof(pers) );
 pers.showData(); //display person
 }
I/O WITH MULTIPLE OBJECTS
 This example opens a file and writes as many objects as the user wants. Then it reads and
displays the entire contents of the file.
 class person //class of persons
 {
 protected:
 char name[80]; int age;
 public:
 void getData() //get person’s data
 { cout << “n Enter name: “; cin >> name;
 cout << “ Enter age: “; cin >> age; }
 void showData() //display person’s data
 { cout << “n Name: “ << name;
 cout << “n Age: “ << age; } };
 int main() {
 char ch;
 person pers; fstream file; //create input/output file
 //open for append
 file.open(“GROUP.DAT”, ios::app | ios::out |
 ios::in | ios::binary );
 do //data from user to file
 {
cout << “nEnter person’s data:”;
pers.getData(); //get one person’s data
file.write(
reinterpret_cast<char*>(&pers),
sizeof(pers) );
cout << “Enter another person (y/n)? “;
cin >> ch;
}
while(ch==’y’); //quit on ‘n’
file.seekg(0); //reset to start of file
file.read(
reinterpret_cast<char*>(&pers),
sizeof(pers) );
while( !file.eof() ) //quit on EOF
{
cout << “nPerson:”; //display person
pers.showData(); //read another person
file.read(
reinterpret_cast<char*>(&pers),
sizeof(pers) );
}
}
SPECIFYING THE POSITION
 int main()
 {
 person pers; ifstream infile; //create input file
 infile.open(“GROUP.DAT”, ios::in | ios::binary); //open file
 infile.seekg(0, ios::end); //go to 0 bytes from end
 int endposition = infile.tellg(); //find where we are
 int n = endposition / sizeof(person); //number of persons
 cout << “nThere are “ << n << “ persons in file”;
 cout << “nEnter person number: “;
 cin >> n;
 int position = (n-1) * sizeof(person); //number times size
 infile.seekg(position); //bytes from start
 infile.read( reinterpret_cast<char*>(&pers),sizeof(pers) );
 pers.showData(); //display the person
 }
class person //class of
persons
{ protected:
char name[80];
int age; //person’s age
public: void getData()
{ cout << “n Enter name: ”;
cin >> name;
cout << “ Enter age: ”; cin >>
age; }
void showData(void) { cout
<< “n Name: ” << name;
cout << “n Age: ” << age;
}};
FILE I/O WITH MEMBER FUNCTIONS
 n this section we’ll show two programs that do this. The first
uses ordinary member functions in which each object is
responsible for reading and writing itself to a file. The second
shows how static member functions can read and write all the
objects of a class at once.
 Objects That Read and Write Themselves
 Sometimes it makes sense to let each member of a class read
and write itself to a file.
 This is a simple approach, and works well if there aren’t many
objects to be read or written at once. In this example we add
member functions—diskOut() and diskIn()—to the person
class. These functions allow a person object to write itself to
disk and read itself back in.
4/19/2011
6
 class person //class of persons
 { protected:
 char name[40]; //person’s name
 int age; //person’s age
 public:
 void getData(void) {
 cout << “n Enter name: “; cin >> name;
 cout << “ Enter age: “; cin >> age; }
 void showData(void) {
 cout << “n Name: “ << name;
 cout << “n Age: “ << age; }
 void diskIn(int); //read from file
 void diskOut(); //write to file
 static int diskCount(); //return number of persons in file
 };
 void person::diskIn(int pn) //read person number pn
 { //from file
 ifstream infile; //make stream
 infile.open(“PERSFILE.DAT”, ios::binary); //open it
 infile.seekg( pn*sizeof(person) ); //move file ptr
 infile.read((char*)this, sizeof(*this)); //read one person
 }
 void person::diskOut() //write person to end of file
 {
 ofstream outfile; //make stream
 outfile.open(“PERSFILE.DAT”, ios::app | ios::binary); //open it
 outfile.write( (char*)this, sizeof(*this) ); //write to it
 }
 int person::diskCount() //return number of persons
 { //in file
 ifstream infile;
 infile.open(“PERSFILE.DAT”, ios::binary);
 infile.seekg(0, ios::end); //go to 0 bytes from end
 //calculate number of persons
 return (int)infile.tellg() / sizeof(person);
 }
 int main()
 {
 person p; //make an empty person
 char ch;
 do { //save persons to disk
 cout << “Enter data for person:”;
 p.getData(); //get data
 p.diskOut(); //write to disk
 cout << “Do another (y/n)? “;
 cin >> ch;
 } while(ch==’y’); //until user enters ‘n’
 int n = person::diskCount(); //how many persons in file?
 cout << “There are “ << n << “ persons in filen”;
 for(int j=0; j<n; j++) //for each one
 {
 cout << “nPerson “ << j;
 p.diskIn(j); //read person from disk
 p.showData(); //display person
 } }

More Related Content

What's hot

Python part2 v1
Python part2 v1Python part2 v1
Python part2 v1Sunil OS
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Chris Adamson
 
C,c++ interview q&a
C,c++ interview q&aC,c++ interview q&a
C,c++ interview q&aKumaran K
 
Managing input and output operation in c
Managing input and output operation in cManaging input and output operation in c
Managing input and output operation in cyazad dumasia
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpen Gurukul
 
Managing console i/o operation,working with files
Managing console i/o operation,working with filesManaging console i/o operation,working with files
Managing console i/o operation,working with filesramya marichamy
 
(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_iNico Ludwig
 
Future vs. Monix Task
Future vs. Monix TaskFuture vs. Monix Task
Future vs. Monix TaskHermann Hueck
 
Raspberry Pi - Lecture 5 Python for Raspberry Pi
Raspberry Pi - Lecture 5 Python for Raspberry PiRaspberry Pi - Lecture 5 Python for Raspberry Pi
Raspberry Pi - Lecture 5 Python for Raspberry PiMohamed Abdallah
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMSfawzmasood
 
Python Part 1
Python Part 1Python Part 1
Python Part 1Sunil OS
 
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & AnalyticsQuark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & AnalyticsJohn De Goes
 
Python Training v2
Python Training v2Python Training v2
Python Training v2ibaydan
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesGanesh Samarthyam
 
Java Basics
Java BasicsJava Basics
Java BasicsSunil OS
 

What's hot (20)

Python part2 v1
Python part2 v1Python part2 v1
Python part2 v1
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
 
C,c++ interview q&a
C,c++ interview q&aC,c++ interview q&a
C,c++ interview q&a
 
Managing input and output operation in c
Managing input and output operation in cManaging input and output operation in c
Managing input and output operation in c
 
Console i/o for c++
Console i/o for c++Console i/o for c++
Console i/o for c++
 
C Basics
C BasicsC Basics
C Basics
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ Programming
 
Managing console i/o operation,working with files
Managing console i/o operation,working with filesManaging console i/o operation,working with files
Managing console i/o operation,working with files
 
C++ Presentation
C++ PresentationC++ Presentation
C++ Presentation
 
The Style of C++ 11
The Style of C++ 11The Style of C++ 11
The Style of C++ 11
 
(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i
 
Future vs. Monix Task
Future vs. Monix TaskFuture vs. Monix Task
Future vs. Monix Task
 
Raspberry Pi - Lecture 5 Python for Raspberry Pi
Raspberry Pi - Lecture 5 Python for Raspberry PiRaspberry Pi - Lecture 5 Python for Raspberry Pi
Raspberry Pi - Lecture 5 Python for Raspberry Pi
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMS
 
Python Part 1
Python Part 1Python Part 1
Python Part 1
 
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & AnalyticsQuark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
 
Python Training v2
Python Training v2Python Training v2
Python Training v2
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on Examples
 
Writing Parsers and Compilers with PLY
Writing Parsers and Compilers with PLYWriting Parsers and Compilers with PLY
Writing Parsers and Compilers with PLY
 
Java Basics
Java BasicsJava Basics
Java Basics
 

Similar to streams and files

Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++Shyam Gupta
 
Managing,working with files
Managing,working with filesManaging,working with files
Managing,working with fileskirupasuchi1996
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)Saifur Rahman
 
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docxCS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docxfaithxdunce63732
 
File handling in_c
File handling in_cFile handling in_c
File handling in_csanya6900
 
Buffer Overflows
Buffer OverflowsBuffer Overflows
Buffer OverflowsSumit Kumar
 
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...ANUSUYA S
 
Os Vanrossum
Os VanrossumOs Vanrossum
Os Vanrossumoscon2007
 
C_and_C++_notes.pdf
C_and_C++_notes.pdfC_and_C++_notes.pdf
C_and_C++_notes.pdfTigabu Yaya
 
Chapter28 data-file-handling
Chapter28 data-file-handlingChapter28 data-file-handling
Chapter28 data-file-handlingDeepak Singh
 
FILE OPERATIONS.pptx
FILE OPERATIONS.pptxFILE OPERATIONS.pptx
FILE OPERATIONS.pptxDeepasCSE
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial javaTpoint s
 

Similar to streams and files (20)

Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Managing,working with files
Managing,working with filesManaging,working with files
Managing,working with files
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
 
Ds lec 14 filing in c++
Ds lec 14 filing in c++Ds lec 14 filing in c++
Ds lec 14 filing in c++
 
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docxCS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
 
Managing console input
Managing console inputManaging console input
Managing console input
 
C++ io manipulation
C++ io manipulationC++ io manipulation
C++ io manipulation
 
File handling in_c
File handling in_cFile handling in_c
File handling in_c
 
Buffer Overflows
Buffer OverflowsBuffer Overflows
Buffer Overflows
 
working with files
working with filesworking with files
working with files
 
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
 
7512635.ppt
7512635.ppt7512635.ppt
7512635.ppt
 
Os Vanrossum
Os VanrossumOs Vanrossum
Os Vanrossum
 
C_and_C++_notes.pdf
C_and_C++_notes.pdfC_and_C++_notes.pdf
C_and_C++_notes.pdf
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
Chapter28 data-file-handling
Chapter28 data-file-handlingChapter28 data-file-handling
Chapter28 data-file-handling
 
FILE OPERATIONS.pptx
FILE OPERATIONS.pptxFILE OPERATIONS.pptx
FILE OPERATIONS.pptx
 
Files
FilesFiles
Files
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 

Recently uploaded

WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 

Recently uploaded (20)

WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 

streams and files

  • 1. 4/19/2011 1 File and Streams W14L26 CHAPTER REVIEW  The largest part of this chapter is devoted to showing how to perform file-related activities using C++ streams.  We’ll show how to read and write data to files in a variety of ways, how to handle errors, and how files and OOP are related.  C programmers may wonder what advantages there are to using the stream classes for I/O, instead  of traditional C functions such as printf() and scanf(), and—for files—fprintf(), fscanf(), and so on. STREAM CLASSES  A stream is a general name given to a flow of data. In C++ a stream is represented by an object of a particular class. So far we’ve used the cin and cout stream objects.  Different streams are used to represent different kinds of data flow. For example, the ifstream class represents data flow from input disk files.  Advantages of Streams  Simplicity: no %d formatting character since each object already knows how to display itself.  Another reason is that you can overload existing operators and functions, such as the insertion (<<) and extraction (>>) operators, to work with classes that you create. STREAM I/O LIBRARY HEADER FILES  C++ views each files as a sequence of bytes.  Each file ends with an end-of-file marker.  When a file is opened, an object is created and a stream is associated with the object.  iostream -- contains basic information required for all stream I/O operations  iomanip -- contains information useful for performing formatted I/O with parameterized stream manipulators  fstream -- contains information for performing file I/O operations  strstream -- contains information for performing in- memory I/O operations (i.e., into or from strings in memory) THE STREAM CLASS HIERARCHY THE STREAM CLASS HIERARCHY  We’ve already made extensive use of some stream classes.  The extraction operator >> is a member of the istream class, and the insertion operator << is a member of the ostream class.  Both of these classes are derived from the ios class.  The cout object, representing the standard output stream, which is usually directed to the video display, is a predefined object of the ostream_withassign class, which is derived from the ostream class.  Similarly cin is an object of the istream_withassign class, which is derived from istream.  The classes used for input and output to the video display and keyboard are declared in the header file IOSTREAM,
  • 2. 4/19/2011 2 IN SHORT  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.  These classes are derived directly or indirectly from the classes istream, and ostream.  We have already used objects whose types were these classes: cin is an object of class istream and cout is an object of class ostream.  Therfore, we have already been using classes that are related to our file streams. And in fact, we can use our file streams the same way we are already used to use cin and cout, with the only difference that we have to associate these streams with physical files. Let's see an example: SIMPLE PROGRAM  // basic file operations  #include <iostream>  #include <fstream>  using namespace std;  int main () {  ofstream myfile;  myfile.open ("example.txt");  myfile << "Writing this to a file.n"; myfile.close();  return 0; } THE IOS CLASS  The ios class is the granddaddy of all the stream classes, and contains the majority of the features you need to operate C++ streams. The three most mportant features are,  Formatting Flags  Formatting flags are a set of enum definitions in ios. They act as on/off switches that specify choices for various aspects of input and output format and operation.  cout.setf(ios::left); // left justify output text  cout >> “This text is left-justified”;  cout.unsetf(ios::left); // return to default (right justified) Formatting Flags  Flag Meaning  skipws Skip (ignore) whitespace on input  left Left adjust output [12.34 ]  right Right adjust output [ 12.34]  internal Use padding between sign or base indicator and number [+ 12.34]  dec Convert to decimal  oct Convert to octal  Hex Convert to hexadecimal  boolalpha Convert bool to “true” or “false” strings  showbase Use base indicator on output (0 for octal, 0x for hex)  showpoint Show decimal point on output  Uppercase Use uppercase X, E, and hex output letters (ABCDEF)—the default is lowercase  showpos Display + before positive integers  scientific Use exponential format on floating-point output [9.1234E2]  fixed Use fixed format on floating-point output [912.34]  unitbuf Flush all streams after insertion  stdio Flush stdout, stderror after insertion MANIPULATORS  cout << “To each his own.” << endl;  cout << setiosflags(ios::fixed) // use fixed decimal point  << setiosflags(ios::showpoint) // always show decimal point  No-Argument ios Manipulators  Ws, dec,oct, hex, endl ,ends, flush, lock, unlock  cout << hex << var;  ios Manipulators with Arguments  Manipulator Argument  setw() field width (int)  setfill() fill character (int)  setprecision() precision (int)  setiosflags() formatting flags (long)  resetiosflags() formatting flags (long) FUNCTIONS  ch = fill(); fill(ch); p = precision(); precision(p); w = width();  width(w); setf(flags); unsetf(flags); setf(flags, field);  cout.width(14); cout.fill(‘*’);
  • 3. 4/19/2011 3 THE ISTREAM CLASS (SEE YOUR BOOK)  The istream class, which is derived from ios, performs input- specific activities, or extraction.  Istrea  Function  >>  get(ch);  get(str)  get(str, MAX)  get(str, DELIM)  get(str, MAX, DELIM)  getline(str, MAX, DELIM)  putback(ch)  ignore(MAX,  peek(ch)m Functions THE OSTREAM CLASS  The ostream class handles output or insertion activities.  Function  <<  put(ch)  flush()  write(str, SIZE)  seekp(position)  seekp(position, seek_dir)  pos = tellp() STREAM ERRORS  cin >> var; What happens if a user enters the string “nine” instead of the integer 9, or pushes the [Enter] key without entering anything? Or what happens if there’s a hardware failure?  Some tables are given in your book please see to understand different types of error. DISK FILE I/O WITH STREAMS  Most programs need to save data to disk files and read it back in. Working with disk files requires another set of classes: ifstream for input, fstream for both input and output, and ofstream for output.  Objects of these classes can be associated with disk files, and we can use their member functions to read and write to the files.  Formatted File I/O  In formatted I/O, numbers are stored on disk as a series of characters. Thus 6.02, rather than being stored as a 4-byte type float or an 8-byte type double, is stored as the characters ‘6’, ‘.’, ‘0’, and ‘2’. This can be inefficient for numbers with many digits, but it’s appropriate in many situations and easy to implement. Characters and strings are stored more or less normally. WRITING DATA  #include <fstream.h> //for file I/O  #include <iostream.h>  #include<stdio.h>  #include<conio.h>  int main()  {  char ch = 'x';  int j = 77;  double d = 6.02;  char str1[] = “Amar"; //strings without  char str2[] = “Sara";  ofstream outfile("fdata.txt"); //create ofstream object  outfile << ch //insert (write) data  << j  << ' ' //needs space between numbers  << d  << str1  << ' ' //needs spaces between strings  << str2;  cout << "File writtenn";  getch();  } READING DATA  #include <fstream.h> //for file I/O  #include <iostream.h>  #include<stdio.h>  #include<conio.h>  int main()  {  char ch;  int j;  double d;  string str1;  string str2;  ifstream infile(“fdata.txt”); //create ifstream object //extract (read) data from it  infile >> ch >> j >> d >> str1 >> str2;  cout << ch << endl //display the data  << j << endl  << d << endl  << str1 << endl  << str2 << endl;  return 0;  }
  • 4. 4/19/2011 4 STRINGS WITH EMBEDDED BLANKS  The technique of our last examples won’t work with char* strings containing embedded blanks. To handle such strings, you need to write a specific delimiter character after each string, and use the getline() function, rather than the extraction operator, to read them in.  #include <fstream> //for file I/O  int main()  {  ofstream outfile(“TEST.TXT”); //create file for output send text to file  outfile << “I fear you cant understand it without practice!n”;  outfile << “so you should give some timen”;  outfile << “to become a master,n”;  outfile << “to ruled over OOP.n”;  return 0;  } READING WITH EMBEDDED SPACE  #include <iostream>  using namespace std;  int main()  {  const int MAX = 80; //size of buffer  char buffer[MAX]; //character buffer  ifstream infile(“TEST.TXT”); //create file for input  while( !infile.eof() ) //until end-of-file  {  infile.getline(buffer, MAX); //read a line of text  cout << buffer << endl; //display it  }  return 0;  } CHARACTER I/O  The put() and get() functions, which are members of ostream and istream, respectively, can be used to output and input single characters  #include <fstream> //for file functions  #include <iostream>  #include <string>  int main()  {  string str = “Time is a great teacher, but unfortunately it kills all its pupils. ”;  ofstream outfile(“TEST.TXT”); //create file for output  for(int j=0; j<str.size(); j++) //for each character,  outfile.put( str[j] ); //write it to file  cout << “File writtenn”;  return 0;  } READING FROM THE FILE  #include <fstream> //for file functions  #include <iostream>  int main()  {  char ch; //character to read  ifstream infile(“TEST.TXT”); //create file for input  while( infile ) //read until EOF or error  {  infile.get(ch); //read character  cout << ch; //display it  }  cout << endl;  return 0;  } BINARY I/O  // binary input and output with integers  const int MAX = 100; //size of buffer  int buff[MAX]; //buffer for integers  int main()  {  for(int j=0; j<MAX; j++) //fill buffer with data  buff[j] = j; //(0, 1, 2, ...)  ofstream os(“edata.dat”, ios::binary);  os.write( reinterpret_cast<char*>(buff), MAX*sizeof(int) ); //write to it  os.close(); //must close it  for(j=0; j<MAX; j++) //erase buffer  buff[j] = 0;  ifstream is(“edata.dat”, ios::binary); //create input stream  is.read( reinterpret_cast<char*>(buff), MAX*sizeof(int) ); //read from it  for(j=0; j<MAX; j++) //check data  if( buff[j] != j )  { cerr << “Data is incorrectn”; return 1; }  cout << “Data is correctn”;  } BINARY I/O  We use two new functions: write(), a member of ofstream; and read(), a member of ifstream.  These functions think about data in terms of bytes (type char).  They don’t care how the data is formatted, they simply transfer a buffer full of bytes from and to a disk file.  The parameters to write() and read() are the address of the data buffer and its length.  The address must be cast, using reinterpret_cast, to type char*, and the length is the length in bytes (characters), not the number of data items in the buffer.  You must use the ios::binary argument in the second parameter to write() and read() when working with binary data.  The ios::binary argument is an example of a mode bit.
  • 5. 4/19/2011 5 WRITING AN OBJECT TO DISK  When writing an object we generally want to use binary mode. This writes the same bit configuration to disk that was stored in memory, and ensures that numerical data contained in objects is handled properly.  class person //class of persons  {  protected:  char name[80]; //person’s name  short age; //person’s age  public:  void getData() //get person’s data  {  cout << “Enter name: “; cin >> name;  cout << “Enter age: “; cin >> age;  }  }; int main() { person pers; //create a person pers.getData(); //get data for person //create ofstream object ofstream outfile(“PERSON.DAT”, ios::binary); //write to it outfile.write(reinterpret_cast<char*>(& pers), sizeof(pers)); return 0; } READING AN OBJECT FROM DISK  class person //class of persons  {  protected:  char name[80]; //person’s name  short age; //person’s age  public:  void showData() //display person’s data  {  cout << “Name: “ << name << endl;  cout << “Age: “ << age << endl;  }  };  int main()  {  person pers; //create person variable  ifstream infile(“PERSON.DAT”, ios::binary); //create stream  //read stream  infile.read(reinterpret_cast<char*>(&pers), sizeof(pers) );  pers.showData(); //display person  } I/O WITH MULTIPLE OBJECTS  This example opens a file and writes as many objects as the user wants. Then it reads and displays the entire contents of the file.  class person //class of persons  {  protected:  char name[80]; int age;  public:  void getData() //get person’s data  { cout << “n Enter name: “; cin >> name;  cout << “ Enter age: “; cin >> age; }  void showData() //display person’s data  { cout << “n Name: “ << name;  cout << “n Age: “ << age; } };  int main() {  char ch;  person pers; fstream file; //create input/output file  //open for append  file.open(“GROUP.DAT”, ios::app | ios::out |  ios::in | ios::binary );  do //data from user to file  { cout << “nEnter person’s data:”; pers.getData(); //get one person’s data file.write( reinterpret_cast<char*>(&pers), sizeof(pers) ); cout << “Enter another person (y/n)? “; cin >> ch; } while(ch==’y’); //quit on ‘n’ file.seekg(0); //reset to start of file file.read( reinterpret_cast<char*>(&pers), sizeof(pers) ); while( !file.eof() ) //quit on EOF { cout << “nPerson:”; //display person pers.showData(); //read another person file.read( reinterpret_cast<char*>(&pers), sizeof(pers) ); } } SPECIFYING THE POSITION  int main()  {  person pers; ifstream infile; //create input file  infile.open(“GROUP.DAT”, ios::in | ios::binary); //open file  infile.seekg(0, ios::end); //go to 0 bytes from end  int endposition = infile.tellg(); //find where we are  int n = endposition / sizeof(person); //number of persons  cout << “nThere are “ << n << “ persons in file”;  cout << “nEnter person number: “;  cin >> n;  int position = (n-1) * sizeof(person); //number times size  infile.seekg(position); //bytes from start  infile.read( reinterpret_cast<char*>(&pers),sizeof(pers) );  pers.showData(); //display the person  } class person //class of persons { protected: char name[80]; int age; //person’s age public: void getData() { cout << “n Enter name: ”; cin >> name; cout << “ Enter age: ”; cin >> age; } void showData(void) { cout << “n Name: ” << name; cout << “n Age: ” << age; }}; FILE I/O WITH MEMBER FUNCTIONS  n this section we’ll show two programs that do this. The first uses ordinary member functions in which each object is responsible for reading and writing itself to a file. The second shows how static member functions can read and write all the objects of a class at once.  Objects That Read and Write Themselves  Sometimes it makes sense to let each member of a class read and write itself to a file.  This is a simple approach, and works well if there aren’t many objects to be read or written at once. In this example we add member functions—diskOut() and diskIn()—to the person class. These functions allow a person object to write itself to disk and read itself back in.
  • 6. 4/19/2011 6  class person //class of persons  { protected:  char name[40]; //person’s name  int age; //person’s age  public:  void getData(void) {  cout << “n Enter name: “; cin >> name;  cout << “ Enter age: “; cin >> age; }  void showData(void) {  cout << “n Name: “ << name;  cout << “n Age: “ << age; }  void diskIn(int); //read from file  void diskOut(); //write to file  static int diskCount(); //return number of persons in file  };  void person::diskIn(int pn) //read person number pn  { //from file  ifstream infile; //make stream  infile.open(“PERSFILE.DAT”, ios::binary); //open it  infile.seekg( pn*sizeof(person) ); //move file ptr  infile.read((char*)this, sizeof(*this)); //read one person  }  void person::diskOut() //write person to end of file  {  ofstream outfile; //make stream  outfile.open(“PERSFILE.DAT”, ios::app | ios::binary); //open it  outfile.write( (char*)this, sizeof(*this) ); //write to it  }  int person::diskCount() //return number of persons  { //in file  ifstream infile;  infile.open(“PERSFILE.DAT”, ios::binary);  infile.seekg(0, ios::end); //go to 0 bytes from end  //calculate number of persons  return (int)infile.tellg() / sizeof(person);  }  int main()  {  person p; //make an empty person  char ch;  do { //save persons to disk  cout << “Enter data for person:”;  p.getData(); //get data  p.diskOut(); //write to disk  cout << “Do another (y/n)? “;  cin >> ch;  } while(ch==’y’); //until user enters ‘n’  int n = person::diskCount(); //how many persons in file?  cout << “There are “ << n << “ persons in filen”;  for(int j=0; j<n; j++) //for each one  {  cout << “nPerson “ << j;  p.diskIn(j); //read person from disk  p.showData(); //display person  } }