File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.

Slides:



Advertisements
Similar presentations
CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
Advertisements

© 2000 Scott S Albert Structured Programming 256 Chapter 7 Streams and File I/O.
File I/O in C++. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk);is stored on a secondary storage device.
Programming File I/O. COMP102 Prog. Fundamentals File I/O / Slide 2 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
1 Lecture 31 Handling Selected Topics Again!! File I/O Special Lectures.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
File I/O. COMP104 File I/O / Slide 2 Using Input/Output Files * A computer file n is stored on a secondary storage device (e.g., disk) n is permanent.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Tutorial4us.com. File A file is a collection of related data stored in a particular area on the disk. The data is stored in disk using the concept of.
1 File I/O In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
Programming File I/O. COMP102 Prog. Fundamentals File I/O / Slide 2 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Program Input and the Software Design Process ROBERT REAVES.
Chapter 3 Interactivity and Expressions CSC 125 Introduction to C++ programming.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
File I/O ifstreams and ofstreams Sections 11.1 &
1 CS161 Introduction to Computer Science Topic #13.
FILE HANDLING IN C++.
File handling in C++ BCA Sem III K.I.R.A.S. Using Input/Output Files Files in C++ are interpreted as a sequence of bytes stored on some storage media.
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
File I/O in C++ II. Open() function Open() is a member function in each classes ( fstream, ifstream, ofstream) Void fstream :: open ( const char *filename,
FILE I/O IN C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
File Handling in C++.
Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 4 Working with Data Files.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
24 4/11/98 CSE 143 Stream I/O [Appendix C]. 25 4/11/98 Input/Output Concepts  Concepts should be review!  New syntax, but same fundamental concepts.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Chapter 14: Sequential Access Files
ifstreams and ofstreams
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
CS-103 COMPUTER PROGRAMMING
Tutorial4us.com File Handling in C++ Tutorial4us.com.
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.
Copyright © 2003 Pearson Education, Inc.
File I/O.
File I/O.
Basic File I/O and Stream Objects
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
Text Files All the programs you've seen so far have one thing in common: Any data the program uses or calculates is lost when the program ends. In order.
Tutorial4us.com. File A file is a collection of related data stored in a particular area on the disk. The data is stored in disk using the concept of.
Standard Input/Output Stream
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
File I/O.
File I/O in C++ I.
Programming File I/O.
CHAPTER 4 File Processing.
Reading from and Writing to Files
ifstreams and ofstreams
File I/O in C++ II.
Lecture 9 Files Handling
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

File I/O in C++ I

Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide input data to a program or receive output data from a program or both; should reside in Project directory for easy access; must be opened before it is used.

General File I/O Steps 1.Include the header file fstream in the program. 2.Declare file stream variables. 3.Associate the file stream variables with the input/output sources. 4.Open the file 5.Use the file stream variables with >>, <<, or other input/output functions. 6.Close the files.

 ios is the base class.  istream and ostream inherit from ios  ifstream inherits from istream (and ios)  ofstream inherits from ostream (and ios)  iostream inherits from istream and ostream (& ios)  fstream inherits from ifstream, iostream, and ofstream Classes for Stream I/O in C++

STEP1 : Include the header file  stream - a sequence of characters  interactive (iostream)  cin - input stream associated with keyboard.  cout - output stream associated with display  file (fstream)  ifstream - defines new input stream (normally associated with a file).  ofstream - defines new output stream (normally associated with a file). <> #include

STEP2 : Declare file stream variables. ifstream fIn;//input ofstream fOut; // output fstream both //input & output

STEP3 : Associate the file stream variables with the input/output sources. One way is to do it like this : ifstream fIn(“file1.txt”); // input ofstream fOut(“file2,txt”); // output fstream both (“file3.txt”); //input & output

STEP4 : Open the file Opening a file associates a file stream variable declared in the program with a physical file at the source, such as a disk. In the case of an In the case of an input file: the file must exist before the open statement executes. If the file does not exist, the open statement fails and the input stream enters the fail state In the case of an In the case of an output file:  An output file does not have to exist before it is opened;  if the output file does not exist, the computer prepares an empty file for output.  If the designated output file already exists, by default, the old contents are erased when the file is opened.

Object and Member Functions open() both.open("numbers.txt“) File Name Dir:\\folder\fileName.exten tion Extention (.dat,.out,.txt) File Name Dir:\\folder\fileName.exten tion Extention (.dat,.out,.txt)

Validate the file before trying to access First methodSecond method By checking the stream variable; If ( ! both) { Cout << “Cannot open file”; } By using bool is_open() function. If ( !both.is_open()) { Cout << “File is not open.”; }

File I/O Example: Open output file with validation #include using namespace std; void main() { // declear output file variable ofstream outFile; // open an exist file fout.txt outFile.open(“fout.txt”); // Open validation if(! outFile.is_open() ) { Cout << “Cannot open file.\n ”; } }

STEP5: use the file stream STEP6: close the file #include using namespace std; void main() {/* declear and automaticaly open the file*/ ofstream outFile(“fout.txt"); //behave just like cout, put the word into the file outFile << "Hello World!"; outFile.close(); } File I/O Example: Writing

STEP5: use the file stream STEP6: close the file #include using namespace std; Void main() {// declear output file variable ofstream outFile; // open an exist file fout.txt outFile.open(“fout.txt”); //behave just like cout, put the word into the file outFile << "Hello World!"; outFile.close(); } File I/O Example: Writing

Read char by char #include void main() {//Declare and open a text file ifstream INFile(“data.txt"); char ch; //do until the end of file while( ! INFile.eof() ) { INFile.get(ch); // get one character cout << ch; // display the character } INFile.close(); // close the file } File I/O Example: Reading

Read line by line #include int main() {//Declare and open a text file ifstream INFile(“data.txt"); string line; while(! INFile.eof()) {/fetch line from data.txt and put it in a string getline(INFile, line); cout << line; } INFile.close(); // close the file } File I/O Example: Reading