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

Similar documents
C++ Input/Output: Streams

Chapter 3 - Notes Input/Output

BITG 1113: Files and Stream LECTURE 10

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

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

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

Input and Output File (Files and Stream )

What we will learn about this week:

C++ Programming Lecture 10 File Processing

Objects and streams and files CS427: Elements of Software Engineering

Lecture 3 The character, string data Types Files

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

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

Formatting outputs String data type Interactive inputs File manipulators. Access to a library that defines 3. instead, a library provides input

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

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

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

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

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

Streams. Rupesh Nasre.

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

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

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

Week 5: Files and Streams

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

PROGRAMMING EXAMPLE: Checking Account Balance

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

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

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

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

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

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

Chapter 3: Input/Output

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

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

Chapter 5: Loops and Files

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

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

C++ Input/Output Chapter 4 Topics

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

Chapte t r r 9

Simple File I/O.

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

Chapter 14 Sequential Access Files

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

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

Software Design & Programming I

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

Input/Output Streams: Customizing

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

COMP322 - Introduction to C++

C++ Quick Guide. Advertisements

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

Understanding main() function Input/Output Streams

COMP322 - Introduction to C++

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

Streams and Basic File I/O Tools for Stream I/O Character I/O Inheritance

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

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

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

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

Setting Justification

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

File Input / Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #9

Chapter 12. Streams and File I/O

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

for (int i = 1; i <= 3; i++) { do { cout << "Enter a positive integer: "; cin >> n;

I/O streams

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Engineering Problem Solving with C++, Etter/Ingber

Chapter 11 Customizing I/O

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Definition Matching (10 Points)

Arrays. Week 4. Assylbek Jumagaliyev

The following is a typical execution run of this program:

Tutorial letter 202/2/2018

Introduction to C++ (Extensions to C)

Basics of C++ C++ Overview Elements I / O Assignment and Comments Formatted Output. Dr.Hamed Bdour

Developed By : Ms. K. M. Sanghavi

IS 0020 Program Design and Software Tools

Lecture 9. Introduction

System Design and Programming II

More File Operations. Lecture 17 COP 3014 Spring april 18, 2018

Expressions, Input, Output and Data Type Conversions

Object Oriented Programming In C++

LAB: STRUCTURES, ARRAYS,

Scientific Computing

ENGI 1020 Introduction to Computer Programming R E Z A S H A H I D I J U L Y 2 6,

CSCE 206: Structured Programming in C++

Chapter 11 Customizing I/O

File I/O CS 16: Solving Problems with Computers I Lecture #9

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

Chapter 3. Numeric Types, Expressions, and Output

A SHORT COURSE ON C++

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

C++ STREAMS; INHERITANCE AS

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

CS 103 Unit 14 - Streams

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Getting started with C++ (Part 2)

Transcription:

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 output.cpp C++ source file.doc Microsoft word document 2 I/O Streams A stream in C++ is a conduit (pipe) through which data passes Input passes through the istream object and output passes through the ostream object. Input and output are managed by the istream object cin and the ostream object cout The istream class defines the use of the extraction operator >> (used with cin) 3 Stream Extraction and Insertion #include <iostream> Input (stream extraction): cin >> // takes data from stream and sticks in variable Output (stream insertion): cout << // takes data from variable and puts into stream Input (Extraction) skips whitespace! \n, \t,, \r, \v 4 1

Input and Output File Streams Input from a file is managed by an ifstream object in the same way that the input from the keyboard is managed by the istream object cin Similarly, output to a file is managed by an ofstream object in the same way that the output to the monitor is managed by the ostream object cout The difference is that ifstream and ofstream objects have to be declared explicitly and initialized with the external name of the file which they manage #include the <fstream.h> header file Files: Let s Use Them #include <fstream> Declare a file stream variable ifstream infile; //input file stream ofstream outfile; //output file stream Open the files infile.open( MyInput.dat ); outfile.open( MyOutput.dat ); 5 6 Output File Stream Example #include<fstream> #include<iostream> void main() Create a new file stream newfile ofstream newfile; Connect newfile to the file on disk newfile.open("students.txt"); newfile<< this is written to the file ; cout<< this is written to the monitor ; 7 Write a database file, Lab 3 prob 1) char done = 'y'; int acntnum; //an account number float balance; // an account balance ofstream outfile; // a file variable. a) Setting up Output File outfile.open ("accnts.dat"); // prepare database file for writing 8 2

Lab 3 prob 1) continued // Now input values from keyboard and write to the disk. while (done!= 'n') b) Using output file cout << "enter an accnt number and balance: "; cin >> acntnum >> balance; Lab 3 Prob 2) Now Read database file Same file we wrote to float sum = 0.0; ifstream infile; // a file var. for the input infile.open ("accnts.dat"); //prepare file for reading outfile << acntnum << " " << balance << endl; cout << "another account? enter `y' or `n':"; cin >> done; 9 for (n=0; n<3; n++) // sum 3 accounts... infile >> acntnum >> balance; sum += balance; cout << "The total of all accounts is: " << sum ; 10 Always Close your Files Don t forget to close the files when you are done with them infile.close( ); outfile.close( ); Handles any final writing and lets file be reopened later in program. Put this after all reads/writes are finished (usually right before program or function terminates) 11 Alternative Opening Syntax Instead of Declaring then opening: ifstream infile; infile.open( accts.dat ); You can Declare and Open in one statement ifstream infile( accts.dat ); ofstream outfile( accts.dat ); 12 3

Other Capabilities of File Streams A File stream object maintains a true/false value that can be tested to check an operation ifstream infile; infile.open( accts.dat ); If open was unsuccessful if (!infile) cout<< file not found ; exit(1); This message is displayed Loop until end-of-file Process indefinite list in a file: infile>>acntnum>>balance; while(infile) Check stream status after each read // process last data // get next data infile>>acntnum>>balance; And program quits 13 14 A more compact version Process indefinite list in a file: while(infile>>acntnum>>balance) // process data Read data and Check status after each read 15 Tools for Stream Formatting Running Lab 3 Prob 1, if we enter 222 444.777777777 33 12000000.22 We get an accnts.dat file that looks like this: 222 444.778 33 1.2e+07 16 4

Formatting Output to Files Remember to #include <iomanip> Use setprecision(2) -- to force 2 digits of precision for all float data Use fixed to force fixed point formatting (no e-notation allowed) for all following data Use left (-- or right) to justify (line up) on left or right side of column strings look better with left, numbers with right Use setw(10) to output data right justified in a field of 10 spaces Only applies to next data 17 #include <iomanip> Lab 3 Prob1) With Formatting while (done!= 'n') cout << "enter an account number and balance: "; cin >> acntnum >> balance; outfile<<setprecision(2)<<fixed<< setw(10) << acntnum <<setw(15) << balance << endl; cout << "another account? enter `y' or `n':"; cin >> done; 18 The Result! NOW running Listing 8.1 MODIFIED, if we enter 222 444.777777777 33 12000000.22 We get an accnts.dat file that looks like this: 222 444.78 33 12000000.00 19 Creating Space in Output The setw function specifies the number of spaces for the next item Applies only to the next item of output Example: To print the digit 7 in four spaces use outfile<<setw(4)<< 7 << endl; Three of the spaces will be blank 7 (ios::left) Slide 20 5