CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.

Slides:



Advertisements
Similar presentations
CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
Advertisements

1 Text File I/O Chapter 6 Pages File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening.
File streams Chapter , ,
 2006 Pearson Education, Inc. All rights reserved Stream Input/Output.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
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.
CS 117 Spring 2002 Using Files Hanly: Chapter 9, some in Chapter 4 Freidman-Koffman: Chapter 8, iomanip in Chapter 5.
1 File I/O In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 1Winter Quarter I/O Manipulation Lecture.
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.
Chapter 3 Interactivity and Expressions CSC 125 Introduction to C++ programming.
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.
Streams, Files, and Formatting Chapter Standard Input/Output Streams t Stream is a sequence of characters t Working with cin and cout t Streams.
1 Advanced Input and Output COSC1567 C++ Programming Lecture 9.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
C++ Streams Lecture-2.
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.
File I/O ifstreams and ofstreams Sections 11.1 &
1 CS161 Introduction to Computer Science Topic #13.
Streams, Files, and Formatting Chapter Standard Input/Output Streams t Stream is a sequence of characters t Working with cin and cout t Streams.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
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.
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:
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
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.
Lecture Contents I/O Streams. –Input/output streams. –Unformatted vs formatted streams. –Stream manipulators. –Stream error state. –Stream tying. –Examples.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
1 Simple File I/O Chapter 11 Switch Statement Chapter 12.
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 COMS 261 Computer Science I Title: Functions Date: October 24, 2005 Lecture Number: 22.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
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.
1 Chapter 4 Program Input and the Software Design Process.
C++ How to Program, 8/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
Chapter Expressions and Interactivity 3. The cin Object 3.1.
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.
CS212: Object Oriented Analysis and Design
Introduction to C++ (Extensions to C)
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
COMP 2710 Software Construction File I/O
Chapter 21 - C++ Stream Input/Output
Friday, January 19, 2018 Announcements… For Today… For Next Time…
Standard Input/Output Streams
Standard Input/Output Streams
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.
Chapter 3: Input/Output
Chapter 3 Input output.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
Chapter 4 INPUT AND OUTPUT OBJECTS
File I/O in C++ I.
Formatted Input, Output & File Input, Output
CHAPTER 4 File Processing.
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
Lecture 9 Files Handling
File I/O in C++ I.
Presentation transcript:

CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files

CS-1030 Dr. Mark L. Hornick 2 We’ve already seen these cin – input from keyboard First keypress is read in first cout – output to screen First data out is displayed first

CS-1030 Dr. Mark L. Hornick 3 Buffering of cout Data sent to cout is not always displayed right away It is buffered until flushed Three options cout << endl; // Send newline & flush cout << flush; // Flush only cout << ‘\n’; // Send newline only cout is automatically flushed when cin is accessed

CS-1030 Dr. Mark L. Hornick 4 output streams cout The “normal” output, sometimes redirected to a file You may wish to handle certain types of output differently… There are two extra ostreams for doing this cerr Sent to user, automatically flushed clog Sent to user, uses normal buffering

CS-1030 Dr. Mark L. Hornick 5 Manipulating an ostream The appearance of output can be controlled with manipulators Iostream manipulators don’t print Instead, they change the behavior of the output stream Found in iomanip library #include using namespace ios;

CS-1030 Dr. Mark L. Hornick 6 Manipulating an ostream cout << 20; Outputs “20” in decimal (default) cout << dec << 20 Also outputs “20” in decimal cout << oct << 20; outputs “24” (octal representation of 20) cout << hex << 20; outputs “14” (hexadecimal rep. of 20)

CS-1030 Dr. Mark L. Hornick 7 More Manipulators Output streams default to right-aligned text. To left-align the output: cout << setiosflags( left ) What about other defaults? setw(int w) Control width of display field Non-persistent (next item only) fixed, scientific Style of output

CS-1030 Dr. Mark L. Hornick 8 More Manipulators setprecision(int d) Number of digits of precision setfill(char c) Padding character (only with setw) showbase, noshowbase Displays/hides leading 0 or 0x for octal and hex boolalpha, noboolalpha Display true/false or 1/0

CS-1030 Dr. Mark L. Hornick 9 Streams and Input/Output // #includes istream // #includes ostream

CS-1030 Dr. Mark L. Hornick 10 Reading and Writing to and from strings Insertion (writing) to string: << Display string, subject to setw(int) cout << myString; Extraction (reading) from string: >> Reads only to first whitespace; not to end of line! cin >> myString; Extract entire line Reads to specified delimiter (‘\n’ is newline) getline(cin, myString, ‘\n');

CS-1030 Dr. Mark L. Hornick 11 Accessing Files Input can be read from files Output can be sent to files Use a special stream – fstream Or ifstream – for input only Or ofstream – for output only Behavior is similar to cin and cout Described by the fstream library #include

CS-1030 Dr. Mark L. Hornick 12 First associate a file with a file stream Done during fstream declaration Input file: ifstream Name( ); Output file: ofstream Name( ); Example: ifstream infile(“mydata.txt”); Use \\ or / for DOS/Windows paths

CS-1030 Dr. Mark L. Hornick 13 Reading from a File to a string Open a file and read some data: string fileName = “test.txt”; // filename string strText;// holds text from file ifstream infile(fileName.c_str()); if( infile ) { // write only if open // read from file to string getline( infile, strText ); infile.close(); // close the file }

CS-1030 Dr. Mark L. Hornick 14 Writing to a File Open a file and output some data: string fileName = “test.txt”; // a file ofstream outfile(fileName.c_str()); if( outfile ) { // write only if open outfile << “Outputting to file” << endl; outfile << fileName; // write filename outfile.close(); // close the file }

CS-1030 Dr. Mark L. Hornick 15 Handling File Errors What if there is an error with the file? (i.e. r eading a non-existent file) Simply ask the file stream: ifstream myfile(“baadname.txt”); if (!myfile) cout << “Error accessing file ”; Or use the is_open() method: if (!myfile.is_open()) cout << “Error accessing file ”;