Input / output (i/o) Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.

Slides:



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

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.
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.
CS 117 Spring 2002 Using Files Hanly: Chapter 9, some in Chapter 4 Freidman-Koffman: Chapter 8, iomanip in Chapter 5.
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
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.
Chapter 11 Customizing I/O Bjarne Stroustrup
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.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
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.
Week 13 - Friday.  What did we talk about last time?  Server communications on a socket  Function pointers.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
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.
CS212: Object Oriented Analysis and Design
Chapter 1.2 Introduction to C++ Programming
What is wrong in the following function definition
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Topic Pre-processor cout To output a message.
Basics (Variables, Assignments, I/O)
Chapter 1.2 Introduction to C++ Programming
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 3: Expressions and Interactivity.
Basics (Variables, Assignments, I/O)
Copyright © 2003 Pearson Education, Inc.
Chapter 11 Customizing I/O
Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:
Friday, January 19, 2018 Announcements… For Today… For Next Time…
Chapter 2 part #3 C++ Input / Output
Input and Output Chapter 3.
Standard Input/Output Streams
Standard Input/Output Streams
Object Oriented Programming COP3330 / CGS5409
Basics (Variables, Assignments, I/O)
File I/O Streams, files, strings 1 1.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
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.
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
Chapter 11 Customizing I/O
Chapter 4 INPUT AND OUTPUT OBJECTS
Formatted Input, Output & File Input, Output
Chapter 2: Introduction to C++.
Fundamental Programming
Chapter 11 Customizing I/O
Chapter 2 part #3 C++ Input / Output
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
Introduction to cout / cin
C++ for Engineers and Scientists Second Edition
Presentation transcript:

Input / output (i/o) Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny

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 Start of function Without RAII With RAII Acquire resources? Error detected Release resources first Throw will exit function Return 1 Release resources Return 2 Without RAII Start of function Acquire resources? Error detected Throw will exit function Return 1 Return 2 With 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

Algorithm (Process Data) 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 (<iostream>) File Streams (<fstream>) String Streams (<sstream>) 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 Examples 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 Side Question: 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 <iomanip> 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) << 3.1415926; //produces “3.1416” cout << fixed << 3.1415926; //now produces “3.14159” //(notice set precision 5 is still active) Can set to scientific notation as well Can automatically format time and money

References RAII - http://www.tomdalling.com/blog/software-design/resource-acquisition-is-initialisation-raii-explained/ OSStream - http://www.cplusplus.com/reference/sstream/ostringstream/str/ Read comma delimiter file - http://www.cplusplus.com/forum/general/17771/