Computer Programming II Lecture 9. Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading.

Slides:



Advertisements
Similar presentations
Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable.
Advertisements

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.
© 2000 Scott S Albert Structured Programming 256 Chapter 7 Streams and File I/O.
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
CPS120: Introduction to Computer Science Lecture 15 B Data Files.
1 Lecture 31 Handling Selected Topics Again!! File I/O Special Lectures.
An Introduction to Programming with C++ Fifth Edition Chapter 13 Sequential Access Files.
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.
Chapter 15.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
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.
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.
計算機程式語言 Lecture 8-1 國立臺灣大學生物機電系 8 8 I/O File Streams and Data Files.
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.
Darbas su failais Arnas Terekas IT 1gr. Vilniaus universitetas Matematikos ir informatikos fakultetas.
Chapter 8 Data File Basics.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
File I/O ifstreams and ofstreams Sections 11.1 &
C++ Introduction : C++ compilation. Visual Studio 2008 : Creating Command-Line Program.
Chapter 9 I/O Streams and Data Files
1 CS161 Introduction to Computer Science Topic #13.
FILE HANDLING IN C++.
Topics 1.File Basics 2.Output Formatting 3.Passing File Stream Objects to Functions 4.More Detailed Error Testing 5.Member Functions for Reading and 6.Writing.
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.
I/O in C++ October 7, Junaed Sattar. Stream I/O a stream is a flow of bytes/characters/ints or any type of data input streams: to the program output.
C++ Introduction : C++ compilation Standard Input/Output: cin, cout Functions Array, Pointer class File Input/Output.
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.
Loops and Files. 5.1 The Increment and Decrement Operators.
1 COMS 261 Computer Science I Title: Functions Date: October 24, 2005 Lecture Number: 22.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Program Input and the Software Design Process ROBERT REAVES.
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?
Lecture 14 Arguments, Classes and Files. Arguments.
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.
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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
File I/O. Files allow permanent storage of programs and data. ifstream and ofstream objects –For file I/O the inclusion of is required. –Objects for file.
Lecture 2 Fundamental File: Processing Operations
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
CS212: Object Oriented Analysis and Design
CS212: Object Oriented Analysis and Design
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.
Introduction to Programming
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.
Basic Input and Output Operations
Copyright © 2003 Pearson Education, Inc.
FILE HANDLING IN C++.
File I/O.
Basic File I/O and Stream Objects
files Dr. Bhargavi Goswami Department of Computer Science
Today’s Lecture I/O Streams Tools for File I/O
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 in C++ I.
CPS120: Introduction to Computer Science
CS149D Elements of Computer Science
CHAPTER 4 File Processing.
File Streams 12/09/13.
ifstreams and ofstreams
Lecture 9 Files Handling
File I/O in C++ I.
Presentation transcript:

Computer Programming II Lecture 9

Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading from standard input and writing to standard output respectively. - In this lecture we will know how to read and write from a file.

Files Processing - A file is a collection of information, usually stored on a computer’s disk. Information can be saved to files and later retrieved. - The two basic types of files are : text files and binary files.

Files Processing Types of File Access: - Sequential access: With this type of file access one must read the data in order, much like with a tape, whether the data is really stored on tape or not. - Random access (or direct access): This type of file access lets you jump to any location in the file, then to any other, etc., all in a reasonable amount of time.

Files Processing C++ File I/O Classes : There are 3 File I/O classes in C++ which are used for File Input(Read) / Output (Write) operations : ifstream : It is used to read information from file. ofstream : It is used to create files and to write information to file. fstream : It has the capabilities of both ofstream and ifstream which means it can create files, write information to file, and read information from file. *To perform file processing in C++, header files and must be included in your C++ source file.

Files Processing Opening a file: A file must be opened before you can read from it or write to it. Either the ofstream or fstream object may be used to open a file for writing and ifstream object is used to open a file for reading purpose only.

Files Processing - In order to open a file with a stream object we use its member function open(): object_name.open (filename, mode); - Where filename is representing the name of the file to be opened, and mode is an optional parameter with a combination of the following flags:

Files Processing

Writing a file : While doing C++ programming, you write information to a file from your program using the stream insertion operator (<<) just as you use that operator to output information to the screen. The only difference is that you use an ofstream or fstream object instead of the cout object.

Files Processing Writing to a text file using fstream class: Writing to a text file can be achieved with the stream operators. This also follows the same order of operations, though with a slight difference. 1. open a file – in write mode. 2. Write to a file. 3. close the file. - This sample code snippet explains how to use the C++ file i/o stream operators to write data from a file. In all cases the header file fstream.h must be included.

Files Processing

Reading from a file: You read information from a file into your program using the stream extraction operator (>>) just as you use that operator to input information from the keyboard. The only difference is that you use an ifstream or fstream object instead of the cin object.

Files Processing Reading a text file using (fstream) class: There are several ways of reading the text from a file. But all of them have a common approach as follows: Open the file Read the data Close the file Look at the following sample code to see the difference.

Files Processing

Closing a file: When we are finished with our input and output operations on a file we shall close it so that its resources become available again. In order to do that we have to call the stream's member function close(). This member function takes no parameters, and what it does is to flush the associated buffers and close the file: Object_name.close( );

Files Processing - Write a C++ program that used to: 1- Allow user to enter his\her name. 2- Create a text file with this name (myfile) on the disk drive (D) and then save the user’s name inside it.

Files Processing

- Write a C++ program that used to create a text file with this name (student) on the disk drive (D) and then save the data for 3 students inside it. Note : - The student data are : student number and student name. - These data should inserted by user.

Files Processing

- Write a C++ program that used to allow you to enter the student data and then it will add it to the previous text file which named as (student) on the disk drive (D). Note : - The student data are : student number and student name.

Files Processing