DISK FILE PROGRAM. ios. ofstream

Size: px
Start display at page:

Download "DISK FILE PROGRAM. ios. ofstream"

Transcription

1 [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 as a sequence, or stream of bytes AND, a file the user level is sequence of intermixed data types characters, mathematical values, class objects etc. FILE STREAM CLASSES Stream is a flow of data which is a sequence of bytes. Different streams are used to represent different kinds of data flow. Each stream is associated with a particular class, which contains member functions and definitions for dealing with that particular kind of data flow. The stream that supplies data to the program is known as input stream. It reads the data from the file and hands it over to the program. The stream that receives data from the program is known as output stream. It writes the received data to the file. It clear form the following figure: DISK FILE Output stream Input stream Data output PROGRAM Data input to Program The file I/O system contains a set of classes that define the file handling method (functions). These classes, designed to manage the disk files are declared in fstream.h, therefore, we must include this file in a program that works with files. The classes defined inside fstream.h are derived from classes under iostream.h (This header file manages console I/O operations). The following figure shows the stream class hierarchy. get( ), getline( ), read( ) seekg( ), tellg( ) ios Put( ), write( ), seekp( ),tellp( ) Console I/O istream ostream streambuf iostream iostream.h file File I/O fstreambase ifstream fstream ofstream filebuf close( ) open( ) DATA FILE The data files are the files that store data permanently pertaining to a specific application, for later use. The data files cab be stored as text files or as binary files. This tutorial is written by: Er. A. Nasra Page-1

2 [2] A text file stores information in ASCII characters. Each line of text is terminated or delimited with special character known as EOL character. Internal translation takes palace when EOL character is read or written. A binary file is just a file that contains information in the same format in which the information is held in memory. There is no delimiter in binary file, and no translation takes place. As a result, binary files are faster and easier for a program to access, read and write than text files. Unless we specify, files are by default are considered and treated as text files. OPENING AND CLOSING A FILE(TEXT FILE) Opening of a file is done in two ways: (i) Using the constructor function of the stream class (for single file) (ii) Using the function open( ) (for multiple files) Closing of a file is done by disconnectint it with the stream it is associated with. This is accomplished by the following statement: stream_object close( ) ; For example, if a file hdfile is connected with an ofstream, then it can be closed as: hdfile.close( ) ; The function close( ) flushes the buffer befor terminating the connection of the file with the stream. OPENING A FILE USING CONSTRUCTORS Let us suppose that we have to open a file named think.dat containing following text: Take your happiness to new level with, Transcend s new PF730 Digital Phot Frame. We open this file by using the constructgor of ofstream class, with thye help of following statement: ofstream hdfile( think.dat ) This statement opens an output stream called hdfile and initialize it with the name think.dat. The think.dat is the name of the file lying on the hard-disk, but in program it will be recognized as HD File. Hence, whatever is written in hdfile stream in the program, will actually be written into the file think.dat. The following program creates the required fiel: #include<iostream.h> //for file I/O main( ) { ofstream hdfile( think.dat ) ; if(!hdfile) { cout<< Can t open this file ; hdfile<< Take your happiness to new level with,\n ; hdfile<< Transcend s new PF730 Digital Phot Frame. ; hdfile.close( ) ; //closing the file THE FUNCTION get( ) This function is same as the operator >>, with the exception that it accepts white or blank spaces in the input string. It stops at \n. This function is used in two ways: (i) get(ch) ; which reads one character at a time form the stream. (ii) get(ch, n) ; which reads a string of character of length (n 1). Example: // This program reads a file named message.dat. int main( ) { ifstream yfile( message.dat ) ; char row[90], ch ; int i ; if(!yile) This tutorial is written by: Er. A. Nasra Page-2

3 [3] { cout<< Can t open file ; for(i = 1 ; i <= 4 ; i++) { yfile.get(row, 80) ; // read a line form the file. yfile.get(ch) ; // lread \n cout << row << /n ; // display the line on screen. yfile.close( ) ; THE FUNCTION getline( ) The format of this function is : getline(ch, size) ; It reads the characters from a line till it encounters \n or till(size 1). Example: // this program reads a file named message.dat int main( ) { ifstream yfile( message.dat ) ; char row[80] ; int i ; if(!yile) { cout<< Can t open file ; for(i = 1 ; i <= 4 ; i++) { yfile.getline(row, 80) ; // read a line form the file. cout << row << /n ; // display the line on screen. yfile.close( ) ; THE FUNCTION put( ) This function writes a single character at a time to the output stream. Fro example the statement afile.put( M ) ; will write a character M into the file named afile. Similarly, the statement bfile.put(v) ; will write the character content of character-variable v into the file named bfile. Example: //this program writes a line of text in a file character by character and displays it. #include<conio.h> int main( ) { char fname[15] ; char text[ ] = Nasra s World of Study. ; char line[30] ; int i ; clrscr( ) ; cout<< Enter the name of the file to be opened ; cin >> fname ; ofstream xfile(fname) ; for( i = 0 ; I < 30 ; i++) { xfile.put( text[i ] ) ; //write a character into the file. xfile.put( \n ) ; xfile.close( ) ; ifstream afile(fname) ; //open the file in Input mode. This tutorial is written by: Er. A. Nasra Page-3

4 [4] afile.getline(line, 30) ;//reads a line form the file. cout<< \n << line ; //display it afile.close( ) ; OPENING A FILE BY USING THE FUNCTION open( ) This function is used to open a file in a program, when we specify the stream to be connected with the file. For illustration, a file named diary.dat can be opened for output by the following set of statements: ofstream afile ; //declares afile of type ofstream (output file stream) afile.open( diary.dat ) // assigns the file stream to the file named diary.dat Thus, in the program the file diary.dat would be identified as afile. The major benefit of this method of opening a file is that more than one files can be opened at a time in a program. Example: //A program that opens an input stream called myfile and associate it with the textfile whose exact copy is to be created. An output stream called docfile will be opened and associated woth the new textfile containing the desired copy. #include<conio.h> main( ) { char infile[15], outfile[15] ; char ch ; clrscr( ) ; cout << \n Enter the input file name ; cin >> infile ; ifstream myfile ; myfile.open(infile) ; if(!myfile) { cout<< \ncan t open input file ; cout<< \n Enter the output file name ; cin >>outfile ; ofstream docfile ; docfile.open(outfile) ; if(!docfile) { cout << \ncan t open input file ; while(myfile) { myfile.get(ch) ; docfile.put(ch) ; myfile.close( ) ; docfike.close( ) ; THE FUNCTION read( ) AND write( ), (BINARY FILES) These functions read and write block of binary data. The general form of these functions are: istream & read((char*) & buf, int sizeof(buf)) ; ostream & write((char*) & buf, int sizeof(buf)) ; The read( ) function reads sizeof(buf) (it can any other integer value also) in bytes from the associated stream and puts them in the buffer pointed to by buf. The write( ) write function writes sizeof(buf) ( it can be any other integer value also) in bytes to the associated stream from the buffer pointed to by buf. This tutorial is written by: Er. A. Nasra Page-4

5 [5] Example:// A program which creates a file with record structure (Name, Roll, Marks in 4 subject, Total marks) and stores the data of all the students of a class in the file. The name of the file is provided by the user. #include<conio.h> main( ) { struct student //declaration of structure. { char name[20] ; int roll, sub1, sub2, sub3, sub4, total ; ; student srec; //declaration of structure variable. int n, i, size ; char fname[15] ;//variable to store file name. cout<< Enter the name of the file to be created ; cin >> fname ; ofstream ofile ; ofile.open(fname) ; //opening the file in output mode. If(!ofile) { cout<< Can t open the file ; size = sizeof(student) ; //computation of size of structure. cout<< Enter the number of student of in the class ; cin >>n ; for ( i = 0 ; i < n ; i++) { clrscr( ) ; cout<< Name : ; cin >>srec.name ; cout<< Roll : ; cin >>srec.roll ; cout<< Sub1 : ; cin >>srec. Sub1 ; cout<< Sub2 : ; cin >>srec. Sub2 ; cout<< Sub3 : ; cin >>srec. Sub3 ; cout<< Sub4 : ; cin >>srec. Sub4 ; srec/tpta; = srec.sub1 + srec.sub2 + srec.sub3 + srec.sub4 ; ofile.write((char*) &srec, size) ; //writing into file. ofile.close( ) ; DETECTING EOF We can detect the end of the file (EOF) by suing the member function eof( ) which has the prototype: int eof( ) ; It returns nonzero when the end of the file has been reached, otherwise it returns zero. For instance, let us consider the following code fragment: ifstream fin ; fin.open( master ) ; while(!fine.eof( ) ) //as long as eof( ) is zero {... //that is, file s end is not reached, process the file.... if( fin.eof( ) ) // if nonzero cout<< End of file is reacher!!\n ; To detect end of file, without using eof( ), we may check whether the stream object has become Null or not e.g., This tutorial is written by: Er. A. Nasra Page-5

6 [6] ifstream fin ; fin.open ( Master ) ; while(!fin) // as long as eof( ) is zero { // that is, the file s end is not reached // process the file. THE CONCEPT OF FILE MODES The filemode describes how a file is to be used i.e. to read from it, to write to it, to append it, and so on. When a stream is associated with a file, either by initializing a file stream object with a file name or by using the open( ) method, the filemode, is of type int, and can be chosen from several constants defined in the ios class. Following table lista the filemodes available with their meaning. S.N. Filemode Parameter Meaning Stream 1. ios : : in It opens file for reading (in input mode) ifstream 2. ios : : out It opens file for writing (in output mode). This also opens the file in ios : : trunc mode, by default. This means an existing file is truncated when opened i.e. its previous contents are discarded. ofstream 3. ios : : ate This seeks to eof upon opening of the file. I/O operations can ofstream still occur anywhere within the file. ifstream 4. ios : : app This appends the data to eof. ofstream 5. ios : : trunc This causes the contents of a pre-existing file by the same name to be destroyed and truncates the file to zero length. ofstream 6. ios : : nocreate This caused the open( ) function to fail if the file does not ofstream already exist. It will not create a new file with that name. 7. ios : : noreplace This caused the open( ) function to file if the file already exists. This used when you want to create a new file and at the same time. ofstream 8. ios : : binary This causes a file to be opened in binary mode (By default, files are opened in text mode.) ofstream ifstream 9. ios : : beg Refers to the beginning of the file. 10. ios : : cur Refers to current position in the file. 11. ios : : end Refers to end of the file. Note: ifstream open( ) has default value ios : : in ofstream open( ) has default value ios : : out fstream open( ) has no default value. Two or more filemode constants can be combined using bitwise OR operator ( ). For example: ofstream fout ; fout.open( Master, ios : : app ios : : nocreate) ; will open a file in the append mode if the file exists or open( ) will fail if the file already does not exists. To open a binary file ios : : binary is specified along with the file mode, e.g., fout.open( master, ios : : app ios : : binary) ; OR fout.open( main, ios : : out ios : : binary ) ; FILE POINTERS AND RANDOM ACCESS Every file maintains two pointers called get_pointer (in input file mode) and put_pointer (in output file mode) which tell the current location in the file where writing or reading will go on. These pointers help manage random access in file. In C++ random access is achieved with the help of seekg( ), seekp( ), tellg( ) and tellp( ). This tutorial is written by: Er. A. Nasra Page-6

7 [7] Function seekg( ) seekp( ) tellg( ) tellp( ) Working Sets current get position in a stream. It works for ifstream object. Sets current put position in a stream. It works for ofstream object. Returns the current get position in a stream. It works for ifstream object. Returns the corrent put position in a stream. It works for ofstream object. Example: ifstreaqm fin ; ofstream fout ; fin.seekg(40) ; // will move the get_pointer to byte number 40 in the file. fout.seekg(40) ; // will move the put_pointer to byte number 40 in the file. The functions seekg( ) or seekp( ) can be used in other form also, as given below: fin.seekg(40, ios : : beg) ; // go the byte no. 40 from beginning to the file linked with fin. Fin.seekg( -4, ios : : cur) ; // back up 4 bytes form current position of get_poiinter. Fin.seekg(0, ios : : end) ; // got the end of the file. Fin.seekg( -7, ios : : end) ; // backup 7 bytes form end of the file. SEARCHING OPERATIONS ON BINARY FILE The search procedure in C++ can be done in two ways: (i) with records implemented through structures. (ii) with records implemented through classes. Let us understand this issue through following examples: Example: // program to search in a file having records maintained through structures. sturct cloth { int clcode ; //cloth code; char name[25] ; char brand[20] ; float price ; cl1 ; void main( ) { int clc ; char found = n ; //clc cloth code. ifstream cfile( cloth.dat, ios : : in) ; // file cloth.dat must exist on disk. cout << Enter cloth code to be search for: ; cin >> clc ; while(!fin.eof( )) { cfile.read((char*) & cl1, sizeof(cl1)) ; if(cl1.clcode == clc) { cout<< cl1.name <<, clcode << clc << has price of Rs. <<cl1.price << only ; found = y ; break ; if(found == n ) cout<< cloth code not found in file!!! <<endl ; cfile.close( ) ; Example: // program to search in a file having records maintained thought classes. #include<fstrea.h> class stu { int rollno ; char name[25] ; char Class[4] ; float marks ; char grade ; public: void getdata( ) { cout << Rollno = ; cin >> rollno ; This tutorial is written by: Er. A. Nasra Page-7

8 [8] cout << Name = ; cin >> name ; cout << Class = ; cin >> Class ; cout << Marks = ; cin >> marks ; if (marks >= 75) grade = A ; else if(marks.= 60) grade = B ; else if(marks.= 50) grade = C ; else if(marks.= 40) grade = D ; else grade = F ; void putdata( ) { cout << name <<, rollno << rollno << has << marks << % mrks and << grade << grade << endl ; int getrno( ) //accessro function. { return rollno ; s1 ; void main( ) { int rn ; char found = n ; ifstream fi( stu.dat, ios : : in) ; //stu.dat must exist in disk. cout << Enter rollno to be searche for ; cin >> rn ; while(!fi.eof( )) { fi.reaed((char*) &s1, sizeof(s1)) ; if(s1.getrno( ) == rn) { s1.putdata( ) ; found = y ; break ; if(found == n ) cout << Rollno not found in file!!! <<endl ; fi.close( ) ; UPDATING A FILE : RANDOM ACCESS Updating means the routine task in the maintenance of any data file. The updating includes one or more of the following tasks: Displaying the contents of a file. Modifying an existing item. Adding / Appending a new item. Deleting an existing item. Example: /* Let us first create a file called STOCK.dat and input some data. Then in the next program we will learn the updating of the same file. */ // This program illustrates how class objects can be written to and read from the disk file. #include<iostream.h> #include<iomanip.h> class INVENTORY { char name[10] ; // item name int code ; // item code float cost ; // cost of each item public: void readdata( ) ; void writedata( ) ; ; This tutorial is written by: Er. A. Nasra Page-8

9 [9] void INVENTORY : : readdata( ) // read from keyboard { cout << Enter name : ; cin >> name ; cout << Enter code : ; cin >> code ; cout << Enter cost : ; cin >> cost ; void INVERTORY : : writedata( ) // formatted display on screen { cout << setiosflags(ios : :left) << setw(10) << name ; << setiosflags(ios: : right) << setw(10) << code ; << setprecision(2) << setw(10) << cost ; << endl ; int main( ) { INVENTORY item[3] ; // declare arr of 3 objects fstream file ; // input and output file file.open( STOCK.dat, ios : : in ios : : out) ; cout << ENTER DETAILS FOR THREE ITEMS \n ; for( int i = 0 ; i < 3 ; i++) { item[ i ].readdata( ) ; file.write((char *) & item[i], sizeof(item[i])) ; file.seekg(0) ; // reset to start cout << \n OUTPUT \n\n ; for( i = 0 ; i < 3 ; i++) { file.read((char *) & item[i], sizeof(item[i])) ; item[i].writedata( ) ; file.close( ) ; The output of the above program would be: ENTER DETAILS FOR THREE ITEMS Enter name : C++ Enter code : 202 Enter cost : 1750 Enter name : FORTRAN Enter code : 204 Enter cost : 1550 Enter name : JAVA Enter code : 225 Enter cost : 2250 OUTPUT C FORTRAN JAVA /* Now, let us understand the program for the updating of the above file named STOCK.dat for five items and performing the operations of appending, modifying and displaying the contents of the file. */ #include<iostream.h> #include<iomanip.h> This tutorial is written by: Er. A. Nasra Page-9

10 [10] class INVENTORY { char name[10] ; // item name int code ; // item code float cost ; // cost of each item public: void getdata( ) { cout << Enter name : ; cin >> name ; cout << Enter code : ; cin >> code ; cout << Enter cost : ; cin >> cost ; void putdata( ) { cout << setiosflags(ios : :left) << setw(10) << name ; << setiosflags(ios: : right) << setw(10) << code ; << setprecision(2) << setw(10) << cost ; << endl ; ; // End of the class definition int main( ) { INVENTORY item ; fstream inoutfile ; // I/O stream inoutfile.open( STOCK.dat, ios : : ate ios : : in ios : : out ios : : binary) ; inoutfile.seekg(0, ios : : beg) ; // go to start cout << CURRENT CONTENTS OF STOCK << endl ; while(inoutfile.read((char *) & item, sizeof(item)) { item.putdata( ) ; inoutfile.clear( ) ; // turn off EOF flag /* Appending one more data */ cout << \n ADD AN ITEM \N ; item.getdata( ) ; char ch ; cin.get(ch) ; inoutfile.write((charr *) & item, sizeof(item)) ; // display the appended file inoutfile.seekg(0) ; // go to the start cout << CONTENTS OF APPENDED FIEL \N ; while(inoutile.ead((char *) & item, sizefo (item)) { item.putdata( ) ; // find number of objects in the file int last = inoutfile.tellg( ) ; int n = last/sizeof(item) ; cout << Number of objects = << n << endl ; cout << Total bytes in the file = << last << endl ; /* MODIFY THE DETAILS OF AN ITEM */ cout << Enter object number to be updated \n ; int object ; cin >> object ; cin.get(ch) ; int location = (object 1) * sizeof(item) ; if(inoutfile.eof( ) ) inoutfile.clear( ) ; inoutfile.seekg(location) ; cout << Ener new values of the object \n ; item.getdata( ) ; cin.get(ch) ; This tutorial is written by: Er. A. Nasra Page-10

11 [11] inoutile.write((char *) & item, sizeof(item)) << flush ; /* SHOW UPDATED FILE */ inoutile.seekg(0) ; // go to the start cout << CONTENTS OF UPDATED FILE \N ; while(inoutfile.read((char *) & item, sizeof(item)) { item.putdata( ) ; inoutfile.close( ) ; // end of main. The output of the above program will be: CURRENT CONTENTS OF STOCK AA BB CC DD XX ADD AN ITEM Enter name : ZZ Enter code : 33 Enter cost : 700 CONTENT OF APPENDED FILE AA BB CC DD XX ZZ Number of objects = 6 Total bytes in the files = 96 Enter object number to be updated 6 Enter new values of he object Enter name : YY Enter code : 66 Enter cost : 477 CONTENT OF UPDATED FILE AA BB CC DD XX YY This tutorial is written by: Er. A. Nasra Page-11

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Consider the following example where a base class has been derived by other two classes: 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

CS201 Latest Solved MCQs

CS201 Latest Solved MCQs Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability

More information

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Quiz Start Time: 09:34 PM Time Left 82 sec(s) Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability

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

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

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

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

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

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

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

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

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

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

A stream is infinite. File access methods. File I/O in C++ 4. File input/output David Keil CS II 2/03. The extractor and inserter form expressions

A stream is infinite. File access methods. File I/O in C++ 4. File input/output David Keil CS II 2/03. The extractor and inserter form expressions Topic: File input/output I. Streams II. Access methods III. C++ style Input, output, random access Stream classes: ifstream, ofstream IV. C style The FILE data type Opening files Writing to, reading text

More information

AC55/AT55 OBJECT ORIENTED PROGRAMMING WITH C++ DEC 2013

AC55/AT55 OBJECT ORIENTED PROGRAMMING WITH C++ DEC 2013 Q.2 a. Discuss the fundamental features of the object oriented programming. The fundamentals features of the OOPs are the following: (i) Encapsulation: It is a mechanism that associates the code and data

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

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

Streams. Rupesh Nasre.

Streams. Rupesh Nasre. Streams Rupesh Nasre. OOAIA January 2018 I/O Input stream istream cin Defaults to keyboard / stdin Output stream ostream cout std::string name; std::cout > name; std::cout

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

Module C++ I/O System Basics

Module C++ I/O System Basics 1 Module - 36 C++ I/O System Basics Table of Contents 1. Introduction 2. Stream classes of C++ 3. Predefined Standard Input/Output Streams 4. Functions of class 5. Functions of class

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

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

And Even More and More C++ Fundamentals of Computer Science

And Even More and More C++ Fundamentals of Computer Science And Even More and More C++ Fundamentals of Computer Science Outline C++ Classes Friendship Inheritance Multiple Inheritance Polymorphism Virtual Members Abstract Base Classes File Input/Output Friendship

More information

Software Design & Programming I

Software Design & Programming I Software Design & Programming I Starting Out with C++ (From Control Structures through Objects) 7th Edition Written by: Tony Gaddis Pearson - Addison Wesley ISBN: 13-978-0-132-57625-3 Chapter 3 Introduction

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

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING CS6456 OBJECT ORIENTED PROGRAMMING

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING CS6456 OBJECT ORIENTED PROGRAMMING DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING CS6456 OBJECT ORIENTED PROGRAMMING Unit I : OVERVIEW PART A (2 Marks) 1. Give some characteristics of procedure-oriented

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

Streams. Parsing Input Data. Associating a File Stream with a File. Conceptual Model of a Stream. Parsing. Parsing

Streams. Parsing Input Data. Associating a File Stream with a File. Conceptual Model of a Stream. Parsing. Parsing Input Data 1 Streams 2 Streams Conceptual Model of a Stream Associating a File Stream with a File Basic Stream Input Basic Stream Output Reading Single Characters: get() Skipping and Discarding Characters:

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

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

Programming II with C++ (CSNB244) Lab 10. Topics: Files and Stream

Programming II with C++ (CSNB244) Lab 10. Topics: Files and Stream Topics: Files and Stream In this lab session, you will learn very basic and most common I/O operations required for C++ programming. The second part of this tutorial will teach you how to read and write

More information

CS103L SPRING 2018 UNIT 7: FILE I/O

CS103L SPRING 2018 UNIT 7: FILE I/O CS103L SPRING 2018 UNIT 7: FILE I/O I/O STREAMS IN C++ I/O: short for input/ouput Older term from mainframe days for getting data into and out of your program C++ offers object oriented abstractions to

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

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

Sample Paper 2013 SUB: COMPUTER SCIENCE GRADE XII TIME: 3 Hrs Marks: 70

Sample Paper 2013 SUB: COMPUTER SCIENCE GRADE XII TIME: 3 Hrs Marks: 70 Sample Paper 2013 SUB: COMPUTER SCIENCE GRADE XII TIME: 3 Hrs Marks: 70 INSTRUCTIONS: All the questions are compulsory. i. Presentation of answers should be neat and to the point. iii. Write down the serial

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

Object Oriented Pragramming (22316)

Object Oriented Pragramming (22316) Chapter 1 Principles of Object Oriented Programming (14 Marks) Q1. Give Characteristics of object oriented programming? Or Give features of object oriented programming? Ans: 1. Emphasis (focus) is on data

More information

Object Oriented Programming CS250

Object Oriented Programming CS250 Object Oriented Programming CS250 Abas Computer Science Dept, Faculty of Computers & Informatics, Zagazig University arabas@zu.edu.eg http://www.arsaliem.faculty.zu.edu.eg Object Oriented Programming Principles

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

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

VuZs Team's Work. CS201 Spring Solved by vuzs Team with Reference Written by Administrator Wednesday, 19 May :52

VuZs Team's Work. CS201 Spring Solved by vuzs Team with Reference Written by Administrator Wednesday, 19 May :52 CS201 Spring2009 5 Solved by vuzs Team with Reference Written by Administrator Wednesday, 19 May 2010 17:52 MIDTERM EXAMINATION Spring 2009 CS201- Introduction to Programming Shared & Solved by vuzs Team

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

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

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

SUBMITTED AS A PART OF C.B.S.E. CURRICULUM FOR THE YEAR

SUBMITTED AS A PART OF C.B.S.E. CURRICULUM FOR THE YEAR SUBMITTED AS A PART OF C.B.S.E. CURRICULUM FOR THE YEAR 2008-09 CONTENTS CERTIFICATE ACKNOWLEDGEMENT PROJECT PREAMBLE PROJECT STUDY ALGORITHM SOURCE CODE OUTPUT CERTIFICATE This is to certify that, Roll

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

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

POINTERS - Pointer is a variable that holds a memory address of another variable of same type. - It supports dynamic allocation routines. - It can improve the efficiency of certain routines. C++ Memory

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

I Mid Semester May 2012 : Class XII : Computer Science Max Mark 50 : Time 2 Hrs. 1. a) What is macro in C++? Give example 2

I Mid Semester May 2012 : Class XII : Computer Science Max Mark 50 : Time 2 Hrs. 1. a) What is macro in C++? Give example 2 I Mid Semester May 01 : Class XII : Computer Science Max Mark 50 : Time Hrs 1. a) What is macro in C++? Give example b) Give the Header file for the following functions:- i) gets ( ) ii) tolower( ) 1 c)

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

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

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

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

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

SECTION A (15 MARKS) Answer ALL Questions. Each Question carries ONE Mark. 1 (a) Choose the correct answer: (10 Marks)

SECTION A (15 MARKS) Answer ALL Questions. Each Question carries ONE Mark. 1 (a) Choose the correct answer: (10 Marks) SECTION A (15 MARKS) Answer ALL Questions. Each Question carries ONE Mark. 1 (a) Choose the correct answer: (10 Marks) 1. The function is used to reduce function call a. Overloading b. Inline c. Recursive

More information

CPE Summer 2015 Exam I (150 pts) June 18, 2015

CPE Summer 2015 Exam I (150 pts) June 18, 2015 Name Closed notes and book. If you have any questions ask them. Write clearly and make sure the case of a letter is clear (where applicable) since C++ is case sensitive. You can assume that there is one

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

by Pearson Education, Inc. All Rights Reserved. 2

by Pearson Education, Inc. All Rights Reserved. 2 In Fig. 17.4, the file is to be opened for output, so an ofstream object is created. Two arguments are passed to the object s constructor the filename and the file-open mode (line 12). For an ofstream

More information

All About: File I/O in C++ By Ilia Yordanov, ; C++ Resources

All About: File I/O in C++ By Ilia Yordanov,  ; C++ Resources All About: File I/O in C++ By Ilia Yordanov, loobian@cpp-home.com www.cpp-home.com ; C++ Resources This tutorial may not be republished without a written permission from the author! Introduction This tutorial

More information

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010 CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011 Lectures 1-22 Moaaz Siddiq Asad Ali Latest Mcqs MIDTERM EXAMINATION Spring 2010 Question No: 1 ( Marks: 1 ) - Please

More information

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

File Operations. Lecture 16 COP 3014 Spring April 18, 2018 File Operations Lecture 16 COP 3014 Spring 2018 April 18, 2018 Input/Ouput to and from files File input and file output is an essential in programming. Most software involves more than keyboard input and

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

Understanding main() function Input/Output Streams

Understanding main() function Input/Output Streams Understanding main() function Input/Output Streams Structure of a program // my first program in C++ #include int main () { cout

More information

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points) Name Definition Matching (8 Points) 1. (8 pts) Match the words with their definitions. Choose the best definition for each word. Relational Expression Iteration Counter Count-controlled loop Loop Flow

More information

Name Section: M/W T/TH Number Definition Matching (8 Points)

Name Section: M/W T/TH Number Definition Matching (8 Points) Name Section: M/W T/TH Number Definition Matching (8 Points) 1. (8 pts) Match the words with their definitions. Choose the best definition for each word. Iteration Counter Event Counter Loop Abstract Step

More information

Consider the following statements. string str1 = "ABCDEFGHIJKLM"; string str2; After the statement str2 = str1.substr(1,4); executes, the value of str2 is " ". Given the function prototype: float test(int,

More information