Reading from and Writing to Files

Slides:



Advertisements
Similar presentations
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.
Advertisements

What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
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.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
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.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
File I/O ifstreams and ofstreams Sections 11.1 &
1 CS161 Introduction to Computer Science Topic #13.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
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.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
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++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
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.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Chapter 14: Sequential Access Files
Chapter 1.2 Introduction to C++ Programming
ifstreams and ofstreams
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
LESSON 2 Basic of C++.
CS-103 COMPUTER PROGRAMMING
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
Introduction to C++ October 2, 2017.
COMP 2710 Software Construction File I/O
Copyright © 2003 Pearson Education, Inc.
Basic File I/O and Stream Objects
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
switch Selection Structure
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Let’s all Repeat Together
CS150 Introduction to Computer Science 1
Arithmetic Operations
CPS120: Introduction to Computer Science
CHAPTER 4 File Processing.
do/while Selection Structure
What Actions Do We Have Part 1
Reading from and Writing to Files
C++ Programming Lecture 8 File Processing
ifstreams and ofstreams
Functions Divide and Conquer
Chapter 1 c++ structure C++ Input / Output
Reading from and Writing to Files Part 2
File I/O in C++ I.
Presentation transcript:

Reading from and Writing to Files 10/11/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Last Time We Covered nested loops Today we will Learn how to write C++ programs that can read from and write to files 10/11/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Data Storage Data stored in variables is temporary Files are used to permanently store large amounts of data We will learn how to write programs that can Create files Write to files Read from files This is similar to how we read from the keyboard and wrote to the screen 10/11/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 1. Libraries To access files you will need to include <iostream> <fstream> 10/11/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 2. File Variables ifstream inputInfo; ofstream outputInfo; File variables or pointers are the ways that you refer to the files you are using Can specify which input/output file to use May input from more than one file May output to more than one file 10/11/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 3. Opening Files fileptr.open(“filename”) Same syntax for both input and output files Filename is a string literal Example: ifstream inputInfo; inputInfo.open(“input.dat”, ios::out); 10/11/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 3. Opening Files ios::out Indicates the file opening mode: out: open a file for output in: open a file for input app: append all output to end of file 10/11/04 CS150 Introduction to Computer Science 1

4. Check File Opened Correctly Before we start using the file for reading or writing, we should make sure that it opened correctly if(!inputInfo == true) { cout << “Error opening input file “; exit(1); } Exit(1) forces the program to exit with an error 10/11/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 == true These two statements are equivalent if(!inputInfo == true) if(!inputInfo) Even if you don’t have == true in your loop, C++ will put it there by default This applies to all conditional statements in repetition and selection structures 10/11/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Using File Variables Use input file variable wherever you use cin Examples: inputInfo >> num; Output output file variable wherever you use cout outputInfo << num; 10/11/04 CS150 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/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example #include<fstream> #include<iostream> using namespace std; int main() { ofstream outputInfo; int num; outputInfo.open("out.dat", ios::out); if (!outputInfo) cout << "*** Error opening file" << endl; exit (1); } cout << "Enter a number (9999 to quit): "; cin >> num; while ( num != 9999 ) outputInfo << num << " "; return 0; 10/11/04 CS150 Introduction to Computer Science 1

Example: 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) #include<fstream> #include<iostream> using namespace std; int main() { ifstream inInfo; double num, sum = 0; inInfo.open("in.dat", ios::in); if (!inInfo) cout << "*** Error opening file" << endl; exit (1); } inInfo >> num; while ( num != -9999 ) cout << num << " "; sum += num; cout << endl << "The sum of the above numbers is " << sum << endl; return 0; 10/11/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Reading Untill the EOF It is possible to read from a file until the end is reached while ( inInfo >> num ) { cout << num << " "; sum += num; } 10/11/04 CS150 Introduction to Computer Science 1

CS150 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. 10/11/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Solution ifstream inInfo; char letter; inInfo.open("in.dat", ios::in); if (!inInfo) { cout << "*** Error opening file" << endl; exit (1); } while ( inInfo >> letter ) cout << letter; cout << endl; 10/11/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 The Output 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 10/11/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Solution ifstream inInfo; char letter; inInfo.open("in.dat", ios::in); if (!inInfo) { cout << "*** Error opening file" << endl; exit (1); } while ( inInfo.get( letter ) ) cout << letter; cout << endl; 10/11/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 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 inInfo is the file variable? int i,j; double x,y; char ch; inInfo >> i >> x >> y; inInfo >> i >> j; inInfo >> ch >> i; inInfo >> x >> y >> ch >> x; 12 33.4 -1.99962 12 33 1 2 12.0 33.4 d 12.3 #include<fstream> #include<iostream> using namespace std; int main() { ifstream inInfo; int i, j; double x, y; char ch; inInfo.open("in.dat", ios::in); if (!inInfo) cout << "*** Error opening file" << endl; exit (1); } inInfo >> i >> x >> y; cout << i << endl << x << endl << y << endl; return 0; 10/11/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Summary In today’s lecture we covered Reading to and writing from files Readings P. 809 - 819 10/11/04 CS150 Introduction to Computer Science 1