Consider the following example where a base class has been derived by other two classes:

Size: px
Start display at page:

Download "Consider the following example where a base class has been derived by other two classes:"

Transcription

1 Class : BCA 3rd Semester Course Code: BCA-S3-03 Course Title: Object Oriented Programming Concepts in C++ Unit IV Polymorphism The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance. C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function. Consider the following example where a base class has been derived by other two classes: #include <iostream> using namespace std; class Shape protected: int width, height; public: Shape( int a = 0, int b = 0) width = a; height = b; ; int area() cout << "Parent class area :" <<endl; return 0; class Rectangle: public Shape public: Rectangle( int a = 0, int b = 0):Shape(a, b) int area () cout << "Rectangle class area :" <<endl; return (width * height); ; class Triangle: public Shape public: Triangle( int a = 0, int b = 0):Shape(a, b) int area () cout << "Triangle class area :" <<endl;

2 ; return (width * height / 2); // Main function for the program int main( ) Shape *shape; Rectangle rec(10,7); Triangle tri(10,5); // store the address of Rectangle shape = &rec; // call rectangle area. shape->area(); // store the address of Triangle shape = &tri; // call triangle area. shape->area(); return 0; When the above code is compiled and executed, it produces the following result: Parent class area Parent class area The reason for the incorrect output is that the call of the function area() is being set once by the compiler as the version defined in the base class. This is called static resolution of the function call, or static linkage - the function call is fixed before the program is executed. This is also sometimes called early binding because the area() function is set during the compilation of the program. But now, let's make a slight modification in our program and precede the declaration of area() in the Shape class with the keyword virtual so that it looks like this: class Shape protected: int width, height; public: Shape( int a = 0, int b = 0) width = a; height = b;

3 virtual int area() cout << "Parent class area :" <<endl; return 0; ; After this slight modification, when the previous example code is compiled and executed, it produces the following result: Rectangle class area Triangle class area This time, the compiler looks at the contents of the pointer instead of it's type. Hence, since addresses of objects of tri and rec classes are stored in *shape the respective area() function is called. As you can see, each of the child classes has a separate implementation for the function area(). This is how polymorphism is generally used. You have different classes with a function of the same name, and even the same parameters, but with different implementations. Virtual Function A virtual function is a function in a base class that is declared using the keyword virtual. Defining in a base class as virtual function, with another version in a derived class, signals to the compiler that we don't want static linkage for this function. What we do want is the selection of the function to be called at any given point in the program to be based on the kind of object for which it is called. This sort of operation is referred to as dynamic linkage, or late binding. Pure Virtual Functions It's possible that you'd want to include a virtual function in a base class so that it may be redefined in a derived class to suit the objects of that class, but that there is no meaningful definition you could give for the function in the base class. We can change the virtual function area() in the base class to the following: class Shape protected: int width, height; public: Shape( int a = 0, int b = 0) width = a; height = b; ; // pure virtual function virtual int area() = 0;

4 The = 0 tells the compiler that the function has no body and above virtual function will be called pure virtual function. Early binding Most of the function calls the compiler encounters will be direct function calls. A direct function call is a statement that directly calls a function. For example: #include <iostream> void PrintValue(int nvalue) std::cout << nvalue; int main() PrintValue(5); // This is a direct function call return 0; Direct function calls can be resolved using a process known as early binding. Early binding (also called static binding) means the compiler is able to directly associate the identifier name (such as a function or variable name) with a machine address. Remember that all functions have a unique machine address. So when the compiler encounters a function call, it replaces the function call with a machine language instruction that tells the CPU to jump to the address of the function. Let s take a look at a simple calculator program that uses early binding: #include <iostream> using namespace std; int Add(int nx, int ny) return nx + ny; int Subtract(int nx, int ny) return nx - ny; int Multiply(int nx, int ny) return nx * ny; int main() int nx; cout << "Enter a number: "; cin >> nx;

5 int ny; cout << "Enter another number: "; cin >> ny; int noperation; do cout << "Enter an operation (0=add, 1=subtract, 2=multiply): "; cin >> noperation; while (noperation < 0 noperation > 2); int nresult = 0; switch (noperation) case 0: nresult = Add(nX, ny); break; case 1: nresult = Subtract(nX, ny); break; case 2: nresult = Multiply(nX, ny); break; cout << "The answer is: " << nresult << endl; return 0; Because Add(), Subtract(), and Multiply() are all direct function calls, the compiler will use early binding to resolve the Add(), Subtract(), and Multiply() function calls. The compiler will replace the Add() function call with an instruction that tells the CPU to jump to the address of the Add() function. The same holds true for Subtract() and Multiply(). Abstract Class Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract classes are used to provide an Interface for its sub classes. Classes inheriting an Abstract Class must provide definition to the pure virtual function, otherwise they will also become abstract class. Characteristics of Abstract Class Abstract class cannot be instantiated, but pointers and refrences of Abstract class type can be created. Abstract class can have normal functions and variables along with a pure virtual function. Abstract classes are mainly used for Upcasting, so that its derived classes can use its interface. Classes inheriting an Abstract Class must implement all pure virtual functions, or else they will become Abstract too. Pure Virtual Functions Pure virtual Functions are virtual functions with no definition. They start with virtual keyword and ends with= 0. Here is the syntax for a pure virtual function, virtual void f() = 0;

6 Example of Abstract Class class Base //Abstract base class public: virtual void show() = 0; //Pure Virtual Function ; class Derived:public Base public: void show() cout << "Implementation of Virtual Function in Derived class"; ; int main() Base obj; Base *b; Derived d; b = &d; b->show(); //Compile Time Error Output : Implementation of Virtual Function in Derived class In the above example Base class is abstract, with pure virtual show() function, hence we cannot create object of base class. Data File Handling In C++ File. The information / data stored under a specific name on a storage device, is called a file. Stream. It refers to a sequence of bytes. Text file. It is a file that stores information in ASCII characters. In text files, each line of text is terminated with a special character known as EOL (End of Line) character or delimiter character. When this EOL character is read or written, certain internal translations take place. Binary file. It is a file that contains information in the same format as it is held in memory. In binary files, no delimiters are used for a line and no translations occur here. Classes for file stream operation 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. Opening a file

7 OPENING FILE USING CONSTRUCTOR ofstream outfile("sample.txt"); //output only ifstream infile( sample.txt ); //input only OPENING FILE USING open() method Stream-object.open( filename, mode) ofstream outfile; outfile.open("sample.txt"); ifstream infile; infile.open("sample.txt"); File mode parameter Meaning ios::app Append to end of file ios::ate go to end of file on opening ios::binary file open in binary mode ios::in open file for reading only ios::out open file for writing only ios::nocreate open fails if the file does not exist ios::noreplace open fails if the file already exist ios::trunc delete the contents of the file if it exist The mode parameter can have different interpretations as under All these flags can be combined using the bitwise operator OR ( ). For example, if we want to open the file example.bin in binary mode to add data we could do it by the following call to member function open(): fstream file; file.open ("example.bin", ios::out ios::app ios::binary); Closing File

8 outfile.close(); infile.close(); INPUT AND OUTPUT OPERATION put() and get() function the function put() writes a single character to the associated stream. Similarly, the function get() reads a single character form the associated stream. example : file.get(ch); file.put(ch); write() and read() function write() and read() functions write and read blocks of binary data. example: file.read((char *)&obj, sizeof(obj)); file.write((char *)&obj, sizeof(obj)); ERROR HANDLING FUNCTION FUNCTION RETURN VALUE AND MEANING eof() returns true (non zero) if end of file is encountered while reading; otherwise return false(zero) fail() return true when an input or output operation has failed bad() returns true if an invalid operation is attempted or any unrecoverable error has occurred. good() returns true if no error has occurred.

9 File Pointers And Their Manipulation All i/o streams objects have, at least, one internal stream pointer: ifstream, like istream, has a pointer known as the get pointer that points to the element to be read in the next input operation. ofstream, like ostream, has a pointer known as the put pointer that points to the location where the next element has to be written. Finally, fstream, inherits both, the get and the put pointers, from iostream (which is itself derived from both istream and ostream). These internal stream pointers that point to the reading or writing locations within a stream can be manipulated using the following member functions: seekg() moves get pointer(input) to a specified location seekp() moves put pointer (output) to a specified location tellg() gives the current position of the get pointer tellp() gives the current position of the put pointer The other prototype for these functions is: seekg(offset, refposition ); seekp(offset, refposition ); The parameter offset represents the number of bytes the file pointer is to be moved from the location specified by the parameter refposition. The refposition takes one of the following three constants defined in the ios class. ios::beg start of the file ios::cur current position of the pointer ios::end end of the file example: file.seekg(-10, ios::cur);

10 Basic Operation On Text File In C++ File I/O is a five-step process: 1. Include the header file fstream in the program. 2. Declare file stream object. 3. Open the file with the file stream object. 4. Use the file stream object with >>, <<, or other input/output functions. 5. Close the files. Following program shows how the steps might appear in program. Program to write in a text file #include <fstream> using namespace std; int main() ofstream fout; fout.open("out.txt"); char str[300] = "Time is a great teacher but unfortunately it kills all its pupils. Berlioz"; //Write string to the file. fout << str; fout.close(); return 0; Program to read from text file and display it #include<fstream> #include<iostream> using namespace std; int main() ifstream fin; fin.open("out.txt"); char ch; while(!fin.eof()) fin.get(ch);

11 cout << ch; fin.close(); return 0; Program to count number of characters. #include<fstream> #include<iostream> using namespace std; int main() ifstream fin; fin.open("out.txt"); int count = 0; char ch; while(!fin.eof()) fin.get(ch); count++; cout << "Number of characters in file are " << count; fin.close(); return 0; Program to count number of words #include<fstream> #include<iostream> using namespace std; int main() ifstream fin; fin.open("out.txt"); int count = 0; char word[30]; while(!fin.eof())

12 fin >> word; count++; cout << "Number of words in file are " << count; fin.close(); return 0; Program to count number of lines #include<fstream> #include<iostream> using namespace std; int main() ifstream fin; fin.open("out.txt"); int count = 0; char str[80]; while(!fin.eof()) fin.getline(str,80); count++; cout << "Number of lines in file are " << count; fin.close(); return 0; Program to copy contents of file to another file. #include<fstream> using namespace std; int main() ifstream fin; fin.open("out.txt"); ofstream fout; fout.open("sample.txt");

13 char ch; while(!fin.eof()) fin.get(ch); fout << ch; fin.close(); fout.close(); return 0; Basic Operation On Binary File In C++ When data is stored in a file in the binary format, reading and writing data is faster because no time is lost in converting the data from one format to another format. Such files are called binary files. This following program explains how to create binary files and also how to read, write, search, delete and modify data from binary files. #include<iostream> #include<fstream> #include<cstdio> using namespace std; class Student int admno; char name[50]; public: void setdata() cout << "\nenter admission no. "; cin >> admno; cout << "Enter name of student "; cin.getline(name,50); void showdata() cout << "\nadmission no. : " << admno; cout << "\nstudent Name : " << name; int retadmno() return admno;

14 ; /* * function to write in a binary file. */ void write_record() ofstream outfile; outfile.open("student.dat", ios::binary ios::app); Student obj; obj.setdata(); outfile.write((char*)&obj, sizeof(obj)); outfile.close(); /* * function to display records of file */ void display() ifstream infile; infile.open("student.dat", ios::binary); Student obj; while(infile.read((char*)&obj, sizeof(obj))) obj.showdata(); infile.close(); /* * function to search and display from binary file */ void search(int n) ifstream infile; infile.open("student.dat", ios::binary);

15 Student obj; while(infile.read((char*)&obj, sizeof(obj))) if(obj.retadmno() == n) obj.showdata(); infile.close(); /* * function to delete a record */ void delete_record(int n) Student obj; ifstream infile; infile.open("student.dat", ios::binary); ofstream outfile; outfile.open("temp.dat", ios::out ios::binary); while(infile.read((char*)&obj, sizeof(obj))) if(obj.retadmno()!= n) outfile.write((char*)&obj, sizeof(obj)); infile.close(); outfile.close(); remove("student.dat"); rename("temp.dat", "student.dat"); /* * function to modify a record */ void modify_record(int n) fstream file; file.open("student.dat",ios::in ios::out);

16 Student obj; while(file.read((char*)&obj, sizeof(obj))) if(obj.retadmno() == n) cout << "\nenter the new details of student"; obj.setdata(); int pos = -1 * sizeof(obj); file.seekp(pos, ios::cur); file.write((char*)&obj, sizeof(obj)); file.close(); int main() //Store 4 records in file for(int i = 1; i <= 4; i++) write_record(); //Display all records cout << "\nlist of records"; display(); //Search record cout << "\nsearch result"; search(100); //Delete record delete_record(100); cout << "\nrecord Deleted"; //Modify record cout << "\nmodify Record 101 "; modify_record(101); return 0; I/O Manipulators Manipulators are the most common way to control output formating.

17 I/O manipulators that take parameters are in the <iomanip> include file. Default Floating-point Format Unless you use I/O manipulators (or their equivalent), the default format for each floating-point number depends on its value. No decimal point: prints as 1 No trailing zeros: prints as 1.5 Scientific notation for large/small numbers: prints as e+09 Example #include <iostream> #include <iomanip> using namespace std; int main() const float tenth = 0.1; const float one = 1.0; const float big = ; cout << "A. " << tenth << ", " << one << ", " << big << endl; cout << "B. " << fixed << tenth << ", " << one << ", " << big << endl; cout << "C. " << scientific << tenth << ", " << one << ", " << big << endl; cout << "D. " << fixed << setprecision(3) << tenth << ", " << one << ", " << big << endl; cout << "E. " << setprecision(20) << tenth << endl; cout << "F. " << setw(8) << setfill('*') << 34 << 45 << endl; cout << "G. " << setw(8) << 34 << setw(8) << 45 << endl; return 0; produces this output (using DevC ): A. 0.1, 1, e+009 B , , C e-001, e+000, e+009 D , 1.000, E F. ******3445 G. ******34******45 The following are the list of standard manipulator used in a C++ program. To carry out the operations of these manipulator functions in a user program, the header file input and output manipulator <iomanip.h> must be included. Predefined manipulators

18 Following are the standard manipulators normally used in the stream classes: endl hex, dec, oct setbase setw setfill setprecision ends ws flush setiosflags resetiosflags (a) Endl the endl is an output manipulator to generate a carriage return or line feed character. The endl may be used several times in a C++ statement. For example, (1) cout << a << endl << b << endl; (2) cout << a = << a << endl; cout << b = << b << endl; A program to display a message on two lines using the endl manipulator and the corresponding output is given below. / / using endl manipulator #include <iostream.h> Void main (void) cout << My name is Komputer ; cout << endl;

19 cout << many greetings to you ; Output of the above program My name is Komputer Many greetings to you The endl is the same as the non-graphic character to generate line feed (\n). A program to illustrate the usage of line feed character and the endl manipulator and the corresponding output are given below. #include <iostream.h> void main ( ) int a; a = 20; cout << a = << a << endl; cout << a = << a << \n ; cout << a = << a << \n ; Output of the above program a = 20 a = 20 a = 20 (b) Setbase() The setbase() manipulator is used to convert the base of one numeric value into another base. Following are the common base converters in C++. dec - decimal base (base = 10) hex - hexadecimal base (base = 16) oct - octal base (base = 8)

20 In addition to the base conversion facilities such as to bases dec, hex and oct, the setbase()manipulator is also used to define the base of the numeral value of a variable. The prototype ofsetbase() manipulator is defined in the iomanip.h header file and it should be include in user program. The hex, dec, oct manipulators change the base of inserted or extracted integral values. The original default for stream input and output is dec. PROGRAM A program to show the base of a numeric value of a variable using hex, oct and dec manipulator functions. / / using dec, hex, oct manipulator #include <iostream.h> Void main (void) int value; cout << Enter number << endl; cin >> value; cout << Decimal base = << dec << value << endl; cout << Hexadecimal base = << hex << value << endl; cout << Octal base = << oct << value << endl; Output of the above program Enter number 10 Decimal base = 10 Hexadecimal base = a Octal base = 12 PROGRAM A program to show the base of a numeric value of a variable using setbase manipulator function. / /using setbase manipulator

21 #include <iostream.h> #include <iomanip.h> void main (void) int value cout << Enter number << endl; cin >> value; cout << decimal base = << setbase (10) cout << value << endl; cout << hexadecimal base = << setbase (16); cout << value << endl; cout << Octal base = << setbase (8) << value << endl; The output of this program is the same as the output of program 2.2 (c) Setw () The setw ( ) stands for the set width. The setw ( ) manipulator is used to specify the minimum number of character positions on the output field a variable will consume. The general format of the setw manipulator function is setw( int w ) Which changes the field width to w, but only for the next insertion. The default field width is 0. For example, cout << setw (1) << a << endl; cout << setw (10) << a << endl; Between the data variables in C++ space will not be inserted automatically by the compiler. It is upto a programmer to introduce proper spaces among data while displaying onto the screen. PROGRAM A program to display the content of a variable without inserting any space. #include <iostream.h>

22 void main (void) int a,b; a = 200; b = 300; cout << a << b << endl; Output of the above program PROGRAM A program to insert a tab character between two variables while displaying the content onto the screen. / /using setw manipulator #include <iostream.h> #include <iomanip.h> void main (void) int a,b; a = 200; b = 300; cout << a << \t << b << endl; Output of the above program PROGRAM A program to display the data variables using setw manipulator functions.

23 / /using setw manipulator #include <iostream.h> #include <iomanip.h> void main (void) int a,b; a = 200; b = 300; cout << setw (5) << a << setw (5) << b << endl; cout << setw (6) << a << setw (6) << b << endl; cout << setw (7) << a << setw (7) << b << endl; cout << setw (8) << a << setw (8) << b << endl; Output of the above program (d) Setfill() The setfill ( ) manipulator function is used to specify a different character to fill the unused field width of the value. The general syntax of the setfill ( ) manipulator is setfill( char f) which changes the fill character to f. The default fill character is a space. For example, setfill (. ) ; / / fill a dot (. ) character setfill ( * ) / / fill a asterisk (*) character PROGRAM

24 A program to illustrate how a character is filled in filled in the unused field width of the value of the data variable. / /using setfill manipulator #include <iostream.h> #include <iomanip.h> void main ( void) int a,b; a = 200; b = 300; cout << setfill ( * ); cout << setw (5) << a << setw (5) << b << endl; cout << setw (6) << a << setw (6) << b << endl; cout << setw (7) << a << setw (7) << b << endl; cout << setw (8) << a << setw (8) << b << endl; Output of the above program **200**300 ***200***300 ****200****300 *****200*****300 (e) Setprecision() The setprecision ( ) is used to control the number of digits of an output stream display of a floating point value. The setprecision ( ) manipulator prototype is defined in the header file <iomanip.h>. The general syntax of the setprecision manipulator is Setprecision (int p) Which sets the precision for floating point insertions to p. The default precision is 6 PROGRAM

25 A program to use the setprecision manipulator function while displaying a floating point value onto the screen. / /using setprecision manipulator #include <iostream.h> #include <iomanip.h> void main (void) float a,b,c; a = 5; b = 3; c = a/b; cout << setprecision (1) << c << endl; cout << setprecision (2) << c << endl; cout << setprecision (3) << c << endl; cout << setprecision (4) << c << endl; cout << setprecision (5) << c << endl; cout << setprecision (6) << c << endl; Output of the program

26 (f) Ends The ends is a manipulator used to attach a null terminating character ( \0 ) at the end of a string. The ends manipulator takes no argument whenever it is invoked. This causes a null character to the output. PROGRAM A program to show how a null character is inserted using ends manipulator while displaying a string onto the screen. / /using ends manipulator #include <iostream.h> #include <iomanip.h> Void main ( ) int number = 231; cout << \ << number = << number << ends; cout << \ << endl; Output of the above program number = 123 (g) Ws The manipulator function ws stands for white space. It is used to ignore the leading white space that precedes the first field. / /using ws manipulator #include <iostream.h> #include <iomanip.h> Void main ( ) char name [100] cout << enter a line of text \n ; cin >> ws; cin >> name;

27 cout << typed text is = << name << endl; Output of the program enter a line of text this is a text typed text is = this (b) Flush The flush member function is used to cause the stream associated with the output to be completed emptied. This argument function takes no input prameters whenever it is invoked. For input on the screen, this is not necessary as all output is flushed automatically. However, in the case of a disk file begin copied to another, it has to flush the output buffer prior to rewinding the output file for continued use. The function flush ( ) does not have anything to do with flushing the input buffer. / /using flush member function #include <iostream.h> #include <iomanip.h> void main ( ) cout << My name is Komputer \n ; cout << many greetings to you \n ; cout. flush ( ) ; The hex, dec, oct, ws, endl, ends and flush manipulators are defined in stream.h. The other manipulators are defined in iomanip.h which must be included in any program that employs them. (i) Setiosflags and Resetiosflags The setiosflags manipulator function is used to control different input and output settings. The I/O stream maintains a collection of flag bits. The setiosflags manipulator performs the same function as the setf function. The flags represented by the set bits in f are set. The general syntax of the setiosflags is, setiosflags ( long h)

28 The restiosflags manipulator performs the same function as that of the resetf function. The flags represented by the set bits in f are reset. The general syntax of the resetiosflags is a follows, Resetiosflags (long f) PROGRAM A program to demonstrate how setiosflags is set while displaying the base of a numeral. / /using setiosflags of basefield #include <iostream.h> #include <iomanip.h> void main (void) int value; cout << enter a number \n ; cin >> value; cout << setiosflags (ios : : showbase) ; cout << setiosflags (ios : : dec) ; cout << decimal = << value << endl; cout << setiosflags (ios : : hex) ; cout << hexadecimal = << value << endl ; cout << setiosflags (ios : : oct) ; cout << octal = << value << endl ; Output of the above program enter a number 10 decimal = 10

29 hexadecimal = 0xa octal = 012

Object Oriented Programming Using C++ UNIT-3 I/O Streams

Object Oriented Programming Using C++ UNIT-3 I/O Streams File - The information / data stored under a specific name on a storage device, is called a file. Stream - It refers to a sequence of bytes. Text file - It is a file that stores information in ASCII characters.

More information

Convenient way to deal large quantities of data. Store data permanently (until file is deleted).

Convenient way to deal large quantities of data. Store data permanently (until file is deleted). FILE HANDLING Why to use Files: Convenient way to deal large quantities of data. Store data permanently (until file is deleted). Avoid typing data into program multiple times. Share data between programs.

More information

C++ Programming Lecture 10 File Processing

C++ Programming Lecture 10 File Processing C++ Programming Lecture 10 File Processing By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department Outline Introduction. The Data Hierarchy. Files and Streams. Creating a Sequential

More information

Chapte t r r 9

Chapte t r r 9 Chapter 9 Session Objectives Stream Class Stream Class Hierarchy String I/O Character I/O Object I/O File Pointers and their manipulations Error handling in Files Command Line arguments OOPS WITH C++ Sahaj

More information

Chapter-12 DATA FILE HANDLING

Chapter-12 DATA FILE HANDLING Chapter-12 DATA FILE HANDLING Introduction: A file is a collection of related data stored in a particular area on the disk. Programs can be designed to perform the read and write operations on these files.

More information

CS Programming2 1 st Semester H Sheet # 8 File Processing. Princess Nora University College of Computer and Information Sciences

CS Programming2 1 st Semester H Sheet # 8 File Processing. Princess Nora University College of Computer and Information Sciences Princess Nora University College of Computer and Information Sciences CS 142-341 Programming2 1 st Semester 1434-1435 H Sheet # 8 File Processing Question#1 Write line of code to do the following: 1- Open

More information

Unit-V File operations

Unit-V File operations Unit-V File operations What is stream? C++ IO are based on streams, which are sequence of bytes flowing in and out of the programs. A C++ stream is a flow of data into or out of a program, such as the

More information

Object Oriented Programming In C++

Object Oriented Programming In C++ C++ Question Bank Page 1 Object Oriented Programming In C++ 1741059 to 1741065 Group F Date: 31 August, 2018 CIA 3 1. Briefly describe the various forms of get() function supported by the input stream.

More information

After going through this lesson, you would be able to: store data in a file. access data record by record from the file. move pointer within the file

After going through this lesson, you would be able to: store data in a file. access data record by record from the file. move pointer within the file 16 Files 16.1 Introduction At times it is required to store data on hard disk or floppy disk in some application program. The data is stored in these devices using the concept of file. 16.2 Objectives

More information

CSC 138 Structured Programming CHAPTER 4: TEXT FILE [PART 1]

CSC 138 Structured Programming CHAPTER 4: TEXT FILE [PART 1] CSC 138 Structured Programming CHAPTER 4: TEXT FILE [PART 1] LEARNING OBJECTIVES Upon completion, you should be able to: o define C++ text files o explain the benefits of using I/O file processing o explain

More information

C++ files and streams. Lec 28-31

C++ files and streams. Lec 28-31 C++ files and streams Lec 28-31 Introduction So far, we have been using the iostream standard library, which provides cin and cout methods for reading from standard input and writing to standard output

More information

CS2141 Software Development using C/C++ Stream I/O

CS2141 Software Development using C/C++ Stream I/O CS2141 Software Development using C/C++ Stream I/O iostream Two libraries can be used for input and output: stdio and iostream The iostream library is newer and better: It is object oriented It can make

More information

C++ does not, as a part of the language, define how data are sent out and read into the program

C++ does not, as a part of the language, define how data are sent out and read into the program Input and Output C++ does not, as a part of the language, define how data are sent out and read into the program I/O implementation is hardware dependent The input and output (I/O) are handled by the standard

More information

C++ Binary File I/O. C++ file input and output are typically achieved by using an object of one of the following classes:

C++ Binary File I/O. C++ file input and output are typically achieved by using an object of one of the following classes: C++ Binary File I/O C++ file input and output are typically achieved by using an object of one of the following classes: ifstream for reading input only. ofstream for writing output only. fstream for reading

More information

Chapter 3 - Notes Input/Output

Chapter 3 - Notes Input/Output Chapter 3 - Notes Input/Output I. I/O Streams and Standard I/O Devices A. I/O Background 1. Stream of Bytes: A sequence of bytes from the source to the destination. 2. 2 Types of Streams: i. Input Stream:

More information

I/O Streams and Standard I/O Devices (cont d.)

I/O Streams and Standard I/O Devices (cont d.) 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 from the standard input device Learn how to use predefined

More information

C++ Input/Output: Streams

C++ Input/Output: Streams C++ Input/Output: Streams Basic I/O 1 The basic data type for I/O in C++ is the stream. C++ incorporates a complex hierarchy of stream types. The most basic stream types are the standard input/output streams:

More information

Page 1

Page 1 Virtual Functions (introduction) A virtual function is one that does not really exist but it appears real in some parts of the program. Virtual functions are advanced features of the object oriented programming

More information

Streams - Object input and output in C++

Streams - Object input and output in C++ Streams - Object input and output in C++ Dr. Donald Davendra Ph.D. Department of Computing Science, FEI VSB-TU Ostrava Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input

More information

C++ Quick Guide. Advertisements

C++ Quick Guide. Advertisements C++ Quick Guide Advertisements Previous Page Next Page C++ is a statically typed, compiled, general purpose, case sensitive, free form programming language that supports procedural, object oriented, and

More information

Computer programs are associated to work with files as it helps in storing data & information permanently. File - itself a bunch of bytes stored on

Computer programs are associated to work with files as it helps in storing data & information permanently. File - itself a bunch of bytes stored on Computer programs are associated to work with files as it helps in storing data & information permanently. File - itself a bunch of bytes stored on some storage devices. In C++ this is achieved through

More information

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class C++ IO C++ IO All I/O is in essence, done one character at a time For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Concept: I/O operations act on streams

More information

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT Two Mark Questions UNIT - I 1. DEFINE ENCAPSULATION. Encapsulation is the process of combining data and functions

More information

Streams in C++ Stream concept. Reference information. Stream type declarations

Streams in C++ Stream concept. Reference information. Stream type declarations Stream concept A stream represent a sequence of bytes arriving, being retrieved, being stored, or being sent, in order. A stream is continuos and offer sequential access to the data. Each byte can be read

More information

Module 11 The C++ I/O System

Module 11 The C++ I/O System Table of Contents Module 11 The C++ I/O System CRITICAL SKILL 11.1: Understand I/O streams... 2 CRITICAL SKILL 11.2: Know the I/O class hierarchy... 3 CRITICAL SKILL 11.3: Overload the > operators...

More information

This chapter introduces the notion of namespace. We also describe how to manage input and output with C++ commands via the terminal or files.

This chapter introduces the notion of namespace. We also describe how to manage input and output with C++ commands via the terminal or files. C++ PROGRAMMING LANGUAGE: NAMESPACE AND MANGEMENT OF INPUT/OUTPUT WITH C++. CAAM 519, CHAPTER 15 This chapter introduces the notion of namespace. We also describe how to manage input and output with C++

More information

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University) Estd: 1994 JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli - 621014 (An approved by AICTE and Affiliated to Anna University) ISO 9001:2000 Certified Subject Code & Name : CS 1202

More information

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat Ch - 7. Data File Handling

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat Ch - 7. Data File Handling Introduction Data File Handling The fstream.h Header file Data Files Opening and Closing File Steps to process a File in your Program Changing the behavior of Stream Sequential I/O With Files Detecting

More information

DATA FILE HANDLING FILES. characters (ASCII Code) sequence of bytes, i.e. 0 s & 1 s

DATA FILE HANDLING FILES. characters (ASCII Code) sequence of bytes, i.e. 0 s & 1 s DATA FILE HANDLING The Language like C/C++ treat everything as a file, these languages treat keyboard, mouse, printer, Hard disk, Floppy disk and all other hardware as a file. In C++, a file, at its lowest

More information

Lecture 9. Introduction

Lecture 9. Introduction Lecture 9 File Processing Streams Stream I/O template hierarchy Create, update, process files Sequential and random access Formatted and raw processing Namespaces Lec 9 Programming in C++ 1 Storage of

More information

Writing a Good Program. 7. Stream I/O

Writing a Good Program. 7. Stream I/O Writing a Good Program 1 Input and Output I/O implementation is hardware dependent C++ does not, as a part of the language, define how data are sent out and read into the program The input and output (I/O)

More information

File I/O Christian Schumacher, Info1 D-MAVT 2013

File I/O Christian Schumacher, Info1 D-MAVT 2013 File I/O Christian Schumacher, chschuma@inf.ethz.ch Info1 D-MAVT 2013 Input and Output in C++ Stream objects Formatted output Writing and reading files References General Remarks I/O operations are essential

More information

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools 1 IS 0020 Program Design and Software Tools Stack/Queue - File Processing Lecture 10 March 29, 2005 Introduction 2 Storage of data Arrays, variables are temporary Files are permanent Magnetic disk, optical

More information

Input and Output File (Files and Stream )

Input and Output File (Files and Stream ) Input and Output File (Files and Stream ) BITE 1513 Computer Game Programming Week 14 Scope Describe the fundamentals of input & output files. Use data files for input & output purposes. Files Normally,

More information

Developed By : Ms. K. M. Sanghavi

Developed By : Ms. K. M. Sanghavi Developed By : Ms. K. M. Sanghavi Designing Our Own Manipulators We can design our own manipulators for certain special purpose.the general form for creating a manipulator without any arguments is: ostream

More information

BITG 1113: Files and Stream LECTURE 10

BITG 1113: Files and Stream LECTURE 10 BITG 1113: Files and Stream LECTURE 10 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the fundamentals of input & output files. 2. Use data files for input & output

More information

ios ifstream fstream

ios ifstream fstream File handling in C++ In most of the real time programming problems we need to store the data permanently on some secondary storage device so that it can be used later. Whenever we have to store the data

More information

File I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents

File I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents File I/O 1 File Names and Types A file name should reflect its contents Payroll.dat Students.txt Grades.txt A file s extension indicates the kind of data the file holds.dat,.txt general program input or

More information

Fall 2017 CISC/CMPE320 9/27/2017

Fall 2017 CISC/CMPE320 9/27/2017 Notices: CISC/CMPE320 Today File I/O Text, Random and Binary. Assignment 1 due next Friday at 7pm. The rest of the assignments will also be moved ahead a week. Teamwork: Let me know who the team leader

More information

UEE1303(1070) S 12 Object-Oriented Programming in C++

UEE1303(1070) S 12 Object-Oriented Programming in C++ Computational Intelligence on Automation Lab @ NCTU Learning Objectives UEE1303(1070) S 12 Object-Oriented Programming in C++ Lecture 06: Streams and File Input/Output I/O stream istream and ostream member

More information

Random File Access. 1. Random File Access

Random File Access. 1. Random File Access Random File Access 1. Random File Access In sequential file access, the file is read or written sequentially from the beginning. In random file access, you can skip around to various points in the file

More information

Week 3: File I/O and Formatting 3.7 Formatting Output

Week 3: File I/O and Formatting 3.7 Formatting Output Week 3: File I/O and Formatting 3.7 Formatting Output Formatting: the way a value is printed: Gaddis: 3.7, 3.8, 5.11 CS 1428 Fall 2014 Jill Seaman spacing decimal points, fractional values, number of digits

More information

Advanced I/O Concepts

Advanced I/O Concepts Advanced Object Oriented Programming Advanced I/O Concepts Seokhee Jeon Department of Computer Engineering Kyung Hee University jeon@khu.ac.kr 1 1 Streams Diversity of input sources or output destinations

More information

Physical Files and Logical Files. Opening Files. Chap 2. Fundamental File Processing Operations. File Structures. Physical file.

Physical Files and Logical Files. Opening Files. Chap 2. Fundamental File Processing Operations. File Structures. Physical file. File Structures Physical Files and Logical Files Chap 2. Fundamental File Processing Operations Things you have to learn Physical files and logical files File processing operations: create, open, close,

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

Objects and streams and files CS427: Elements of Software Engineering

Objects and streams and files CS427: Elements of Software Engineering Objects and streams and files CS427: Elements of Software Engineering Lecture 6.2 (C++) 10am, 13 Feb 2012 CS427 Objects and streams and files 1/18 Today s topics 1 Recall...... Dynamic Memory Allocation...

More information

COMP322 - Introduction to C++

COMP322 - Introduction to C++ COMP322 - Introduction to C++ Lecture 05 - I/O using the standard library, stl containers, stl algorithms Dan Pomerantz School of Computer Science 5 February 2013 Basic I/O in C++ Recall that in C, we

More information

CSc Introduc/on to Compu/ng. Lecture 19 Edgardo Molina Fall 2011 City College of New York

CSc Introduc/on to Compu/ng. Lecture 19 Edgardo Molina Fall 2011 City College of New York CSc 10200 Introduc/on to Compu/ng Lecture 19 Edgardo Molina Fall 2011 City College of New York 18 Standard Device Files Logical file object: Stream that connects a file of logically related data to a program

More information

Fundamentals of Programming Session 27

Fundamentals of Programming Session 27 Fundamentals of Programming Session 27 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel s slides Sharif University of Technology Outlines

More information

10/23/02 21:20:33 IO_Examples

10/23/02 21:20:33 IO_Examples 1 Oct 22 22:07 2000 extractor1.c Page 1 istream &operator>>( istream &in, Point &p ){ char junk; in >> junk >> p.x >> junk >> p.y >> junk; return in; 2 Oct 22 22:07 2000 extractor2.c Page 1 istream &operator>>(

More information

Introduction to C++ (Extensions to C)

Introduction to C++ (Extensions to C) Introduction to C++ (Extensions to C) C is purely procedural, with no objects, classes or inheritance. C++ is a hybrid of C with OOP! The most significant extensions to C are: much stronger type checking.

More information

Downloaded from

Downloaded from DATA FILE HANDLING IN C++ Key Points: Text file: A text file stores information in readable and printable form. Each line of text is terminated with an EOL (End of Line) character. Binary file: A binary

More information

COMP322 - Introduction to C++

COMP322 - Introduction to C++ COMP322 - Introduction to C++ Winter 2011 Lecture 05 - I/O using the standard library & Introduction to Classes Milena Scaccia School of Computer Science McGill University February 1, 2011 Final note on

More information

Chapter 12: Advanced File Operations

Chapter 12: Advanced File Operations Chapter 12: Advanced File Operations 12.1 File Operations File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to files Used in many applications:

More information

Chapter 14 Sequential Access Files

Chapter 14 Sequential Access Files Chapter 14 Sequential Access Files Objectives Create file objects Open a sequential access file Determine whether a sequential access file was opened successfully Write data to a sequential access file

More information

UNIT V FILE HANDLING

UNIT V FILE HANDLING UNIT V CONTENTS: Streams and formatted I/O I/O manipulators File handling Random access Object serialization Namespaces Std namespace ANSI String Objects Standard template library FILE HANDLING Streams:

More information

QUESTION BANK. SUBJECT CODE / Name: CS2311 OBJECT ORIENTED PROGRAMMING

QUESTION BANK. SUBJECT CODE / Name: CS2311 OBJECT ORIENTED PROGRAMMING QUESTION BANK DEPARTMENT:EEE SEMESTER: V SUBJECT CODE / Name: CS2311 OBJECT ORIENTED PROGRAMMING UNIT III PART - A (2 Marks) 1. What are the advantages of using exception handling? (AUC MAY 2013) In C++,

More information

Chapter 8 File Processing

Chapter 8 File Processing Chapter 8 File Processing Outline 1 Introduction 2 The Data Hierarchy 3 Files and Streams 4 Creating a Sequential Access File 5 Reading Data from a Sequential Access File 6 Updating Sequential Access Files

More information

I BSc(IT) [ Batch] Semester II Core: Object Oriented Programming With C plus plus - 212A Multiple Choice Questions.

I BSc(IT) [ Batch] Semester II Core: Object Oriented Programming With C plus plus - 212A Multiple Choice Questions. Dr.G.R.Damodaran College of Science (Autonomous, affiliated to the Bharathiar University, recognized by the UGC)Reaccredited at the 'A' Grade Level by the NAAC and ISO 9001:2008 Certified CRISL rated 'A'

More information

Chapter 12. Streams and File I/O. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Chapter 12. Streams and File I/O. Copyright 2010 Pearson Addison-Wesley. All rights reserved Chapter 12 Streams and File I/O Copyright 2010 Pearson Addison-Wesley. All rights reserved Learning Objectives I/O Streams File I/O Character I/O Tools for Stream I/O File names as input Formatting output,

More information

Study Material for Class XII. Data File Handling

Study Material for Class XII. Data File Handling Study Material for Class XII Page 1 of 5 Data File Handling Components of C++ to be used with handling: Header s: fstream.h Classes: ifstream, ofstream, fstream File modes: in, out, in out Uses of cascaded

More information

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay Input and Output Data Processing Course, I. Hrivnacova, IPN Orsay Output to the Screen Input from the Keyboard IO Headers Output to a File Input from a File Formatting I. Hrivnacova @ Data Processing Course

More information

Streams contd. Text: Chapter12, Big C++

Streams contd. Text: Chapter12, Big C++ Streams contd pm_jat@daiict.ac.in Text: Chapter12, Big C++ Streams Objects are Abstracted Wrapper around input/output source/destinations Steps in reading/writing streams- Open: Establish connection between

More information

System Design and Programming II

System Design and Programming II System Design and Programming II CSCI 194 Section 01 CRN: 10968 Fall 2017 David L. Sylvester, Sr., Assistant Professor Chapter 12 Advanced File Operation File Operations A file is a collection of data

More information

Advanced File Operations. Review of Files. Declaration Opening Using Closing. CS SJAllan Chapter 12 2

Advanced File Operations. Review of Files. Declaration Opening Using Closing. CS SJAllan Chapter 12 2 Chapter 12 Advanced File Operations Review of Files Declaration Opening Using Closing CS 1410 - SJAllan Chapter 12 2 1 Testing for Open Errors To see if the file is opened correctly, test as follows: in.open("cust.dat");

More information

EP241 Computing Programming

EP241 Computing Programming EP241 Computing Programming Topic 9 File Management Department of Engineering Physics University of Gaziantep Course web page www.gantep.edu.tr/~bingul/ep241 Sep 2013 Sayfa 1 Overview of Streams in C++

More information

Object Oriented Programming

Object Oriented Programming Object Oriented Programming Course Title: Object Oriented Programming Full Marks: 60 20 20 Course No: CSC161 Pass Marks: 24 8 8 Nature of Course: Theory Lab Credit Hrs: 3 Semester: II Course Description:

More information

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018 C++ Basics Lecture 2 COP 3014 Spring 2018 January 8, 2018 Structure of a C++ Program Sequence of statements, typically grouped into functions. function: a subprogram. a section of a program performing

More information

Chapter 12. Streams and File I/O. Copyright 2016 Pearson, Inc. All rights reserved.

Chapter 12. Streams and File I/O. Copyright 2016 Pearson, Inc. All rights reserved. Chapter 12 Streams and File I/O Copyright 2016 Pearson, Inc. All rights reserved. Learning Objectives I/O Streams File I/O Character I/O Tools for Stream I/O File names as input Formatting output, flag

More information

Fig: iostream class hierarchy

Fig: iostream class hierarchy Unit 6: C++ IO Systems ================== Streams: Θ A stream is a logical device that either produces or consumes information. Θ A stream is linked to a physical device by the I/O system. Θ All streams

More information

More File IO. CIS 15 : Spring 2007

More File IO. CIS 15 : Spring 2007 More File IO CIS 15 : Spring 2007 Functionalia Office Hours Today 2 to 3pm - 0317 N (Bridges Room) HW 2 due on Sunday March 11, 11:59pm Note: Midterm is on MONDAY, March 12th Review: Thursday Today: Survey

More information

File handling Basics. Lecture 7

File handling Basics. Lecture 7 File handling Basics Lecture 7 What is a File? A file is a collection of information, usually stored on a computer s disk. Information can be saved to files and then later reused. 2 File Names All files

More information

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FACULTY INFORMATION TECHNOLOGY AND COMMUNICATION (FTMK) BITE 1513 GAME PROGRAMMING I.

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FACULTY INFORMATION TECHNOLOGY AND COMMUNICATION (FTMK) BITE 1513 GAME PROGRAMMING I. y UNIVERSITI TEKNIKAL MALAYSIA MELAKA FACULTY INFORMATION TECHNOLOGY AND COMMUNICATION (FTMK) BITE 1513 GAME PROGRAMMING I Lab Module 7 CLASSES, INHERITANCE AND POLYMORPHISM Department of Media Interactive

More information

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 I/O Streams as an Introduction to Objects and Classes Overview 6.1 Streams and Basic File I/O 6.2 Tools for Stream I/O 6.3 Character I/O Slide 6-3 6.1 Streams and Basic File I/O I/O Streams I/O

More information

Lecture 5 Files and Streams

Lecture 5 Files and Streams Lecture 5 Files and Streams Introduction C programs can store results & information permanently on disk using file handling functions These functions let you write either text or binary data to a file,

More information

Getting started with C++ (Part 2)

Getting started with C++ (Part 2) Getting started with C++ (Part 2) CS427: Elements of Software Engineering Lecture 2.2 11am, 16 Jan 2012 CS427 Getting started with C++ (Part 2) 1/22 Outline 1 Recall from last week... 2 Recall: Output

More information

Chapter 21 - C++ Stream Input/Output

Chapter 21 - C++ Stream Input/Output Chapter 21 - C++ Stream Input/Output Outline 21.1 Introduction 21.2 Streams 21.2.1 Iostream Library Header Files 21.2.2 Stream Input/Output Classes and Objects 21.3 Stream Output 21.3.1 Stream-Insertion

More information

We will exclusively use streams for input and output of data. Intro Programming in C++

We will exclusively use streams for input and output of data. Intro Programming in C++ C++ Input/Output: Streams The basic data type for I/O in C++ is the stream. C++ incorporates a complex hierarchy of stream types. The most basic stream types are the standard input/output streams: 1 istream

More information

Chapter Overview. I/O Streams as an Introduction to Objects and Classes. I/O Streams. Streams and Basic File I/O. Objects

Chapter Overview. I/O Streams as an Introduction to Objects and Classes. I/O Streams. Streams and Basic File I/O. Objects Chapter 6 I/O Streams as an Introduction to Objects and Classes Overview 6.1 Streams and Basic File I/O 6.2 Tools for Stream I/O 6.3 Character I/O Copyright 2008 Pearson Addison-Wesley. All rights reserved.

More information

Input/Output Streams: Customizing

Input/Output Streams: Customizing DM560 Introduction to Programming in C++ Input/Output Streams: Customizing Marco Chiarandini Department of Mathematics & Computer Science University of Southern Denmark [Based on slides by Bjarne Stroustrup]

More information

The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types. The C++ Language Input and Output Output! Output is information generated by a program.! Frequently sent the screen or a file.! An output stream is used to send information. Another type supplied by C++

More information

Polymorphism Part 1 1

Polymorphism Part 1 1 Polymorphism Part 1 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid

More information

Today in CS162. External Files. What is an external file? How do we save data in a file? CS162 External Data Files 1

Today in CS162. External Files. What is an external file? How do we save data in a file? CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file? CS162 External Data Files 1 External Files So far, all of our programs have used main memory to temporarily store

More information

Text File I/O. #include <iostream> #include <fstream> using namespace std; int main() {

Text File I/O. #include <iostream> #include <fstream> using namespace std; int main() { Text File I/O We can use essentially the same techniques we ve been using to input from the keyboard and output to the screen and just apply them to files instead. If you want to prepare input data ahead,

More information

Chapter 12. Streams and File I/O

Chapter 12. Streams and File I/O Chapter 12 Streams and File I/O Learning Objectives I/O Streams File I/O Character I/O Tools for Stream I/O File names as input Formatting output, flag settings Introduction Streams Special objects Deliver

More information

Introduction. Lecture 5 Files and Streams FILE * FILE *

Introduction. Lecture 5 Files and Streams FILE * FILE * Introduction Lecture Files and Streams C programs can store results & information permanently on disk using file handling functions These functions let you write either text or binary data to a file, and

More information

basic_fstream<chart, traits> / \ basic_ifstream<chart, traits> basic_ofstream<chart, traits>

basic_fstream<chart, traits> / \ basic_ifstream<chart, traits> basic_ofstream<chart, traits> The C++ I/O System I/O Class Hierarchy (simplified) ios_base ios / \ istream ostream \ / iostream ifstream fstream ofstream The class ios_base -- public variables and methods The derived classes istream,

More information

Physics 6720 I/O Methods October 30, C++ and Unix I/O Streams

Physics 6720 I/O Methods October 30, C++ and Unix I/O Streams Physics 6720 I/O Methods October 30, 2002 We have been using cin and cout to handle input from the keyboard and output to the screen. In these notes we discuss further useful capabilities of these standard

More information

BITG 1233: Introduction to C++

BITG 1233: Introduction to C++ BITG 1233: Introduction to C++ 1 Learning Outcomes At the end of this lecture, you should be able to: Identify basic structure of C++ program (pg 3) Describe the concepts of : Character set. (pg 11) Token

More information

Lecture 3 The character, string data Types Files

Lecture 3 The character, string data Types Files Lecture 3 The character, string data Types Files The smallest integral data type Used for single characters: letters, digits, and special symbols Each character is enclosed in single quotes 'A', 'a', '0',

More information

C++ Input/Output Chapter 4 Topics

C++ Input/Output Chapter 4 Topics Chapter 4 Topics Chapter 4 Program Input and the Software Design Process Input Statements to Read Values into a Program using >>, and functions get, ignore, getline Prompting for Interactive Input/Output

More information

Chapter 21 - C++ Stream Input/Output

Chapter 21 - C++ Stream Input/Output Chapter 21 - C++ Stream Input/Output Outline 21.1 Introduction 21.2 Streams 21.2.1 Iostream Library Header Files 21.2.2 Stream Input/Output Classes and Objects 21.3 Stream Output 21.3.1 Stream-Insertion

More information

Stream States. Formatted I/O

Stream States. Formatted I/O C++ Input and Output * the standard C++ library has a collection of classes that can be used for input and output * most of these classes are based on a stream abstraction, the input or output device is

More information

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords. Chapter 1 File Extensions: Source code (cpp), Object code (obj), and Executable code (exe). Preprocessor processes directives and produces modified source Compiler takes modified source and produces object

More information

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team Question No: 1 ( Marks: 2 ) Write a declaration statement for an array of 10

More information

Come and join us at WebLyceum

Come and join us at WebLyceum Come and join us at WebLyceum For Past Papers, Quiz, Assignments, GDBs, Video Lectures etc Go to http://www.weblyceum.com and click Register In Case of any Problem Contact Administrators Rana Muhammad

More information

by Pearson Education, Inc. All Rights Reserved. 2

by Pearson Education, Inc. All Rights Reserved. 2 The C++ standard libraries provide an extensive set of input/output capabilities. C++ uses type-safe I/O. Each I/O operation is executed in a manner sensitive to the data type. If an I/O member function

More information

DISK FILE PROGRAM. ios. ofstream

DISK FILE PROGRAM. ios. ofstream [1] DEFINITION OF FILE A file is a bunch of bytes stored on some storage media like magnetic disk, optical disk or solid state media like pen-drive. In C++ a file, at its lowest level is interpreted simply

More information

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter

More information