Jan 27, C++ STL Streams. Daniel Maleike

Similar documents
Streams - Object input and output in C++

Streams. Ali Malik

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

Chapter 12 - C++ Stream Input/Output

Standard I/O in C and C++

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

PIC10B/1 Winter 2014 Exam I Study Guide

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

Java IO and C++ 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.

Introduction to C++ (Extensions to C)

Documentation. Programming / Documentation Slide 42

Lecture 5 Files and Streams

G52CPP C++ Programming Lecture 18

Informatik I (D-ITET) Übungsstunde 11, Hossein Shafagh

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

Introduction. Lecture 5 Files and Streams FILE * FILE *

G52CPP C++ Programming Lecture 17

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

struct Buffer { Buffer(int s) { buf = new char[s]; } ~Buffer() { delete [] buf; } char *buf; };

CS201 Solved MCQs.

Crash Course in C++ R F L Evans. www-users.york.ac.uk/~rfle500/

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Objects and streams and files CS427: Elements of Software Engineering

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.

Fundamentals of Programming Session 27

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Module 11 The C++ I/O System

Due Date: See Blackboard

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

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

Understanding main() function Input/Output Streams

Time(int h, int m, int s) { hrs = h; mins = m; secs = s; } void set(int h, int m, int s) { hrs = h; mins = m; secs = s; }

Note 11/13/2014. They are like those i s, j s, and temp s that appear and disappear when the function starts and finishes...

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

IS 0020 Program Design and Software Tools

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

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

I/O Streams. Stream Input. I/O Stream Manipulators. State-of-Stream Member Functions. I/O Stream Format Flag Member Functions

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 2. Yusuf Pisan

Intermediate Programming Methodologies in C++ CIS22B. Joe Bentley

CS11 Advanced C++ Lecture 2 Fall

PHY4321 Summary Notes

PIC10B/1 Winter 2014 Final Exam Study Guide

Chapter 8 File Processing

Operator Overloading in C++ Systems Programming

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

C C C C++ 2 ( ) C C++ 4 C C

Strings and Stream I/O

Programmazione. Prof. Marco Bertini

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

Lecture 9. Introduction

Arizona s First University. ECE 373. Operation Overloading. The Revolution Operation Will Be Televised (and, these slides will be online later today)

CSI33 Data Structures

Set Implementation Version 1

UNIT V FILE HANDLING

Fundamentals of Programming Session 28

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

CS 215 Fundamentals of Programming II Spring 2011 Project 2

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

Looping and Counting. Lecture 3 Hartmut Kaiser hkaiser/fall_2012/csc1254.html

Shahram Rahatlou. Static Data Members Enumeration std::pair, std::vector, std::map. Computing Methods in Physics

Looping and Counting. Lecture 3. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

Chapter 15 - C++ As A "Better C"

Interview Questions of C++

CMSC 202 Midterm Exam 1 Fall 2015

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

10/23/02 21:20:33 IO_Examples

CSE 100: C++ IO CONTD., AVL TREES

Overloading Operators in C++

CSI33 Data Structures

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Inheritance and Polymorphism

Functions and Recursion

Fundamentals of Programming Session 25

Operator Overloading

Why Is Repetition Needed?

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

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

Fundamental of Programming (C)

CS3157: Advanced Programming. Outline

Advanced I/O Concepts

Topics. bool and string types input/output library functions comments memory allocation templates classes

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Chapter 18 - C++ Operator Overloading

CS 247: Software Engineering Principles. ADT Design

Laboratory 7. Programming Workshop 2 (CSCI 1061U) Faisal Qureshi.

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

CSC 330 Object Oriented Programming. Operator Overloading Friend Functions & Forms

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Bruce Merry. IOI Training Dec 2013

Fig: iostream class hierarchy

CSE 303: Concepts and Tools for Software Development

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

Standard Library. Lecture 27. Containers. STL Containers. Standard Library

I/O streams

W3101: Programming Languages C++ Ramana Isukapalli

Chapter 2 - Control Structures

Transcription:

C++ STL Streams

Why Stream-IO? The STL way for I/O Input, output, formatting, file access More type-safe than printf(), scanf() Extensible with user defined types (classes) Inheritable, i.e. custom I/O targets, e.g. logging interface void PrintPatient(std::ostream& s, const Patient& p) { s << Patient << p.patientid << named ' << p.patientname << ' << std::endl; s << Total body fat: << std::setprecision(1) << p.patientpercentagefat << sdd::endl; } Page 2

Overview istream ostream namespace std { ostream cout; // connected to standard output iostream ostream cerr; // connected to error output istream cin; } // connected to standard input fstream stringstream Output to any instance of std::ostream Input from any instance of std::istream Identical code for files, strings, console,... Page 3

Reading from file #include <fstream> std::ifstream numberfile( c:/my/files/numb.ers ); // or numberfile.open(..) if (numberfile.fail()) return; std::vector<int> numbers; int i; while ( numberfile >> i ) // fails on EOF or INVALID integer (type-safety) { std::cout << Read << i << from file << std::endl; numbers.push_back(i); } numberfile.close(); Page 4

Stream status if ( stream ) if ( stream.fail() ) previous operation was successful, same as!stream.fail() previous operation failed if ( stream.eof() ) reading past end of stream attempted if ( stream.bad() ) stream state undefined; cannot be used anymore! if ( stream.good() ) none of bad/eof/fail Page 5

Formatting for input and output Formatting is influenced by a number of flags Two versions for setting flags stream.setf( std::ios_base::boolalpha ); // write bool values as true/false stream << std::ios_base::boolalpha << true; boolalpha, noboolalpha // 0/1 or false/true showbase, noshowbase // 0xA3 or A3 showpoint, noshowpoint // 1 or 1. showpos // 1 or +1 uppercase // 1e+3 or 1E+3 setprecision(int n) setbase(int n)... width(int n), fill(char c), left, right, internal Page 6

String streams Can be used to format numbers into strings #include <sstream> std::stringstream stream; str << Formatted number: << std::fixed << std::setprecision(2) << 5.1233; std::string s( stream.str() ); // s == 5.12 Page 7

Locales nice formatting and potential source of errors A locale collects the language / culture specific representations of numbers, number punctuation (decimal point, thousands separator) time, date, weekdays, currency symbols and formatting character collation (order of letters for comparisons: ä > å? ) Examples for locale names: C, POSIX de_de, de_de.utf8, de_de@euro, lang_german_germany #include <locale> std::locale currentlocale( std::cout.getloc() ); // current output locale (e.g. de_de ) std::cout << 5.43 << std::endl; // 5,43 std::locale clocale( C ); std::cout.imbue( clocale ); std::cout << 5.43 << std::endl; // 5.43 Page 8

Custom data types output #include <iostream> class Fred { public: friend std::ostream& operator<< (std::ostream& o, const Fred& fred); }; std::ostream& operator<< (std::ostream& o, const Fred& fred) { return o << One Fred at << (void*) this; } int main() { Fred f; std::cout << "My Fred object: " << f << "\n"; } Page 9

Custom data types input #include <iostream> class Fred { public: friend std::istream& operator>> (std::istream& o, Fred& fred); private: int filling; }; std::istream& operator>> (std::istream& i, Fred& fred) { return i >> fred.filling; } Page 10

Outlook Poco library uses streaming for encryption/decryption (CryptoIOS) compression (ZipIOS) socket communication (SocketIOS)... (similar streaming interfaces in Qt) Page 11

Useful resources Tutorial http://www.informit.com/articles/article.aspx?p=170770 STL references http://www.cplusplus.com/reference/iostream/ http://www.cppreference.com/wiki/ <iostream>-faq http://www.parashift.com/c++-faq-lite/input-output.html List of POSIX locales `locale -a` in any terminal MSDN about Windows locales http://msdn.microsoft.com/en-us/library/hzz3tw78.aspx Page 12