Streams. Rupesh Nasre.

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

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

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

C++ Input/Output: Streams

Introduction to C++ (Extensions to C)

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

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

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

Software Design & Programming I

Chapter 3 - Notes Input/Output

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

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

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

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

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

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

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

Module C++ I/O System Basics

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

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

Lecture 5 Files and Streams

Streams - Object input and output in C++

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

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

C++ Input/Output Chapter 4 Topics

Introduction. Lecture 5 Files and Streams FILE * FILE *

Chapter 12 - C++ Stream Input/Output

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

Chapter 12. Streams and File I/O

CSE 333 Lecture 9 - intro to C++

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

Setting Justification

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

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

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

Fig: iostream class hierarchy

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

by Pearson Education, Inc. All Rights Reserved. 2

Class 14. Input File Streams. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

CS 103 Unit 14 - Streams

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

This chapter introduces the notion of namespace. We also describe how to manage input and output with C++ commands via the terminal or files.

CS 103 Unit 14 - Streams

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

Chapter 21 - C++ Stream Input/Output

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

Strings and Stream I/O

Arrays. Week 4. Assylbek Jumagaliyev

Definition Matching (10 Points)

Chapter 3: Input/Output

Input/output. Remember std::ostream? std::istream std::ostream. std::ostream cin std::istream. namespace std { class ostream { /*...

Engineering Problem Solving with C++, Etter/Ingber

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

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

10/23/02 21:20:33 IO_Examples

Streams. Ali Malik

Chapter 21 - C++ Stream Input/Output

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

Overview of Lecture. 1 Overloading I/O Operators. 2 Overloading << and >> for Fractions. 3 Formatted vs Unformatted Input

Understanding main() function Input/Output Streams

Objects and streams and files CS427: Elements of Software Engineering

Operator Overloading in C++ Systems Programming

IS 0020 Program Design and Software Tools

Fundamentals of Programming Session 27

C++ Programming Lecture 10 File Processing

Chapte t r r 9

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

CSCI 104 Overview. Mark Redekopp David Kempe

C++ does not, as a part of the language, define how data are sent out and read into the program

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

What we will learn about this week:

COMP322 - Introduction to C++

basic_fstream<chart, traits> / \ basic_ifstream<chart, traits> basic_ofstream<chart, traits>

Introduction to Standard C++ Console I/O. C++ Object Oriented Programming Pei-yih Ting NTOU CS

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

Chapter 14 Sequential Access Files

CS3157: Advanced Programming. Outline

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

CS103L SPRING 2018 UNIT 7: FILE I/O

Programmazione. Prof. Marco Bertini

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

CSE 100: STREAM I/O, BITWISE OPERATIONS, BIT STREAM I/O

String Variables and Output/Input. Adding Strings and Literals to Your Programming Skills and output/input formatting

C++ files and streams. Lec 28-31

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

CS201 Solved MCQs.

3.1. Chapter 3: The cin Object in Program 3-1. Displaying a Prompt 8/23/2014. The cin Object

Chapter 3: Expressions and Interactivity

Input/Output Streams: Customizing

COMP322 - Introduction to C++

C++ basics Getting started with, and Data Types.

G52CPP C++ Programming Lecture 18

Lecture 9. Introduction

Developed By : Ms. K. M. Sanghavi

Sample Code: OUTPUT Daily Highs & Lows

Chapter 3: Expressions and Interactivity. Copyright 2012 Pearson Education, Inc. Thursday, October 9, 14

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

Fundamentals of Programming Session 25

STL. Prof Tejada Week 14

Transcription:

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 << "Enter your name: "; std::cin >> name; std::cout << "Hello " << name << "!\n"; Defaults to console / stdout Uses header <iostream> cin and cout are objects. >> and << are overloaded.

cin By default, reads whitespace-separated formatted tokens. But be careful with char and string. Example stream: 12 17.3 19 int A, B; double X; cin >> A; cin >> X; cin >> B; 12 17.3 19 int A, B; char X; cin >> A; cin >> B; cin >> X; cin >> A; 12 17 '.' 3 int A; char B, C, D; cin >> A; 12 cin >> B; '1' cin >> C; '7' string A, B, C; cin >> A; 12 cin >> B; 17.3 cin >> C; 19

ignore cin ignores the leading whitespace. One can ignore input until a character of relevance is seen. cin.ignore(n, ch); // skip upto N or till ch is seen. Useful for ignoring a line (ch = '\n') cout << "Enter your name: "; cin.ignore(2, 'A'); cin >> name; // ABCD cout << name; // BCD cout << "Enter your name: "; cin.ignore(2, 'Z'); cin >> name; // ABCD cout << name; // CD

cout Uses buffered output \n can be printed with endl. May not print to screen. To force, use cout << flush. endl involves flush. std::string name; std::cout << "Enter your name: "; std::cin >> name; std::cout << "Hello " << name << endl;

File streams ifstream and ofstream What is the output? stream4.cpp ifstream file("stream4.cpp"); string word; while (!file.eof()) { file >> word; cout << word; file.close(); // optional #include<iostream>#include<fstream>usingnamespacestd;intmain() {ifstreamfile("stream4.cpp");stringword;while(!file.eof()) {file>>word;cout<<word;file.close();//optionalreturn0;

ifstream Issue: Last line is read twice. Solution: read precedes eof. stream4.cpp ifstream file("stream4.cpp"); string word; file >> word; while (!file.eof()) { cout << word; file >> word; file.close(); // optional #include<iostream>#include<fstream>usingnamespacestd;intmain() {ifstreamfile("stream4.cpp");stringword;while(!file.eof()) {file>>word;cout<<word;file.close();//optionalreturn0;

cat What is the output? ifstream file("stream4.cpp"); string line; getline(file, line); while (!file.eof()) { cout << line; getline(file, line); file.close(); // optional ifstream file("stream4.cpp"); string word; while (!file.eof()) { file >> word; cout << word; file.close(); // optional

cat What is the output? ifstream file("stream4.cpp"); string word; while (!file.eof()) { file >> word; cout << word; file.close(); // optional ifstream file("stream4.cpp"); string line; getline(file, line); while (!file.eof()) { cout << line << endl; getline(file, line); file.close(); // optional

cp./a.out <inputfile> <outputfile> #include <stdlib.h> int main(int argc, char *argv[]) { ifstream iifile(argv[1]); ofstream oofile(argv[2]); string line; getline(iifile, line); while (!iifile.eof()) { oofile << line << endl; getline(iifile, line);

cp./a.out <inputfile> <outputfile> #include <stdlib.h> int main(int argc, char *argv[]) { if (argc!= 3) { cerr << "Usage: " << argv[0] << " <inputfile> <outputfile>" << endl; exit(1); ifstream iifile(argv[1]); ofstream oofile(argv[2]); string line; getline(iifile, line); while (!iifile.eof()) { oofile << line << endl; getline(iifile, line);

#include <stdlib.h> int main(int argc, char *argv[]) { if (argc!= 3) { cerr << "Usage: " << argv[0] << " <inputfile> <outputfile>" << endl; exit(1); ifstream iifile(argv[1]); if (iifile.fail()) { cerr << "File " << argv[1] << " could not be opened." << endl; exit(2); ofstream oofile(argv[2]); string line; getline(iifile, line); while (!iifile.eof()) { oofile << line << endl; getline(iifile, line);

Formatted I/O data.txt Name: Roll Number: Marks John Augustine: CS12D001: 88 Madhu Mutyam: CS11D111: 89 Rupesh Nasre: CS13B000: 25 ifstream file("data.txt"); string name, roll, marks; getline(file, name, ':'); while (!file.eof()) { getline(file, roll, ':'); getline(file, marks, ':'); Name::: Roll Number::: Marks John Augustine CS12D001::: 88 Madhu Mutyam::: CS11D111 89 Rupesh Nasre::: CS13B000::: 25 cout << name << ":::" << roll << ":::" << marks << endl; getline(file, name, ':');

Formatted I/O data.txt Name: Roll Number: Marks John Augustine: CS12D001: 88 Madhu Mutyam: CS11D111: 89 Rupesh Nasre: CS13B000: 25 Note: getline does not ignore leading whitespace. ifstream file("data.txt"); string name, roll, marks; getline(file, name, ':'); while (!file.eof()) { getline(file, roll, ':'); getline(file, marks); Name::: Roll Number::: Marks John Augustine::: CS12D001::: 88 Madhu Mutyam::: CS11D111::: 89 Rupesh Nasre::: CS13B000::: 25 cout << name << ":::" << roll << ":::" << marks << endl; getline(file, name, ':');

Formatted I/O #include <iomanip> Name Roll Number Marks John Augustine CS12D001 88 Madhu Mutyam CS11D111 89 Rupesh Nasre CS13B000 25 ifstream file("data.txt"); string name, roll, marks; getline(file, name, ':'); while (!file.eof()) { getline(file, roll, ':'); getline(file, marks); cout << setw(20) << name; cout << setw(10) << roll; cout << setw(10) << marks; cout << endl; getline(file, name, ':');

Other functions setprecision: for floating point values. get: to read a single character. peek: to examine a character without removing it from the stream. putback: add to the stream.

Acknowledgment https://courses.cs.vt.edu/cs1044/notes/c04.io.pdf