1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.

Slides:



Advertisements
Similar presentations
C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:
Advertisements

File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.
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.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
Computer Skills2 for Scientific Colleges 1 File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
CS 1 Lesson 5 Loops and Files CS 1 -- John Cole.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
1 11/8/06CS150 Introduction to Computer Science 1 More Functions! page 343 November 8, 2006.
Programming is instructing a computer to perform a task for you with the help of a programming language.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
File I/O ifstreams and ofstreams Sections 11.1 &
1 CS161 Introduction to Computer Science Topic #13.
First steps Jordi Cortadella Department of Computer Science.
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.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
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.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Eight: Streams Slides by Evan Gallagher.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
What Actions Do We Have Part 1
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ October 2, 2017.
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
Basic File I/O and Stream Objects
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
Standard Input/Output Stream
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Chapter 3 Input output.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
CHAPTER 4 File Processing.
What Actions Do We Have Part 1
Reading from and Writing to Files
Programming with Data Files
Reading Data From and Writing Data To Text Files
Chapter 1 c++ structure C++ Input / Output
Reading from and Writing to Files Part 2
Reading from and Writing to Files
Presentation transcript:

1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5

2 11/3/08CS150 Introduction to Computer Science 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

3 11/3/08CS150 Introduction to Computer Science 1 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

4 11/3/08CS150 Introduction to Computer Science 1 1. Libraries  To access files you will need to include o

5 11/3/08CS150 Introduction to Computer Science 1 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

6 11/3/08CS150 Introduction to Computer Science 1 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”);

7 11/3/08CS150 Introduction to Computer Science 1 Check File Opened Correctly  Make sure that it opened correctly if(inputInfo != true) { cout << “Error opening input file “; exit(1); }

8 11/3/08CS150 Introduction to Computer Science 1 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;

9 11/3/08CS150 Introduction to Computer Science 1 Example: Writing to a File  The following program asks the user to input numbers and writes these numbers to a file

10 11/3/08CS150 Introduction to Computer Science 1 Example #include 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 11/3/08CS150 Introduction to Computer Science 1 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)

12 11/3/08CS150 Introduction to Computer Science 1 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; }

13 11/3/08CS150 Introduction to Computer Science 1 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.

14 11/3/08CS150 Introduction to Computer Science 1 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();

15 11/3/08CS150 Introduction to Computer Science 1 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

16 11/3/08CS150 Introduction to Computer Science 1 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();

17 11/3/08CS150 Introduction to Computer Science 1 Where to put your text file This directory has the same name as the project.

18 11/3/08CS150 Introduction to Computer Science 1 Adding a text file

19 11/3/08CS150 Introduction to Computer Science 1  Be sure you “Save As” and save the file into the correct directory

20 11/3/08CS150 Introduction to Computer Science 1 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:

21 11/3/08CS150 Introduction to Computer Science 1 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”

22 11/3/08CS150 Introduction to Computer Science 1 Problem  Consider the data file below, where - indicate spaces: d  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;