Week 5: Files and Streams

Similar documents
Objects and streams and files CS427: Elements of Software Engineering

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

BITG 1113: Files and Stream LECTURE 10

Lecture 3 The character, string data Types Files

Input and Output File (Files and Stream )

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

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

Chapter 3 - Notes Input/Output

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

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

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

Lecture 5 Files and Streams

Simple File I/O.

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

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

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

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

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

C++ Input/Output: Streams

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

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

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

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

Chapter 5: Loops and Files

Introduction. Lecture 5 Files and Streams FILE * FILE *

File Input / Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #9

C Legacy Code Topics. Objectives. In this appendix you ll:

Lab: Supplying Inputs to Programs

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

CpSc 101, Fall 2015 Lab7: Image File Creation

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

Chapte t r r 9

COMP322 - Introduction to C++

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

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

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

Spring CS Homework 12 p. 1. CS Homework 12

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

What will happen if the user presses <enter> after every input? Will all work like below? Enter the values of x, y, z: Result: The sum is 17.

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

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

CS101 Linux Shell Handout

Chapter 12. Streams and File I/O

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

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

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

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

Streams and Basic File I/O Tools for Stream I/O Character I/O Inheritance

CSCE 206: Structured Programming in C++

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

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

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

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

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

Formatting outputs String data type Interactive inputs File manipulators. Access to a library that defines 3. instead, a library provides input

CS103L SPRING 2018 UNIT 7: FILE I/O

Fundamental File Processing Operations 2. Fundamental File Processing Operations

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

C++ Programming Lecture 10 File Processing

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

File Input/Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #10

Fall 2017 CISC/CMPE320 9/27/2017

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

What we will learn about this week:

COMP322 - Introduction to C++

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

120++: a C++ Subset Corresponding to A Project-Based Introduction to C++

Definition Matching (10 Points)

Fundamentals of Programming Session 25

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

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

Getting started with C++ (Part 2)

C++ For Science and Engineering Lecture 12

Chapter 14 Sequential Access Files

IS 0020 Program Design and Software Tools

I/O streams

System Design and Programming II

C++ Input/Output Chapter 4 Topics

C++ files and streams. Lec 28-31

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

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

Software Design & Programming I

PROGRAMMING EXAMPLE: Checking Account Balance

CS150 Assignment 7 Cryptography

C++ Quick Guide. Advertisements

Homework 4: (GRADUATE VERSION)

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

Object Oriented Programming CS250

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

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

Lecture 9. Introduction

CS242 COMPUTER PROGRAMMING

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

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

8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017

CMPS 221 Sample Final

Introduction to C ++

University of Utah School of Computing

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

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

Transcription:

CS319: Scientific Computing (with C++) Week 5: and Streams 9am, Tuesday, 12 February 2019 1 Labs and stuff 2 ifstream and ofstream close a file open a file Reading from the file 3 Portable Bitmap Format (pbm) CS319 Week 5: and Streams 1/17

Labs and stuff (a) The lab 3 assignment is due by 9am, 20 Feb. (Actually, that was a typo: I had intended 19 Feb, but won t change it now). (b) Submitting imperfect code is better than submitting nothing; (c) Try to submit early, and ask for feed-back. If there is potential for improvement, you can re-submit. (d) Have a look at the sample solution to Lab 1 to get a sense for how commenting and indent style can improve clarity. I try to be consistent in my indent style (based on Allman-style, though with smaller indents). If you develop your own style, and are consistent, you ll write better code more efficiently. For more, see https://en.wikipedia.org/wiki/indent_style (e) Use the compiler to help you code. For example, turn all on usual warnings (-Wall) to identify possible bugs in the code. (f) Next Tuesday we will start Lab 3. CS319 Week 5: and Streams 2/17

All of the C++ programs we have looked at so far take their input from the standard input stream, which is usually the keyboard. Example: cout << "Enter an inteter: "; cin >> i; Although the standard input stream can be redirected to be, for example, a file (easily done on Linux), it is usually necessary to open a file from within the program and take the data from there. The data is then processed and written to a new file. CS319 Week 5: and Streams 3/17

To achieve either of these tasks in C++, we create a file stream and use it just as we would cin or cout. We ll start by looking at a simple example: (i) open a file, (ii) count the number of characters, (iii) save this number to a new file. Once we have the basic idea, we ll take a closer look at each operation (opening, reading, writing). CS319 Week 5: and Streams 4/17

ifstream and ofstream When working with files, we need to include the fstream header file. To read from a file, declare an object of type ifstream; to write to a file, declare an object of type ofstream. # include <iostream > 10 # include <fstream > # include <cstdlib > 01CountChars.cpp int main ( void ) 14 { std :: ifstream InFile ; 16 std :: ofstream OutFile ; char c; Open the file by calling the open() member function. To read a single character, can use InFile.get() std :: cout << " Processing... " 20 << " CPlusPlusTerms. txt "; std :: cout << " See file Output. txt for " 22 << " more information."; InFile. open (" CPlusPlusTerms. txt "); 24 OutFile. open (" Output. txt "); 26 int i =0; InFile. get ( c ); CS319 Week 5: and Streams 5/17

close a file If there are no more characters left in the input stream, then InFile.eof() evaluates as true. Use the steam objects just as you would use cin or cout: InFile >> data or OutFile << data. Close the files: InFile.close(), OutFile.close() 01CountChars.cpp while (! InFile. eof () ) { 28 i ++; InFile. get ( c ); 30 } 32 OutFile << " CPlusPlusTerms. txt contains " 34 << i << " characters \ n"; 36 InFile. close (); OutFile. close (); 40 } return (0); CS319 Week 5: and Streams 6/17

open a file The method open works differently for ifstream and ofstream: InFile.open() Opens an existing file for reading, OutFile.open() Opens a file for writing. If it already exists, its contents are overwritten. The first argument to open() contains the file name, and is an array of characters. More precisely, it is of type const char*. For example, we could have opened the input file in the last example with: char InFileName [20]= " CPlusPlusTerms. txt ";... std :: cout << " Processing the contents of " << InFileName << std :: endl ;... InFile. open ( InFileName ); Note that this char array is not the same as a string. The precursor to C++, C, handled strings this way, so they are known as C-style strings. CS319 Week 5: and Streams 7/17

open a file If we do want to use C++ style strings (and we probably do), we have to do it as follows. In this example we ll prompt the user to enter the file name. std :: ifstream InFile ; std :: string InFileName ; std :: cout << " Input the name of a file : " << std :: endl ; std :: cin >> InFileName ; InFile. open ( InFileName. c_str ()) CS319 Week 5: and Streams 8/17

open a file If you are typing the file name, there is a chance you will mis-type it, or have it placed in the wrong folder: so always check that the file was opened successfully. To do this, use the fail() function, which evaluates as true if the file was not opened correctly: if (InFile.fail()){ std::cerr << "Error - cannot open " << InFileName << std::endl; exit(1); } A better approach in this case might be to use a while loop, so the user can re-enter the filename. See 02CountCharsV02.cpp CS319 Week 5: and Streams 9/17

open a file Recall that if you open an existing file for output, its contents are lost. If you wish to append data to the end of an existing file, use To open an existing file and append to its contents, use OutFile.open("Output.txt", std::ios::app);..................................................................... Other related functions include is open() and, of course, close()..................................................................... Above we also saw that InFile.eof() evaluates as true if we have reached the end of the (read) file. Related to this are InFile.clear(); // Clear the eof flag InFile.seekg(std::ios::beg); // rewind to begining. CS319 Week 5: and Streams 10/17

Reading from the file In the above example, we read a character from the file using InFile.get(c). This reads the next character from the InFile stream and stores it in c. It will do this for any character, even non-printable ones (such as the newline char). For example, if we wanted to extend our code above to count the number of lines in the file, as well as the number of characters, we could use: std :: ifstream InFile ; int CharCount =0, LineCount =0;... // Open the file, etc. InFile. get ( c ); while (! InFile. eof () ) { CharCount ++; if (c == \n ) LineCount ++; InFile. get ( c ); } CS319 Week 5: and Streams 11/17

Reading from the file Alternatively, we could the stream extraction operator: InFile >> c; However, this would ignore non-printable characters. One can also use get() to read C-style strings. However, to achieve this task, it can be better to use getline(), which allows us to specify a delimiter character: see the CSV example later. CS319 Week 5: and Streams 12/17

Portable Bitmap Format (pbm) Image analysis and processing is an important sub-field of scientific computing. There are many different formats: you are probably familiar with JPEG/JPG, GIF, PNG, BMP, TIFF, and others. One of the simplest formats is the Netpbm format, which you can read about at https://en.wikipedia.org/wiki/netpbm_format There are three variables: Portable BitMap files represent black-and-white images, and have file extension.pbm Portable GrayMap files represent gray-scale images, and have file extension.pgm Portable PixMap files represent 8-big colour (RGB) images, and have file extension.ppm In this example, we ll focus on.pbm files. CS319 Week 5: and Streams 13/17

Portable Bitmap Format (pbm) CS319.pbm 1 P1 25 9 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 0 1 0 0 1 1 1 1 0 5 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 1 0 0 1 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 1 0 7 0 1 0 0 0 0 1 1 1 1 0 1 1 1 1 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 9 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 0 0 0 0 1 0 11 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 The first line is the magic number. Here P1 means that it is a PBM format ASCII (i.e, plain-text) file. The second line has two integer representing the number of columns and rows of pixels in the image, respectively. The remaining lines store the matrix of pixel values: 0 is white, and 1 is black. CS319 Week 5: and Streams 14/17

Portable Bitmap Format (pbm) The file 03FlipPBM.cpp shows how to read such an image, and output its negative. (See notes from class). 03FlipPBM.cpp std :: ifstream InFile ; 16 std :: ofstream OutFile ; std :: string InFileName, OutFileName ; std :: cout << " Input the name of a PBM file : " << std :: endl ; 20 std :: cin >> InFileName ; InFile. open ( InFileName. c_str ()); while ( InFile. fail () ) 24 { std :: cout << " Cannot open " << InFileName << " for reading." 26 std :: cout << " Enter another file name : "; std :: cin >> InFileName ; 28 InFile. open ( InFileName. c_str ()); } 30 std :: cout << " Successfully opened " << InFileName << std :: endl ; CS319 Week 5: and Streams 15/17

Portable Bitmap Format (pbm) 03FlipPBM.cpp 32 // Open the output file OutFileName = " Negative_ " + InFileName ; 34 OutFile. open ( OutFileName. c_str ()); 36 std :: string line ; // Read the "P1" at the start of the file 38 InFile >> line ; OutFile << " P1" << std :: endl ; // Read the number of columns and rows 42 unsigned int rows, cols ; InFile >> cols >> rows ; 44 OutFile << cols << " " << rows << std :: endl ; 46 std :: cout << " read : cols =" << cols << ", rows =" << rows <<std :: e CS319 Week 5: and Streams 16/17

Portable Bitmap Format (pbm) 03FlipPBM.cpp for ( unsigned int i =0; i< rows ; i ++) 48 { for ( unsigned int j =0; j< cols ; j ++) 50 { int pixel ; 52 InFile >> pixel ; OutFile << 1- pixel << " "; 54 } OutFile << std :: endl ; 56 } InFile. close (); 58 OutFile. close (); 60 std :: cout << " Negative of " << InFileName << " written to " << OutFileName << std :: endl ; 62 return (0); CS319 Week 5: and Streams 17/17