C++ Streams © Bruce M. Reynolds & Cliff Green, 2002 1 C++ Programming Certificate University of Washington Cliff Green.

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

Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
IOStreams CNS 3370 Copyright 2003, Fresh Sources, Inc.
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.
File streams Chapter , ,
File I/O in C++. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk);is stored on a secondary storage device.
 2003 Prentice Hall, Inc. All rights reserved. 1 Recursion Recursive functions –Functions that call themselves –Can only solve a base case If not base.
The Standard String Class Is actually a template: –typedef basic_string string This means you can have strings of things other than chars.
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.
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.
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
Chapter 11 Customizing I/O Bjarne Stroustrup
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.
File I/O ifstreams and ofstreams Sections 11.1 &
C to C++ © Bruce M. Reynolds & Cliff Green, C++ Programming Certificate University of Washington Cliff Green.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
Templates © Bruce M. Reynolds & Cliff Green 1 // min function template #include using std::cout; using std::endl; // there’s already one of these in the.
Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel Week 12 Labs 4 and 5 are back File IO Looking ahead to the final.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
The C++ Programming Language Streams. Contents u Output Stream u Input Stream u Formatting u Manipulators u Files & Streams.
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
C++ Programming: chapter 6 – iostream 2014, Spring Pusan National University Ki-Joune Li 1.
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.
Student Book Input / Output in C++
1 I/O  C++ has no built-in support for input/output input/output is a library (iostream)  C++ program views input and output as a stream of bytes  Input:
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with output formatting.
CS  Inserters and Extractors  Stream State  Files Streams  String Streams  Formatting  Manipulators  Internationalization.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
Operator Overloading Chapter Objectives You will be able to Add overloaded operators, such as +,-, *, and / to your classes. Understand and use.
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
C++ Programming: chapter 6 – iostream 2015, Spring Pusan National University Ki-Joune Li 1.
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.
Lecture 14 Arguments, Classes and Files. Arguments.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
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.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
CS116 SENEM KUMOVA METİN. Outline What is C++ ? Some features of C++ Input and Output Operations Manipulators C++ header files Namespaces and scope resolution.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
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.
 Data Streams  Numeric Output  Numeric Input  Multiple Numeric Output  Multiple Numeric Input  Character Output  Character Input  String Output.
Functions Input and output Lecture 2. Constants #define – is a preprocessor directive Most common use.
2008YeungNam Univ. SE Lab. 1 C++ views each file as a sequence of bytes terminated by EOF-marker. Header files Files are opened by creating objects of.
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ (Extensions to C)
Chapter 1.2 Introduction to C++ Programming
Introduction to Programming
Chapter 21 - C++ Stream Input/Output Stream Manipulators
Output Stream Formatting
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
Today’s Lecture I/O Streams Tools for File I/O
Chapter 13: Advanced File and I/O Operations
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
Operator Overloading.
CS150 Introduction to Computer Science 1
File I/O.
C++ Programming: chapter 6 – iostream
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
File I/O in C++ II.
Presentation transcript:

C++ Streams © Bruce M. Reynolds & Cliff Green, C++ Programming Certificate University of Washington Cliff Green

C++ Streams © Bruce M. Reynolds & Cliff Green, // stream output, Quincy Ex #include using std::cout; int main() { int amt (3); char ch ('A‘); double val (1.23); // --- display the initialized variables cout << amt << ' ' << ch << ' ' << val; return 0; }

C++ Streams © Bruce M. Reynolds & Cliff Green, // formatted stream output, Quincy Ex #include using std::cout; using std::dec; using std::oct; using std::hex; int main() { int amount = 123; cout << dec << amount << ' ' << oct << amount << ' ' << hex << amount; return 0; }

C++ Streams © Bruce M. Reynolds & Cliff Green, // reading input, Quincy Ex #include using std::cout; using std::cin; int main() { short int amount; cout << "Enter an amount..."; cin >> amount; cout << "The amount you entered was " << amount; return 0; }

C++ Streams © Bruce M. Reynolds & Cliff Green, // reading a string, Quincy Ex #include using std::cout; using std::cin; using std::string; int main() { cout << "Enter a name..."; char name[20]; cin >> name; cout << "The name you entered was " << name << ‘\n’; cout << “Enter a city: “; string city; cin >> city; cout << “The city you entered was “ << city; return 0; }

C++ Streams © Bruce M. Reynolds & Cliff Green, // setw manipulator, Quincy Ex #include using std::cout; using std::endl; using std::setw; int main() { static double values[] = { 1.23, 35.36, 653.7, }; static char const* names[] = {"Zoot", "Jimmy", "Al", "Stan"}; for (int i = 0; i < 4; ++i) cout << setw(6) << names[i] << setw(10) << values[i] << endl; return 0; }

C++ Streams © Bruce M. Reynolds & Cliff Green, // fill and width member functions, Quincy Ex #include using std::cout; using std::endl; int main() { static double values[] = { 1.23, 35.36, 653.7, }; for (int i = 0; i < 4; ++i) { cout.width(10); cout.fill('*'); cout << values[i] << endl; } return 0; }

C++ Streams © Bruce M. Reynolds & Cliff Green, // the setiosflags manipulator, Quincy Ex #include using namespace std; int main() { static double values[] = { 1.23, 35.36, 653.7, }; static char const* names[] = {"Zoot", "Jimmy", "Al", "Stan"}; for (int i = 0; i < 4; ++i) { cout << setiosflags(ios::left) << setw(6) << names[i] << resetiosflags(ios::left) << setiosflags(ios::right) << setw(10) << values[i] << endl; } return 0; }

C++ Streams © Bruce M. Reynolds & Cliff Green, // istream get member function, Quincy Ex #include using namespace std; int main() { char line[25], ch = 0, *cp; cout '; cp = line; while (ch != 'x') { cin >> ch; *cp++ = ch; } *cp = '\0'; cout << ' ' << line; cout '; cp = line; ch = 0; while (ch != 'x') { cin.get(ch); *cp++ = ch; } *cp = '\0'; cout << ' ' << line; return 0; }

C++ Streams © Bruce M. Reynolds & Cliff Green, // istream getline member function, Quincy Ex #include using std::cout; using std::cin; using std::endl; int main() { char line[25]; cout << " Type a line terminated by 'q'" '; cin.getline(line, 25, 'q'); cout << ' ' << line; return 0; }

C++ Streams © Bruce M. Reynolds & Cliff Green, // istream read member function, Quincy Ex #include using std::cout; using std::cin; int main() { char msg[23]; cin.read(msg, sizeof msg); cout << msg; return 0; }

C++ Streams © Bruce M. Reynolds & Cliff Green, // file appending stream output, Quincy Ex #include using std::ofstream; int main() { ofstream tfile("test.dat", ios::app); tfile << ", and these are more"; return 0; }

C++ Streams © Bruce M. Reynolds & Cliff Green, // reading a writing a stream file, Quincy Ex #include using namespace std; int main() { char const* fname = "test.dat"; // --- read the file into an array ifstream tfile(fname, ios::in | ios::out | ios::binary); ostream ofile(tfile.rdbuf()); char tdata[100]; int i = 0; while (!tfile.eof() && i < sizeof tdata) { tfile.get(tdata[i++]); } ofile.seekp(0, ios::end); ofile << "\r\n"; for (int j = 0; i && j < i-1; ++j) { ofile.put(static_cast (toupper(tdata[j]))); } return 0; }

C++ Streams © Bruce M. Reynolds & Cliff Green, // extracting from istringstream, Quincy Ex #include using namespace std; int main() { string instr; cout << "Enter an integer, a float, and a string: " << flush; getline(cin, instr); istringstream istr(instr); int n; float f; string s; istr >> n >> f >> s; cout << "Extracted from istringstream: " << n << ' ' << f << ' ' << s << endl; return 0; }

C++ Streams © Bruce M. Reynolds & Cliff Green, // the ostringstream class, Quincy Ex #include “Date.h” #include using std::ostringstream; using std::cout; using std::endl; int main() { ostringstream ostr; Date dt(11,17,1997); ostr << "Judy's next birthday is "; ostr << dt; ostr << ". Let's have a party."; ostr << endl; // --- now display the ostrstream object cout << ostr.str(); return 0; }

C++ Streams © Bruce M. Reynolds & Cliff Green, // using ostream>> for debugging // includes, include guard, using declarations left out class Wheel { // traditional technique private: friend ostream& operator<<(ostream &, Wheel const &); public: Wheel() : radius(15), allSeason(true) {} void clearAllSeason() { allSeason = false; } private: int radius; bool allSeason; }; ostream& operator<< (ostream&, Wheel const&); class Car { // alternative technique, friends not needed public: Car() : velocity(0) { spare.clearAllSeason(); } void go() { velocity = 60; } ostream& streamOut(ostream&) const; private: enum { NWHEELS = 4 }; Wheel wheel[NWHEELS]; Wheel spare; int velocity; }; ostream& operator<<(ostream &, Car const & );

C++ Streams © Bruce M. Reynolds & Cliff Green, // using ostream>> for debugging ostream& operator<<(ostream& os, Wheel const & rhs) { os << "Radius is " << rhs.radius; if ( rhs.allSeason ) { os << " (AllSeason)"; } os << endl; return os; } ostream& operator<<(ostream& os, Car const & rhs) { return rhs.streamOut(os); } ostream& Car::streamOut(ostream& os) const { for( int wdx=0; wdx < NWHEELS; ++wdx) { os << ”//Wheel " << wdx << ": " << wheel[wdx]; } os << ”//Spare: " << spare; os << ”//Velocity is: " << velocity << endl; return os; }

C++ Streams © Bruce M. Reynolds & Cliff Green, // using ostream>> for debugging int main() { Car car; car.go(); cout << car; return 0; } //Wheel 0: Radius is 15 (AllSeason) //Wheel 1: Radius is 15 (AllSeason) //Wheel 2: Radius is 15 (AllSeason) //Wheel 3: Radius is 15 (AllSeason) //Spare: Radius is 15 //Velocity is: 60

C++ Streams © Bruce M. Reynolds & Cliff Green, // forget atoi(), itoa(), etc. #include using std::istringstream; using std::ostringstream; using std::cout; int main() { char const * pstr = ” "; istringstream iss( pstr ); double pi; iss >> pi; ostringstream oss; oss << "Pi is: " << pi; cout << oss.str(); return 0; }