CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.

Slides:



Advertisements
Similar presentations
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
Advertisements

CMSC 2021 C++ I/O and Other Topics. CMSC 2022 Using C++ Stream I/O Default input stream is called cin Default output stream is called cout Use the extraction.
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
File streams Chapter , ,
CS-212 C++ I/O Dick Steflik. C++ I/O Modeled after UNIX’s concept of a “stream” –conceptionally a stream is a continuous flow of characters/bytes from.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
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.
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.
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
CSCE 121: Introduction to Program Design and Concepts J. Michael Moore Fall 2014 Set 11: More Input and Output 1 Based on slides created by Bjarne.
C++ Programming Certificate University of Washington Cliff Green
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.
ITEC 320 C++ Examples.
C++ Streams Lecture-2.
File I/O ifstreams and ofstreams Sections 11.1 &
Operator Overloading Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
1 Lecture 19 Structs HW 5 has been posted. 2 C++ structs l Syntax:Example: l Think of a struct as a way to combine heterogeneous data values together.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
CSE 232: C++ debugging in Visual Studio and emacs C++ Debugging (in Visual Studio and emacs) We’ve looked at programs from a text-based mode –Shell commands.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
C++ Streams Lecture-2. C++ Streams Stream  A transfer of information in the form of a sequence of bytes I/O Operations:  Input stream: A stream that.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
C++ Streams © Bruce M. Reynolds & Cliff Green, C++ Programming Certificate University of Washington Cliff Green.
Chapter 9 Streams: Input stream: source of characters. Output stream: destination for characters. Up to now the input stream has defaulted to the keyboard.
Lecture Contents I/O Streams. –Input/output streams. –Unformatted vs formatted streams. –Stream manipulators. –Stream error state. –Stream tying. –Examples.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1436 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
Why Use Namespaces? Classes encapsulate behavior (methods) and state (member data) behind an interface Structs are similar, but with state accessible Classes.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
C++ REVIEW INPUT/OUTPUT (I/O). BRIEF NOTE “CLASSES” AND “STRUCTS” AND “TYPES” They are ADTs… They define data and operations on that data The ADTs in.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
CSE 332: C++ data types, input, and output Built-In (a.k.a. Native) Types in C++ int, long, short, char (signed, integer division) –unsigned versions too.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
GE 211 Programming in C ++ Dr. Ahmed Telba Room :Ac -134
Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams  IOStreams are part of the Standard C++ library.
CSCI 333 Data Structures I/O with C++ 20 October, 2003.
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.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ (Extensions to C)
Chapter 1.2 Introduction to C++ Programming
Introduction to Programming
Friday, January 19, 2018 Announcements… For Today… For Next Time…
Standard Input/Output Streams
Standard Input/Output Streams
C++ Functions, Classes, and Templates
File I/O Streams, files, strings 1 1.
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Programming with Data Files
Why Use Namespaces? Classes encapsulate behavior (methods) and state (member data) behind an interface Structs are similar, but with state accessible Classes.
Standard Input/Output Stream
Chapter 3 Input output.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
File I/O.
Dale/Weems/Headington Program Input and the Software Design Process
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
Chapter 1 c++ structure C++ Input / Output
Input / output (i/o) Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Presentation transcript:

CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args –Using cout to print data out to the terminal –Using cin to obtain data from the user at run-time –Using an ifstream to read data in from a file –Using an ofstream to write data out to a file How to move data between strings, basic types –Using an istringstream to extract int values –Using an ostringstream to assemble a string This module will expand on those ideas –Towards understanding better how steams work –Towards using them even more effectively

CSE 332: C++ IO Example: Moving Data Into and Out of a Struct // struct, operator declarations (in point2d.h) struct Point2D { Point2D (int x, int y); bool operator< (const Point2D &) const; int x_; int y_; }; // extraction operator(also in point2d.h) istream & operator>> (istream &, Point2D &); // insertion operator(also in point2d.h) ostream & operator<< (ostream &, const Point2D &);

CSE 332: C++ IO Defining Stream Operators for a Struct // operator definitions (in point2d.cpp) // extraction operator(also in point2d.h) istream & operator>> (istream &i, Point2D &p) { i >> p.x_ >> p.y_; // extract both variables return i; } // insertion operator(also in point2d.h) ostream & operator<< (ostream &o, const Point2D &p) { o << p.x_ << " " << p.y_; // space delimited return o; }

CSE 332: C++ IO Now Can Use the Struct with Various Streams // uses standard io streams #include using namespace std; #include "point2d.h" int main(int, char*[]){ while (true) { Point2D p(0, 0); cout << "Please enter 2 coordinates (or hit Ctrl-C to quit): "; cin >> p; cout << endl << "You entered " << p << endl; } return 0; } // uses string streams #include using namespace std; #include "point2d.h" int main(int, char*[]){ Point2D p(0, 0); string s ("64 81"); istringstream i(s); i >> p; ostringstream o; o << p; cout << "The string is " << o.str() << endl; return 0; }

CSE 332: C++ IO Flushing Streams (and Stream Manipulators) An output stream may hold onto data for a while, internally –E.g., writing chunks of text rather than a character at a time is efficient –When it writes data out (e.g., to a file, the terminal window, etc.) is entirely up to the stream, unless you tell it to flush out its buffers If a program crashes, any un-flushed stream data is lost –Terminal output & files are as of last flush, not as of where it crashed –So, flushing streams reasonably often is an excellent debugging trick Can tie an input stream directly to an output stream –Output stream is then flushed by call to input stream extraction operator –E.g., my_istream.tie(&my_ostream); –cout is already tied to cin (useful for prompting the user, getting input) Also can flush streams directly using stream manipulators –E.g., cout << flush; or cout << endl; or cout << unitbuf; Other stream manipulators are useful for formatting streams –Field layout: setwidth, setprecision, etc. –Display notation: oct, hex, dec, boolalpha, noboolalpha, scientific, etc.

CSE 332: C++ IO A Few More Useful Details Cannot copy or assign stream objects –Copy construction or assignment syntax using them results in a compile-time error Extraction operator consumes data from input stream –“Destructive read” that reads a different element each time –Use a variable if you want to read same value repeatedly Need to test streams’ condition states –E.g., calling the is_open method on a file stream –E.g., use the stream object in a while or if test –Insertion and extraction operators return a reference to a stream object, so can test them too File stream destructor calls close automatically