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.

Slides:



Advertisements
Similar presentations
CPS120: Introduction to Computer Science INPUT/OUTPUT.
Advertisements

Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
1 CS 105 Lecture 9 Files Version of Mon, Mar 28, 2011, 3:13 pm.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
C++ Numerical Data Input/Output Programming. COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 2 Rules for Division l C++ treats integers.
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.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
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.
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++ Lecture 8 Monday, 25 August I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing.
Week 1 Algorithmization and Programming Languages.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
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:
CPS120: Introduction to Computer Science Formatted I/O.
I/O in C++ October 7, Junaed Sattar. Stream I/O a stream is a flow of bytes/characters/ints or any type of data input streams: to the program output.
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
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.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
CS 11 C++ track: lecture 1 Administrivia Need a CS cluster account sysadmin/account_request.cgi Need to know UNIX (Linux)
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.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
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.
CSCI-1411 F UNDAMENTALS O F C OMPUTING L AB Shane Transue Summer
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
24 4/11/98 CSE 143 Stream I/O [Appendix C]. 25 4/11/98 Input/Output Concepts  Concepts should be review!  New syntax, but same fundamental concepts.
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.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
C++ ReviewEECE 3521 EECS 352 Data Structures and Algorithms José M. Vidal Office: 3A47.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
CS212: Object Oriented Analysis and Design
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
Basics (Variables, Assignments, I/O)
What Actions Do We Have Part 1
Chapter 1.2 Introduction to C++ Programming
Introduction to Programming
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 2 part #3 C++ Input / Output
Standard Input/Output Streams
Standard Input/Output Streams
Basics (Variables, Assignments, I/O)
File I/O Streams, files, strings 1 1.
2.1 Parts of a C++ Program.
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.
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Introduction to cout / cin
Chapter 3 Input output.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
CS150 Introduction to Computer Science 1
Chapter 4 INPUT AND OUTPUT OBJECTS
CS150 Introduction to Computer Science 1
Fundamental Programming
Chapter 2 part #3 C++ Input / Output
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
Introduction to cout / cin
Input / output (i/o) Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Presentation transcript:

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 this class can be composed or reworked for more complex data structures in “real” code

SECOND BRIEF NOTE RAII RAII (Resource Acquisition Is Initialization) Stupid name (admitted by Stroustrup himself) One of the most important C++ concepts! (also admitted by Stroustrup) Simple idea Acquire resources (memory, file pointers, threads) in constructors Release them in destructors Compiler will automatically handle calling destructors (for non pointers…) Important implications in complex code blocks with multiple exits Start of function Acquire resources? Error detected Release resources first Throw will exit function Return 1 Release resources Return 2 Release resources Start of function Acquire resources? Error detected Throw will exit function Return 1Return 2 Without RAII With RAII

ON I/O Algorithms always deal with I/O Data has a need to be persistent or transferred between programs Data could be too large to store in RAM Input (Data) Algorithm (Process Data) Output

MODERN I/O IN C++ Utilizes “streams” Standard Streams ( ) File Streams ( ) String Streams ( ) All streams are based on same concepts and operations and can be broken down into two basic types Input streams Output streams

BASIC STREAM OPERATIONS << – “put to” or “puts” operation Send data to an output stream (note I said “data” and not “string”) cout << “Howdy Aggieland! Home of the ” << 12 << “th man!\n”; >> – “get from” or “gets” operation Read data from an input stream double d; cout << “Enter your favorite rational decimal: ”; cin << d; For user defined types – overload these operators)

STANDARD STREAMS System level I/O, i.e., to the console cin – Standard Input cout – Standard Output cerr – Standard output stream for Errors clog – Standard output stream for Logging Examples cout << “Hello” << endl; cerr << “Kudos, you messed up” << endl;

FILE STREAMS Read/Write to/from external files Output – ofstream Input – ifstream Example ofstream ofs(“MyFile.txt”); if(ofs) //open was a success ofs << “Hello file streams!” << endl; Side Question File streams come with a function close(), which releases the file pointer. Why am I not calling it?

STRING STREAMS Write (convert) data to strings or format strings in nice ways ( ostringstream ) Read data from lines of a file or as tokens from a larger string ( istringstream ) Example double d = 3.14; int i = 5; ostringstream oss; oss << d << “\t” << i;

ESCAPE SEQUENCES Special characters in strings like \n – new line \r – carriage return \t – tab spacing \’ – single quote \” – double quote \\ – backslash etc

FORMATTING STRINGS Use Format for human readability of data Some of the most common uses setw(int n) – set field width cout << setw(10) << 5; //produces “ 5”; setprecision() – sets format of decimal numbers cout << setprecision(5) << ; //produces “3.1416” cout << fixed << ; //now produces “ ” //(notice set precision 5 is still active) Can set to scientific notation as well Can automatically format time and money