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

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

Chapter 14 Sequential Access Files

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

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

what are strings today: strings strings: output strings: declaring and initializing what are strings and why to use them reading: textbook chapter 8

Streams. Rupesh Nasre.

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

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

Chapter 3: Input/Output

C++ Input/Output: Streams

Simple File I/O.

File I/O CS 16: Solving Problems with Computers I Lecture #9

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

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

What we will learn about this week:

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

This chapter introduces the notion of namespace. We also describe how to manage input and output with C++ commands via the terminal or files.

VuZs Team's Work. CS201 Spring Solved by vuzs Team with Reference Written by Administrator Wednesday, 19 May :52

We will exclusively use streams for input and output of data. Intro Programming in C++

Fall 2017 CISC/CMPE320 9/27/2017

Chapter 3 - Notes Input/Output

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

CS201 Latest Solved MCQs

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

Lab: Supplying Inputs to Programs

C++ Input/Output Chapter 4 Topics

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

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

Unit-V File operations

Week 5: Files and Streams

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

Name Section: M/W T/TH Number Definition Matching (8 Points)

Understanding main() function Input/Output Streams

Software Design & Programming I

Developed By : Ms. K. M. Sanghavi

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

CS 103 Unit 14 - Streams

Chapter-12 DATA FILE HANDLING

CS 103 Unit 14 - Streams

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Intermediate Programming / C and C++ CSCI 6610

Lecture 3 The character, string data Types Files

Introduction to C ++

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

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

Setting Justification

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

Name Section: M/W T/TH Number Definition Matching (6 Points)

Chapter Overview. I/O Streams as an Introduction to Objects and Classes. I/O Streams. Streams and Basic File I/O. Objects

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

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

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Fundamentals of Programming Session 23

Chapte t r r 9

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

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

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

CS103L SPRING 2018 UNIT 7: FILE I/O

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

CS101 Linux Shell Handout

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

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Files. In this lesson you will learn how to create and read files. There are two types of files that we will look at: text files and binary files.

File handling Basics. Lecture 7

C++ files and streams. Lec 28-31

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

by Pearson Education, Inc. All Rights Reserved. 2

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

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

Streams. Parsing Input Data. Associating a File Stream with a File. Conceptual Model of a Stream. Parsing. Parsing

After going through this lesson, you would be able to: store data in a file. access data record by record from the file. move pointer within the file

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

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

Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type

CPSC 427: Object-Oriented Programming

File I/O and String Manipula3ons CS 16: Solving Problems with Computers I Lecture #11

Review: C++ Basic Concepts. Dr. Yingwu Zhu

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

Standard File Pointers

Object Oriented Programming COP3330 / CGS5409

Overview of Lecture. 1 Overloading I/O Operators. 2 Overloading << and >> for Fractions. 3 Formatted vs Unformatted Input

Programming II with C++ (CSNB244) Lab 10. Topics: Files and Stream

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

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

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

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

Objects and streams and files CS427: Elements of Software Engineering

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

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

CS Exam 2 Study Suggestions

Why Is Repetition Needed?

Object Oriented Programming In C++

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Introduction. Lecture 5 Files and Streams FILE * FILE *

CS164: Programming Assignment 2 Dlex Lexer Generator and Decaf Lexer

istream& operator >> (istream &sin, Widget &object); ostream& operator << (ostream &sout, Widget &object);

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Chapter 12. Streams and File I/O

Transcription:

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

Streams and files We have already talked about the standard input stream (cin), and the standard output stream (cout). A stream is a sequence of data, one piece of data after the other. cin is actually an object of type istream (input stream). cout is an object of type ostream (output stream).

Streams and files The input operator >> is smart! The standard input stream cin will input a variable of the given type, e.g. cin >> x, will read in a double precision number if x is declared as double x; If, for example, there are characters in the input stream that are input from the console, then these can t be part of x, since x is a number (except possible for the letter e, which stands for an exponent, e.g. 9.34e+2).

File streams File streams are streams that reside on disk rather than input from the keyboard or output to the screen. Files should be opened before they are used, and closed after they are used. Otherwise, file operations will not behave as expected.

File streams Use the same operators << for outputting to a file (as with outputting to standard output), and >> for inputting from a file (as with inputting from the keyboard). E.g. ifstream inputfile; char nextcharacter; inputfile >> nextcharacter; Always put the filename first, then the operator, and then the variable we are reading into. >> will exclude whitespace when it reads from a file.

Reading into input streams (either files or regular streams) Reading in a double precision x. The arrow is the current position in the input stream. This arrow moves along as we read in more info from the file. ifstream inputfile; double x; inputfile >> x;

File streams The input file stream class is ifstream (for reading from files on disk). The output file stream class is ofstream (for writing to files on disk). Can input from an ifstream using >>, get or getline. Can output to an ofstream with << or put.

Input and output redirection After we compile, a program we get an executable file, often ending in the suffix.exe, e.g. countwords.exe If we wish to run this program from the command line, but instead of accepting standard input from the keyboard, we want to get standard input from a file, we may use input redirection, and run the program as follows: countwords.exe < inputfile.txt

Input and output redirection If instead of writing the results to the screen, we wish to write the results to another file, e.g. if there are many lines of output and we do not wish to overwhelm the user, then we use output redirection with > Can use both < and > together. E.g. countwords.exe < inputfile.txt > output.txt Difference between this and ifstream/ofstream is that the input and output files are specified on the command line and not in the program itself.

File streams non member getline function getline is a non-member function of ifstream, which allows the programmer to input a whole line up to the next newline character to the second argument: string aline; getline(infile, aline); If instead you wish to input all characters up to another character (not the newline \n character), then this would be the third argument of the getline function: This reads all characters up to the next occurrence of * in the file, and so can read multiple lines. string aline; getline(infile, aline, '*');

File stream methods As with strings, there are six file stream methods that operate on file stream (ifstream and ofstream) objects. The first two are open() and close(). Suppose we have a file with the entire St. John s and area phone book, which is called phonebook.txt Before we can read from the file, we must open it. To do this, we have to create an object of type ifstream, and then use the open method on it: ifstream phonebookfile; phonebookfile.open( phonebook.txt );

File stream methods After performing some operations on the phone book, e.g. extracting all the phone numbers of a group of friends, we may want to write the phone numbers to another file, so that we can easily access them at any time. Then we can write our friends phone numbers to a file named friends.txt. We must create an object of type ofstream, and then open that file for writing: ofstream friendsfile; friendsfile.open( friends.txt );

File stream methods After we are done reading from a file or writing to a file, and have done whatever we want with the contents of the file, we must close the file. This is accomplished with the close method. Works for both ifstream and ofstream. So, e.g. phonebookfile.close(); friendsfile.close();

File stream methods For ifstreams, we need a method to input from the file that we have opened. This is the get() method, and returns the next character in the ifstream. Method prototype: char get() In a similar fashion, we need a way to output a character to a file we have opened for writing this writes the character stored in the variable c to the ofstream. Method prototype: void put(char c)

File stream methods To actually call these methods on certain objects is easy: char c; phonebookfile.get(c); string friend = Joe ; for (int i = 0; i < friend.length(); i++) { friendfile.put(friend.at(i)); } friendfile.put( \n );

File stream methods Two other methods are eof() and fail(). fail() is a boolean function (returns true or false), which only returns true if some error occurred when reading from or writing to an ifstream/ofstream. eof() is a boolean function which returns true only if the end of the input file stream (ifstream) that we are reading has been reached. E.g. could have: while (!inputfile.eof()) { inputfile.get(c); }