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

Size: px
Start display at page:

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

Transcription

1 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 level, is interpreted simply as a sequence of (or stream of) bytes. Files in computers are of two types. One that stores instruction for the computer, i.e. a program file and the second that stores data, i.e. data file. FILES Program Files Date Files Text Files Binary Files data stored in the form of data stored in the form of characters (ASCII Code) sequence of bytes, i.e. 0 s & 1 s each line terminated with a special Character known as end-of-line there is no EOL character (EOL) character What is a Stream? A stream is a general name given to the flow of data. Different streams are used to represent different kind of data flow. Each stream is associated with a particular class of fstream.h header file of the standard library. The streams are of the following types: Type of stream input stream (Read mode) output stream (Write mode) input/output stream (R/W mode) Associated Class ifstream ofstream fstream Basic Operations on A Text File Creating or writing in file. Reading a text file and displaying contents. Manipulating the contents read from a text file. To be able to carry out the above basic operations on a file, the following sequence has to be followed: Open the file. Perform operation on the file. Close the file. Opening A File Before opening a file, we shall create a file stream object of a particular class (ifstream, ofstream or fstream) depending upon the type of operation. e.g. In order to open a file as an input file i.e. data will be read from it and no other operation would take place, we shall create a file stream object of ifstream type. Page 1

2 Similarly, in order to open an output file (on which no operation can take place except writing only), we shall create a file stream object of ofstream type. A file can be opened in two ways: Using the constructor function of the class (useful when we use only one file in the stream) e.g. ofstream outfile( marks.txt ); Using member function open() of the class (useful in case of multiple files) e.g. ofstream outfile; outfile.open( marks.txt ); Concept of File Modes The file mode describes how a file is to be used - to read from it, to write to it, to append it, and so on. File Mode Constants Meaning Associated Class ios::in Open for reading (default for ifstream) ifstream ios::out Open for writing (default for ofstream) ofstream ios::ate Start reading or writing at the end of file (AT End) ofstream ifstream ios::app Start writing at end of file (APPend) ofstream ios::trunk Truncate file to zero length if it exists (TRUNCate) ofstream ios::nocreate Error when opening if file does not already exist. ofstream ios::noreplace Error when opening for output if file already exist, unless ate or app is set. ofstream ios::binary Open file in binary (not text) mode. ofstream ifstream Note: 1) We can combine two or more file mode constants using the C++ bitwise OR ( ) operator. 2) The fstream class does not a mode by default and, therefore, one must specify the mode explicitly when using an object of the fstream class How is end-of-file detected in a file? The end of any file is checked with eof() function which is predefined in class ios of ifstream, ofstream and fstream classes. If the file pointer points to the end-of-file then condition is TRUE and the object returns zero otherwise it returns a non-zero value. File Pointers Each file has two pointers associated with it which are called file pointers. One of them is the input pointer, known as get pointer; and the other one is called output pointer or the put pointer. File Pointers get pointer (input pointer) put pointer (output pointer) Used for reading the contents of a file location Used for writing to a given file location NOTE: Each time an input or output operation takes place; the concerned pointer is automatically advanced. Page 2

3 Open for reading only Open in app mode Open for writing only I N D I A I N D I A GET POINTER PUT POINTER PUT POINTER How can the file pointers be moved in a file? The file stream classes support some predefined functions that navigate the position of the filepointers. The relevant functions are: seekg(), seekp(), tellg(), and tellp(). Function name seekg() seekp() tellg() tellp() Description Moves the get pointer or input pointer to a specified location Moves the put pointer or the output pointer to a specified ocation Gives the current position of the get pointer Gives the current position of the put pointer seekg() and seekp() are the functions used for manipulation of file pointers. e.g. ifile.seekg(30); This statement moves the get pointer to the byte number 30 in the file linked with ifile. Remember the counting of bytes in a file begins from zero. Another form of seek() function seekg( offset, reposition); seekp( offset, reposition); e.g. ifile.seekg(10, ios::beg); ifile.seekg(10, ios::cur); ifile.seekg(0, ios::end); //goto byte number 10 from the beginning //goto byte number 10 from the current position //goto end of the file. ifile.seekg(-10, ios::end) // goto 10 bytes before the end of file. Closing A File A file is closed by disconnecting it with the stream it is associated with. A file can be closed in two ways: If the file has been opened using constructor, it gets closed automatically as soon as the stream objects go out of scope. This calls the destructor, which closes the file. Using close function of the class e.g. infile.close(); (or) outfile.close(); Page 3

4 DATA FILE HANDLING (Previous Year Ques) 1) Observe the program segment given below carefully and fill the blanks marked as Statement 1 and Statement 2 using seekp() and seekg() functions for performing the required task. #include <fstream.h> class Item int Ino;char Item[20]; public: //Function to search and display the content from a particular //record number void Search(int ); //Function to modify the content of a particular record number void Modify(int); ; void Item::Search(int RecNo) fstream File; File.open( STOCK.DAT,ios::binary ios::in); //Statement 1 File.read((char*)this,sizeof(Item)); cout<<ino<< ==> <<Item<<endl; File.close(); void Item::Modify(int RecNo) fstream File; File.open( STOCK.DAT,ios::binary ios::in ios::out); cout>>ino;cin.getline(item,20); //Statement 2 File.write((char*)this,sizeof(Item)); File.close(); File.seekg(RecNo*sizeof(Item)); //Statement 1 File.seekp(RecNo*sizeof(Item)); //Statement ) Observe the program segment given below carefully, and answer the question that follows : class PracFile int Pracno ; char PracName[20] int TimeTaken ; int Marks ; public : void EnterPrac( ); //Function to enter PracFile details void ShowPrac( ); //Function to display PracFile details int RTime( ) //function to return Time Taken return TimeTaken; void Assignmarks(int M) //Function to assign Marks Marks = M ; ; void AllocateMarks( ) fstream File ; File.open ( MARKS.DAT, ios :: binary l ios :: in l ios :: out ) ; PracFile P ; int Record = 0 ; while (File.read ( (char*) &P, sizeof (P) ) ) if (P.RTime( ) > 50) P.Assignmarks(0); else P.Assignmarks(10); //statement1 // statement2 Record++ ; File. close( ) ; If the function AllocateMarks( ) is supposed to Allocate Marks for the records in the file MARKS.DAT based on their value of the member TimeTaken. Write C++ statements for the statement1 and statement2,where statement 1 is required to position the file write pointer to an appropriate place in the file and statement 2 is to perform the write operation with the modified record. File.seekp(File.tellp()-sizeof(P)); //Statement 1 //File.seekp(Record*sizeof(P)); File.write((char*)&P,sizeof(P)); //Statement 2 //File.write((char*)&P,sizeof(PracFile)); ) Observe the program segment given below carefully, and answer the question that follows : class candidate long Cid ; // Candidate s Id char CName[20] ; // Candidate s Name float Marks ; // Candidate s Marks public ; void Enter( ) ; void Display( ) ; void MarksChange( ) ; // to change marks long R_Cid( ) return Cid ; ; void MarksUpdate (long Id) fstream File ; File.open ( CANDIDATE.DAT, ios :: binary ios::in ios :: out) ; Candidate C ; int Record = 0, Found = 0 ; while (!Found &&File.read((char*)&C, sizeof(c))) if (Id = =C.R_Cid( )) cout << Enter new Marks ; C.MarksChange( ) ; //statement 1 //statement 2 Found = 1 ; Record++ ; if (Found = = 1) cout << Record Updated ; File.close( ) ; Page 4

5 Write the Statement to position the File Pointer at the beginning of the Record for which the Candidate s Id matches with the argument passed, and Statement 2 to write the updated Record at that position. File.seekp(File.tellp( )-sizeof(c)); //Statement 1 //File.seekp(Record*sizeof(C)); File.write((char*)&C, sizeof(c)); //Statement ) Find the output of the following C++ code considering that the binary file MEMBER.DAT exists on the hard disk with the records of 44 members. class MEMBER long Mno; char name[20]; public: void In(); void Out(); ; void main() fstream f; f.open( MEMBER.DAT, ios:: binary ios:: in); Member M; f.read((char*)&m, sizeof(m)); f.read((char*)&m, sizeof(m)); f.read((char*)&m, sizeof(m)); int POSITION = f.tellg()/ sizeof(m); cout<< present record: <<POSITION; cout<<endl; f.close(); present record: ) Write a function in C++ to count the number of lines present in a text file STORY.TXT void CountLine() ifstream FIL; FIL.open( STORY.TXT ); int LINES=0; char STR[80]; while (FIL.getline(STR,80)) LINES++; cout<< No. of Lines: <<LINES<<endl; FIL.close(); 6) Write a function in C++ to count the number of uppercase alphabets present in a text file ARTICLE.TXT. void UpperLetters( ) ifstream fin; fin.open("article.txt"); char ch; int ucnt=0; fin.get(ch); if(isupper(ch)) ucnt++; cout<<"\ntotal number of Uppercase alphabets in the file: "<<ucnt; ) Write a function to count the number of words present in a text file named PARA.TXT. Assume that each word is separated by a single blank/space character and no blanks/spaces in the beginning and end of the file. void WordCount( ) ifstream fin; Fin.open("PARA.TXT"); char word[20]; int count=1; cout<< Can t open the file ; fin.getline(word, 20, ); // space bar as delimiter character count++; cout<<"\ntotal number of Words in the file:" <<count; OR void WordCount( ) ifstream fin("para.txt"); char ch; int count=1; cout<< Can t open the file ; fin.get(ch); if(ch= = ) // space character count++; cout<<"total number of Words:"<<count; 8) Write a function in C++ to count the number of lines that starts with a uppercase letter from a text file ESSAY.TXT void CountLine() ifstream fin; fin.open( STORY.TXT ); Page 5

6 int count=0; char str[80]; fin.getline(str,80); if(isupper(str[0])) count++; cout<< No. of Lines that starts with uppercase letter: <<count<<endl; 9) Write a function in C++ to search for a BookNo from a binary file BOOK.DAT, assuming the binary file is containing the objects of the following class. class BOOK int Bno; char Title[20]; public: int RBno()return Bno; void Enter()cin>>Bno;gets(Title); void Display()cout<<Bno<<Title<<endl; ; void BookSearch() BOOK B; int bn, found=0; fstream fin; fin.open( BOOK.DAT,ios::binary); cout<< File can t be opened! ; cout<< Enter Book Num to search ; cin>>bn; while (fin.read((char*)&b, sizeof(b))) if (B.RBno()==bn) found++; B.Display(); if (found==0) cout<< Sorry! Book not found!!! <<endl; 10) Given a binary file TELEPHON.DAT, containing records of the following class Directory : class Directory char Name[20] ; char Address[30] ; char AreaCode[5] ; char phone_no[15] ; public ; void Register( ) ; void Show( ) ; int CheckCode(char AC[ ]) return strcmp(areacode, AC) ; ; 11) Write a function COPYABC( ) in C++, that would copy all those records having AreaCode as 123 from TELEPHON.DAT to TELEBACK.DAT. void COPYABC( ) ifstream fin( TELEPHON.DAT, ios::binary); ofstream fout( TELEBACK.DAT, ios::binary ios::app); Directory D; // or while(!fin.eof( )) fin.read((char*)&d, sizeof(d)); if(d.checkcode( 123 )= = 0) fout.write((char*)&d, sizeof(d)); fin.close( ); fout.close( ); 12) Given a binary file GAME.DAT, containing records of the following structure type struct Game char GameName[20] ; char Participate[10][30] ; ; Write a function in C++ that would read contents from the file GAME.DAT and creates a file named BASKET.DAT copying only those records from GAME.DAT where the game name is Basket Ball. void BPlayers( ) Game G; ifstream fin( GAME.DAT, ios::binary); ofstream fout( BASKET.DAT,ios::app ios::binary); cout<< File can t be opened! ; // or while(!fin.eof( )) fin.read((char*)&g, sizeof(g)); if(strcmpi(g.gamename, Basket Ball )== 0) fout.write((char*) &G, sizeof(g)); fin.close( ); fout.close( ); 13) Given a binary file SPORTS.DAT,containg records of the following structure type: struct Sports char Event[20] ; char Participant[10][30] ; ; Write a function in C++ that would read contents from the file SPORTS.DAT and creates a file named ATHLETIC.DAT copying only those records from SPORTS.DAT where the event name is Athletics. Page 6

7 void AthletesList( ) Sports S; ifstream fin( SPORTS.DAT, ios::binary); ofstream fout( ATHLETIC.DAT,ios::app ios::binary); cout<< File can t be opened! ; // or while(!fin.eof( )) fin.read((char*)&s,sizeof(sports)); if(strcmp(s.event, Athletics )== 0) fout.write((char*)&s,sizeof(s)); fin.close( ); fout.close( ); 14) Following is the structure of each record in a data file named COLONY.DAT struct COLONY char Colony_Code[10] ; char Colony_Name[10] int No_of_People ; ; Write a function in C++ to update the file with a new value of No_of_People. The value of Colony_Code and No_of_People are read during the execution of the program. void Update( ) COLONY C; fstream finout; finout.open( COLONY.DAT,ios::binary ios:: in ios::out); finout.seekg(0); while(finout) finout.read((char *)&C, sizeof(c)); cout<< \nthe Colony Code is: << C.Colony_Code; cout<< \nthe Colony Name is <<C.Colony_Name; cout<< \nenter the Number of People ; cin>>c.no_of_people; finout.seekp(finout.tellg()-sizeof(c)); finout.write((char *)&C,sizeof(C)); Page 7

File.seekp(File.tellp( )-sizeof(c)); //Statement 1 //File.seekp(Record*sizeof(C));

File.seekp(File.tellp( )-sizeof(c)); //Statement 1 //File.seekp(Record*sizeof(C)); DATA FILE HANDLING OUTSIDE DELHI : 2008 4.a)Observe the program segment given below carefully, and answer the question that follows : class candidate { long Cid ; // Candidate s Id char CName[20] ; //

More information

Downloaded from

Downloaded from ASSIGNMENT 1 TOPIC : File Handling TYPE 1 QUESTION : ( Statement write type questions ) Q1. Observe the program segment given below carefully and fill the blanks marked as Statement 1 and Statement 2 using

More information

Computer Science, Class XII, Chapter No.7 (Data File Handling)

Computer Science, Class XII, Chapter No.7 (Data File Handling) Chapter No.7 (Data File Handling) 1. What is a file? How a text file is different from binary file? 2. What is stream? Name and define the streams generally used for file I/O? 3. Which header file is required

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

(1)Given a binary file PHONE.DAT, containing records of the following structure type class Phonlist { char Name[20]; char Address[30]; char

(1)Given a binary file PHONE.DAT, containing records of the following structure type class Phonlist { char Name[20]; char Address[30]; char (1)Given a binary file PHONE.DAT, containing records of the following structure type class Phonlist char Name[20]; char Address[30]; char AreaCode[5]; char PhoneNo[15]; Public: void Register(); void Show();

More information

Data File Handling FILL IN THE BLANKS EACH QUESTION CARRY 1 MARK

Data File Handling FILL IN THE BLANKS EACH QUESTION CARRY 1 MARK Data File Handling FILL IN THE BLANKS EACH QUESTION CARRY 1 MARK 1.. Observe the program segment given below carefully, and answer the question that follows: class Labrecord int Expno; char Expriment[20];

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

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

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

QUESTION BANK SUB: COMPUTER SCIENCE(083)

QUESTION BANK SUB: COMPUTER SCIENCE(083) BHARATIYA VIDYA BHAVAN S V M PUBLIC SCHOOL, VADODARA QUESTION BANK SUB: COMPUTER SCIENCE(083) CHAPTER 5 Data File Handling 1 Mark Questions 1. Observe the program segment carefully and answer the question

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

Q4. Based on File Handling 6-8 Marks File Pointers tellg() tellp() seekp()seekg() 1-2 Marks Binary File 4 Marks Text file 2 Marks

Q4. Based on File Handling 6-8 Marks File Pointers tellg() tellp() seekp()seekg() 1-2 Marks Binary File 4 Marks Text file 2 Marks Q4. Based on File Handling 6-8 Marks File Pointers tellg() tellp() seekp()seekg() 1-2 Marks Binary File 4 Marks Text file 2 Marks 1. (a) Observe the program segment given below carefully and the questions

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

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

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

BRAIN INTERNATIONAL SCHOOL. Computer Science Assignment CLASS XII OCTOBER 2018 Chapter-7. Data File Handling in C++ Text Files

BRAIN INTERNATIONAL SCHOOL. Computer Science Assignment CLASS XII OCTOBER 2018 Chapter-7. Data File Handling in C++ Text Files BRAIN INTERNATIONAL SCHOOL Computer Science Assignment CLASS XII OCTOBER 2018 Chapter-7. Data File Handling in C++ Text Files Question 1 Question 2 Question 3 Question 4 Question 5 Question 6 Write a C++

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

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

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

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-7 Data File Handling

CHAPTER-7 Data File Handling CHAPTER-7 Data File Handling VERY SHORT/SHORT ANSWER QUESTIONS 1. What are input and output streams? What is the significance of fstream.h file? Ans. Input stream: The stream that supplies data to the

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

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

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

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

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

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

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

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

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

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

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

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

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

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

High Order Thinking Skill Questions Subject : Computer Science Class: XII 1 Mark Questions Programming in C++ 1. Observe the program segment carefully and answer the question that follows: int getitem_no(

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

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

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

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

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

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

Fundamental File Processing Operations 2. Fundamental File Processing Operations

Fundamental File Processing Operations 2. Fundamental File Processing Operations 2 Fundamental File Processing Operations Copyright 2004, Binnur Kurt Content Sample programs for file manipulation Physical files and logical files Opening and closing files Reading from files and writing

More information

Model Sample Paper 2015

Model Sample Paper 2015 Time: 3 Hours MM: 70 Model Sample Paper 2015 Class XII Computer Science (083) Instructions: (i) (ii) All questions are compulsory. Programming Language C++. 1. (a) What is the difference between Actual

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

Sample Paper, 2015 Subject: Computer Science Class 12 th

Sample Paper, 2015 Subject: Computer Science Class 12 th Sample Paper, 2015 Subject: Computer Science Class 12 th Time: 3 Hours Max. Marks: 70 Instructions: i) All questions are compulsory and so attempt all. ii) Programming language: C++. iii) Please check

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

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

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

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

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

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

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

C++ How to Program 14.6

C++ How to Program 14.6 C++ How to Program 14.6 14.6 Random-Access Files pg.611-pg.612 -Unlike sequential files, R.A. files are instant-access applications. Any transaction-processing system. Requiring rapid access to specific

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

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

91/1 COMPUTER SCIENCE. Series SHC/1. dksm ua- jksy ua- Code No. Roll No. Candidates must write the Code on the title page of the answer-book.

91/1 COMPUTER SCIENCE. Series SHC/1. dksm ua- jksy ua- Code No. Roll No. Candidates must write the Code on the title page of the answer-book. Series SHC/1 Roll No. jksy ua- Code No. dksm ua- Candidates must write the Code on the title page of the answer-book. Please check that this question paper contains 12 printed pages. Code number given

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

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

SHORT REVIEW OF CS TOPICS RANDOM NUMBERS (2 MARKS) which generates a random number in the range of 0 to n-1. For example;

SHORT REVIEW OF CS TOPICS RANDOM NUMBERS (2 MARKS) which generates a random number in the range of 0 to n-1. For example; SHORT REVIEW OF CS TOPICS RANDOM NUMBERS (2 MARKS) Generating Random Numbers The key function in generating random numbers is; int random (int n); which generates a random number in the range of 0 to n-1.

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

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

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

JB Academy, Faizabad Half Yearly Examination Subject: Computer Science (083) Class XII

JB Academy, Faizabad Half Yearly Examination Subject: Computer Science (083) Class XII JB Academy, Faizabad Half Yearly Examination - 2017-18 Subject: Computer Science (083) Class XII Time: 3 Hours Max. Marks: 70 Instructions: i) All questions are compulsory and so attempt all. ii) Programming

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

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

CLASS XII SECOND TERM EXAMINATION SUBJECT : COMPUTER SCIENCE SET A2 (SOLUTIONS)

CLASS XII SECOND TERM EXAMINATION SUBJECT : COMPUTER SCIENCE SET A2 (SOLUTIONS) CLASS XII SECOND TERM EXAMINATION 2017-2018 SUBJECT : COMPUTER SCIENCE SET A2 (SOLUTIONS) TIME ALLOWED : 3 HRS. MAX. MARKS:70 General Instructions : This paper consists of 6 questions. There are 7 printed

More information

DATA FILE HANDLING. The function should create another file OUT.TXT with the text

DATA FILE HANDLING. The function should create another file OUT.TXT with the text DATA FILE HANDLING (a) What is the purpose of seekp() and seekg( )1) (b) Write a function in C++ to read a text file SPACE.TXT. Using this file create another file OUT.TXT by replacing more than one space

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

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

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

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

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

Applications with Files, Templates

Applications with Files, Templates Files - Introduction A file is collection of data or information that has a name, called the filename. Files are stored in secondary storage devices such as floppy disks and hard disks. The main memories

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

Files Total: // Files Example 1. #include <iostream> #include <fstream>

Files Total: // Files Example 1. #include <iostream> #include <fstream> Files // Files Example 1 datafile.open("datafile01.txt"); 61.7 86.36 78.12 Total: 261.43 // Prime the reading of the file. while(datafile) cout

More information

KENDRIYA VIDYALAYA IIT CAMPUS CHENNAI 36 COMPUTER SCIENCE. Half Yearly

KENDRIYA VIDYALAYA IIT CAMPUS CHENNAI 36 COMPUTER SCIENCE. Half Yearly KENDRIYA VIDYALAYA IIT CAMPUS CHENNAI 6 COMPUTER SCIENCE Half Yearly Time: Hrs M.M:70 1. a. Explain the difference between entry controlled loop and exit controlled loop with the help of an example. b.

More information

Guru Harkrishan Public School, Karol Bagh Pre Mock Class XII Sub: COMPUTER SCIENCE Allowed :3 hrs

Guru Harkrishan Public School, Karol Bagh Pre Mock Class XII Sub: COMPUTER SCIENCE Allowed :3 hrs Guru Harkrishan Public School, Karol Bagh Pre Mock 2014-15 Class XII Sub: COMPUTER SCIENCE Time Allowed :3 hrs M.M. 70 Please check that this question paper contains 9 printed pages. Please check that

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

DELHI PUBLIC SCHOOL BOKARO STEEL CITY

DELHI PUBLIC SCHOOL BOKARO STEEL CITY DELHI PUBLIC SCHOOL BOKARO STEEL CITY ASSIGNMENT FOR THE SESSION 2015-2016 Class: XII Subject : Computer Science Assignment No. 3 Question 1: (a) What is the difference between Call by Value and Call by

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

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

COMPUTER SCIENCE (Theory) Class XII - Code : 083 Blue Print

COMPUTER SCIENCE (Theory) Class XII - Code : 083 Blue Print COMPUTER SCIENCE (Theory) Class XII - Code : 083 Blue Print S.No. UNIT VSA SA I SA II LA TOTAL (1 Mark) (2 Marks) (3 Marks) (4 Marks) 1 Review of C++ covered in Class XI 1 (1) 8 (4) 3 (1) 12 (6) 2 Object

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

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

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

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

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

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

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

CLASS XII SECOND TERM EXAMINATION SUBJECT : COMPUTER SCIENCE SET A1(SOLUTIONS)

CLASS XII SECOND TERM EXAMINATION SUBJECT : COMPUTER SCIENCE SET A1(SOLUTIONS) CLASS XII SECOND TERM EXAMINATION 2017-2018 SUBJECT : COMPUTER SCIENCE SET A1(SOLUTIONS) TIME ALLOWED : 3 HRS. MAX. MARKS:70 General Instructions : This paper consists of questions. There are 8 printed

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

Fundamentals of Programming Session 28

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

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

Sample Paper and Answer Key (New Pattern) 03 Class XII Subject COMPUTER SCIENCE(083) Time allowed: 3 hours Maximum Marks: 70 Q. (a)write the prototype of a function which accepts two integer type arguments

More information

KENDRIYA VIDYALAYA SANGATHAN ERNAKULAM REGION FIRST PRE-BOARD EXAMINATION COMPUTER SCIENCE

KENDRIYA VIDYALAYA SANGATHAN ERNAKULAM REGION FIRST PRE-BOARD EXAMINATION COMPUTER SCIENCE KENDRIYA VIDYALAYA SANGATHAN ERNAKULAM REGION FIRST PRE-BOARD EXAMINATION 2014-15 COMPUTER SCIENCE Time allowed: 3 hours Maximum Marks : 70 Instructions: (i) All questions are compulsory. (ii) Programming

More information

Chapter 12 File Operations. Starting Out with C++, 3 rd Edition

Chapter 12 File Operations. Starting Out with C++, 3 rd Edition Chapter 12 File Operations 1 12.1 What is a File? A file is a collection on information, usually stored on a computer s disk. Information can be saved to files and then later reused. 2 12.2 File Names

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