I/O streams

Similar documents
pointers & references

File I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents

I/O Streams and Standard I/O Devices (cont d.)

Chapter 14 Sequential Access Files

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

CS2141 Software Development using C/C++ Stream I/O

Text File I/O. #include <iostream> #include <fstream> using namespace std; int main() {

More File Operations. Lecture 17 COP 3014 Spring april 18, 2018

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

Chapter 3: Input/Output

CSC 138 Structured Programming CHAPTER 4: TEXT FILE [PART 1]

Preview 8/28/2018. Review for COSC 120 (File Processing: Reading Data From a File)

CMPS 221 Sample Final

The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

C++ STREAMS; INHERITANCE AS

Week 5: Files and Streams

C++ Input/Output Chapter 4 Topics

Objects and streams and files CS427: Elements of Software Engineering

Today in CS162. External Files. What is an external file? How do we save data in a file? CS162 External Data Files 1

C++ Input/Output: Streams

CS 103 Unit 14 - Streams

CSc Introduc/on to Compu/ng. Lecture 19 Edgardo Molina Fall 2011 City College of New York

Strings and Stream I/O

CS 103 Unit 14 - Streams

CS3157: Advanced Programming. Outline

Standard Library. Lecture 27. Containers. STL Containers. Standard Library

Streams. Rupesh Nasre.

Lecture 12. Monday, February 7 CS 215 Fundamentals of Programming II - Lecture 12 1

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 7. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

What we will learn about this week:

Chapter 12. Streams and File I/O. Copyright 2016 Pearson, Inc. All rights reserved.

Week 3: File I/O and Formatting 3.7 Formatting Output

BITG 1113: Files and Stream LECTURE 10

Chapter 12. Streams and File I/O. Copyright 2010 Pearson Addison-Wesley. All rights reserved

File I/O Christian Schumacher, Info1 D-MAVT 2013

Advanced I/O Concepts

Convenient way to deal large quantities of data. Store data permanently (until file is deleted).

Chapter 3 - Notes Input/Output

Streams in C++ Stream concept. Reference information. Stream type declarations

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING CS6456 OBJECT ORIENTED PROGRAMMING

IS 0020 Program Design and Software Tools

Simple File I/O.

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

CS201 Latest Solved MCQs

Physics 6720 I/O Methods October 30, C++ and Unix I/O Streams

Chapter 12. Streams and File I/O

Introduction to Programming EC-105. Lecture 2

Object Oriented Programming Using C++ UNIT-3 I/O Streams

UEE1303(1070) S 12 Object-Oriented Programming in C++

Fundamentals of Programming Session 27

Templates and Vectors

Scientific Computing

ENGI 1020 Introduction to Computer Programming R E Z A S H A H I D I J U L Y 2 6,

Linked List using a Sentinel

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping).

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

Lecture 5 Files and Streams

CSE 100: STREAM I/O, BITWISE OPERATIONS, BIT STREAM I/O

1 Pointer Concepts. 1.1 Pointer Examples

C++ files and streams. Lec 28-31

Lecture 9. Introduction

A SHORT COURSE ON C++

Due Date: See Blackboard

Inheritance and aggregation

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

G52CPP C++ Programming Lecture 17

Module C++ I/O System Basics

Streams - Object input and output in C++

Object Oriented Programming COP3330 / CGS5409

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

Function Templates. Consider the following function:

COMP322 - Introduction to C++

Abstract Data Types (ADT) and C++ Classes

Introduction to C++ (Extensions to C)

Programming in C/C

Input and Output File (Files and Stream )

Fundamentals of Programming CS-110. Lecture 2

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

CSC 306 LEXICAL ANALYSIS PROJECT SPRING 2017

C++ does not, as a part of the language, define how data are sent out and read into the program

Engineering Problem Solving with C++, Etter/Ingber

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

Stream States. Formatted I/O

CPE Summer 2015 Exam I (150 pts) June 18, 2015

Topics. bool and string types input/output library functions comments memory allocation templates classes

Fall 2017 CISC/CMPE320 9/27/2017

The C++ Language. ! What happens when executing the following code? int anint; Cout << "The integer is " << anint << endl;

Chapte t r r 9

Understanding main() function Input/Output Streams

Class 14. Input File Streams. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Software Design & Programming I

C++ Programming Lecture 10 File Processing

10/23/02 21:20:33 IO_Examples

Transcription:

I/O streams 1-24-2013

I/O streams file I/O end-of-file eof reading in strings Browse the following libraries at cplusplus.com String Library STL: Standard Template Library (vector) IOStream Library HW#2 posted due: Thursday, 1/31/13 Quiz today

vector<point> polygon; Point p1(100,200); Point p2(150,150); Point p3(150,50); Point p4(75,90); polygon.push_back(p1); polygon.push_back(p2); polygon.push_back(p3); polygon.push_back(p4); for (int i = 0; i < polygon.size(); i++) cout << polygon[i] << endl;

preface.txt -------------------------------------------------- 1 After a few computer science courses, students may start to 2 get the feeling that programs can always be written to 3 solve any computational problem. Writing the program may 4 be hard work. For example, it may involve learning a 5 difficult technique. And many hours of debugging. But 6 with enough time and effort, the program can be written. 7 8 So it may come as a surprise that this is not the case: 9 there are computational problems for which no program -------------------------------------------------- next previous open quit ------- command: o file: introduction.txt

void run_file_viewer() main function of the file viewer displays the given number of lines asks user what to do next: q o quit open a text file vector<string> v_document_lines data structure for storing the document s lines

void run_file_viewer() { vector<string> v_document_lines; string file_name; open_file(file_name, v_document_lines); while (true) { // while command is not 'quit' display(file_name, v_document_lines); cout << "command: "; char command = '-'; cin.get(command); cin.get(); // '\n' switch (command) { case 'q': return; case 'o': { open_file(file_name, v_document_lines); } // end case o } // end switch } // end while } // end run_file_viewer

void display(const string & file_name, const vector<string> & v_document_lines) { cout << endl << file_name << endl; string long_separator(50, '-'); cout << long_separator << endl; for (int i = 0; i < v_document_lines.size(); ++i) cout << setw(3) << i+1 << " " << v_document_lines[i] << endl; cout << long_separator << endl << " open quit" << endl; string short_separator(8, '-'); cout << short_separator << endl; } // end display

An input stream is a flow of characters from a source e.g. cin is of type istream cin >> n; // reads a value for n from the default input stream, the keyboard An output stream is a flow of characters to a destination e.g. cout is of type ostream cout << n // displays the value of n to the default output stream, the screen Use library: #include<iostream>

Another source for input could be a file; likewise another destination for output could be a file. #include <fstream>; ifstream in_data; // input file stream in_data.open( pointdata.txt ); /* opens the file (associates it with the text file named pointdata.txt) */ in_data >> a; // a s value is read from the file

int main() { vector<point> polygon; // creates an empty vector /* read in point data from a file and add to the vector */ ifstream in_data; int a; int b; in_data.open("pointdata.txt"); in_data >> a >> b; polygon.push_back( Point(a,b) ); in_data.close();

Tip: always check whether a file was opened successfully in_data.open("pointdata.txt"); if (in_data.fail()) { cout << "Failed to open input file." << endl; exit(1); // aborts the program with error code 1 }

Would like to read in any number of points /* assume input file contains any number of lines, with two integer values per line */ while (in_data >> a >> b) { polygon.push_back( Point(a,b) ); } in_data.close(); // display the polygon for (int i = 0; i < polygon.size(); i++) cout << polygon[i]; cout << endl;

char infilename[16]; // C string cout << "Enter the input file name << (maximum of 15 characters)" << endl; cin >> infilename; in_data.open(infilename); Example: polygon.cpp

vector<string> roster; // empty roster // pseudocode open the input file and check while( there is unread data in the input file ) { read in the name add to the roster }

ifstream in_data; in_data.eof() // not at end of file in_data.getline( str, n) ; // reads in a line of up to n characters into // the C string str string name; in_data.getline( name, 80 ) // fails

char cname[80]; // stores 79 chars plus \0 in_data.getline(cname, 80); // reads chars into cname until either // 79 chars have been read, or // reached the delimiter ( \n in this case) string name( cname ); // constructor for string can use C string

while(!in_data.eof()) { in_data.getline(cname,80); string name(cname); }; problem: the input file: Sam Jim Bob end of file Betty is really a stream: S a m \n J i m space B o b \n B e t t y \n

while(!in_data.eof()) { in_data.getline(cname,80); string name(cname); if (!name.empty()) roster.push_back(name); }; in_data.close(); for (int i = 0; i < roster.size(); i++) cout << roster[i] << endl; cout << "end of roster" << endl;

For Tuesday, read the following from Maciel: Chapter 3 section 3.3 and 3.4 on I/O streams and string streams. Chapter 4 on error checking Chapter 6 on dynamically allocated arrays