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

Similar documents
BITG 1113: Files and Stream LECTURE 10

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

Simple File I/O.

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

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

Input and Output File (Files and Stream )

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

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

Streams. Rupesh Nasre.

Chapter 5: Loops and Files

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

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

COL 100. Minor 2 Exam - Practice

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

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

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

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

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

Chapter 14 Sequential Access Files

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

C++ files and streams. Lec 28-31

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

Scientific Computing

A SHORT COURSE ON C++

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Why Is Repetition Needed?

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

CS 103 Unit 14 - Streams

Understanding main() function Input/Output Streams

C++ Programming Lecture 10 File Processing

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

Software Design & Programming I

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:

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

Summary of basic C++-commands

CS2255 HOMEWORK #1 Fall 2012

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

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

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

Lecture 3 The character, string data Types Files

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

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

CS101 Linux Shell Handout

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

CS 103 Unit 14 - Streams

Chapter 12. Streams and File I/O

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

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

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

CSCI 1061U Programming Workshop 2. C++ Basics

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

Chapter 3 - Notes Input/Output

Sonoma State University Computer Science Department CS 115 Fall 2010 Watts. Project 3 Part 1

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

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

ECE 2036 Lab 1: Introduction to Software Objects

Week 5: Files and Streams

Chapter void Test( int, int, int ); // Function prototype int main() // Function heading { int h; // Local variable

CS201 (Intro. to Computing) Sample Questions & Solutions for the FINAL Exam

Your First C++ Program. September 1, 2010

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

PROGRAMMING EXAMPLE: Checking Account Balance

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

Definition Matching (10 Points)

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.

Computer Programming : C++

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

I/O streams

CMPS 221 Sample Final

C++ Input/Output: Streams

Chapter 1 INTRODUCTION

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

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

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

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

CMSC 202 Midterm Exam 1 Fall 2015

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

System Design and Programming II

CS103L SPRING 2018 UNIT 7: FILE I/O

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

Engineering Problem Solving with C++, Etter/Ingber

Class Example. student.h file: Declaration of the student template. #ifndef STUDENT_H_INCLUDED #define STUDENT_H_INCLUDED

Name SECTION: 12:45 2:20. True or False (12 Points)

More About File Reading.

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

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

Object-Oriented Programming in C++

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

CSCE Practice Midterm. Data Types

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

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

4. Structure of a C++ program

Developed By : Ms. K. M. Sanghavi

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

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

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Chapte t r r 9

Chapter 3: Input/Output

Transcription:

Reading from and Writing to Files Section 3.12 & 13.1 & 13.5 11/3/08 CS150 Introduction to Computer Science 1 1 Files (3.12) Data stored in variables is temporary We will learn how to write programs that can o Create files o Write to files o Read from files 11/3/08 CS150 Introduction to Computer Science 1 2 Steps to Using Files There are five steps that must be taken in order to use files in C++ 1. Include header files 2. Define a file stream object o variable to represent a file 3. Open the file 4. Use the file 5. Close the file 11/3/08 CS150 Introduction to Computer Science 1 3 1

1. Libraries To access files you will need to include o <iostream> o <fstream> 11/3/08 CS150 Introduction to Computer Science 1 4 2. File Stream Objects (Variable) ifstream inputfile; ofstream outputfile; fstream inandout; File stream objects are the ways that you refer to the files you are using o Can specify which input/output file to use o May input from more than one file o May output to more than one file 11/3/08 CS150 Introduction to Computer Science 1 5 3. Opening Files inputfile.open( filename ) Same syntax for both input and output files Filename is a string literal Example: ifstream inputfile; inputfile.open( input.dat ); 11/3/08 CS150 Introduction to Computer Science 1 6 2

Check File Opened Correctly Make sure that it opened correctly if(inputinfo!= true) cout << Error opening input file ; exit(1); 11/3/08 CS150 Introduction to Computer Science 1 7 4. Using File Streams Use input file variable wherever you use cin Examples: o inputfile >> num; Output output file variable wherever you use cout Examples: o outputfile << num; 11/3/08 CS150 Introduction to Computer Science 1 8 Example: Writing to a File The following program asks the user to input numbers and writes these numbers to a file 11/3/08 CS150 Introduction to Computer Science 1 9 3

Example #include<fstream> #include<iostream> using namespace std; int main() ofstream outputfile; int num; outputinfo.open("out.dat"); if(!outputinfo) cout << "*** Error opening file" << endl; exit (1); cout << "Enter a number (9999 to quit): "; cin >> num; while(num!= 9999) outputfile << num << " "; cin >> num; outputinfo.close(); return 0; 11/3/08 CS150 Introduction to Computer Science 1 10 Reading from a File Write a program that will read in a sequence of numbers (double) from a file and calculate the sum. Assume that the last number is the trailer (-9999) 11/3/08 CS150 Introduction to Computer Science 1 11 Reading Until the EOF (p 811) It is possible to read from a file until the end is reached while(inputfile >> num) cout << num << " "; sum += num; 11/3/08 CS150 Introduction to Computer Science 1 12 4

Reading Characters Write a program that reads in some text from a file and outputs that text to the screen The file contains: Hello Everyone! I'm a file that contains some text. 11/3/08 CS150 Introduction to Computer Science 1 13 Solution ifstream inputfile; char letter; inputfile.open("in.dat"); if (!ininfo) cout << "*** Error opening file" << endl; exit (1); while (inputfile >> letter) cout << letter; cout << endl; inputfile.close(); 11/3/08 CS150 Introduction to Computer Science 1 14 The Output (p 828) HelloEveryone!I'mafilethatcontainssometext. What s happened?! All spaces, tabs, and new lines have been ignored. This is because >> only reads visible characters How can we read all characters so that the output looks exactly like the input 11/3/08 CS150 Introduction to Computer Science 1 15 5

Solution ifstream inputfile; char letter; inputfile.open("in.dat"); if(!ininfo) cout << "*** Error opening file" << endl; exit (1); while(inputfile.get(letter)) cout << letter; cout << endl; inputfile.close(); 11/3/08 CS150 Introduction to Computer Science 1 16 Where to put your text file This directory has the same name as the project. 11/3/08 CS150 Introduction to Computer Science 1 17 Adding a text file 11/3/08 CS150 Introduction to Computer Science 1 18 6

Be sure you Save As and save the file into the correct directory 11/3/08 CS150 Introduction to Computer Science 1 19 Practice Write a program that will read the following file and find the largest value. The file may contain any number of values. Output the largest value to the screen and to the file largest.txt. File: 59 98 99 77 66 73 85 11/3/08 CS150 Introduction to Computer Science 1 20 Practice Read in the following file. The rows represent students, the columns represent assignments. There are 5 assignments and an unknown number of students. Find the average grade for each student and for each assignment. Print the results to the screen and the file results.txt 99.0 98.0 89.5 77.5 66.0 73.0 85.0 77.5 89.5 89.0 62.0 84.0 83.0 77.0 88.5 11/3/08 CS150 Introduction to Computer Science 1 21 7

Problem Consider the data file below, where - indicate spaces: --12--33.4 -d--12.3-2--5 What values would be assigned to the variables for each of the statements below where inputfile is the file variable? int i,j; double x,y; char ch; o inputfile >> i >> x >> y; o inputfile >> i >> j; o inputfile >> ch >> i; o inputfile >> x >> y >> ch >> x; 11/3/08 CS150 Introduction to Computer Science 1 22 8