CS150 Introduction to Computer Science 1

Slides:



Advertisements
Similar presentations
CMSC 2021 C++ I/O and Other Topics. CMSC 2022 Using C++ Stream I/O Default input stream is called cin Default output stream is called cout Use the extraction.
Advertisements

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 Text File I/O Chapter 6 Pages File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening.
CPS120: Introduction to Computer Science Lecture 15 B Data Files.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
#include using namespace std; void main() { int a[3]={10,11,23}; for(int i=0;i
1 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.
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.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Program Input and the Software Design Process ROBERT REAVES.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Chapter 8 Streams and Files Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
Chapter 8 Data File Basics.
C++ Introduction : C++ compilation. Visual Studio 2008 : Creating Command-Line Program.
1 CS161 Introduction to Computer Science Topic #13.
Topics 1.File Basics 2.Output Formatting 3.Passing File Stream Objects to Functions 4.More Detailed Error Testing 5.Member Functions for Reading and 6.Writing.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
First steps Jordi Cortadella Department of Computer Science.
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.
Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.
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.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
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.
13/15/2016CS150 Introduction to Computer Science 1 Summary  Assignment due on Wednesday, October 29,  Tutor will be in the lab on Tuesday evening,
File I/O. Files allow permanent storage of programs and data. ifstream and ofstream objects –For file I/O the inclusion of is required. –Objects for file.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
Chapter 14: Sequential Access Files
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
Lecture 9 Files, Pointers
Copyright © 2003 Pearson Education, Inc.
Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:
File I/O.
Interactive I/O Input from keyboard Must prompt user User friendly
Basic File I/O and Stream Objects
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
Text Files All the programs you've seen so far have one thing in common: Any data the program uses or calculates is lost when the program ends. In order.
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Standard Input/Output Stream
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
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CISC181 Introduction to Computer Science Dr
Formatted Input, Output & File Input, Output
CS150 Introduction to Computer Science 1
CPS120: Introduction to Computer Science
CHAPTER 4 File Processing.
CS150 Introduction to Computer Science 1
Reading from and Writing to Files
Programming with Data Files
Reading Data From and Writing Data To Text Files
Reading from and Writing to Files Part 2
Lecture 9 Files Handling
File I/O in C++ I.
Reading from and Writing to Files
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

CS150 Introduction to Computer Science 1 Summary Don’t forget! Assignment is due on Wednesday, October 29, 2003. 2/24/2019 CS150 Introduction to Computer Science 1

Ifstreams and Ofstreams So far, we have been reading user input from the keyboard. We have also been writing the output of our programs directly to the screen. What if we want to read in a large amount of data that is stored in a file. What if we want to write a large amount of data and store it in a file. 2/24/2019 CS150 Introduction to Computer Science 1

Steps for External files include<fstream> Declare file variables (pointers) that will correspond to the files you are using Input files type: ifstream Output files type: ofstream Open file Use file for input/output Close files 2/24/2019 CS150 Introduction to Computer Science 1

Declaring File pointers ifstream ifil; ofstream ofil; 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 2/24/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Opening files <fileptr>.open(<filename>) Same syntax for both input and output files Filename may be string literal or char array variable Example: ifstream ifil; ifil.open(“input.dat”); 2/24/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Checking for errors Should check for errors opening file if(ifil.fail()) { cout << “Error opening input file “; exit(1); } Note: you must open files first before using them! 2/24/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Using file pointers Use input file pointer wherever you use cin Examples: ifil >> ch; Output output file ptr wherever you use cout ofil << ch; Make sure to use correct file pointer! 2/24/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Closing files Must do at end when file is finished ifil.close(); ofil.close(); Same syntax for input and output files 2/24/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example #include<fstream> #include<iostream> #define inFile "in.txt" int main( void ) { ifstream ifil; //input stream string name; ifil.open(inFile); if (ifil.fail()) cout << "*** Error opening file" << endl; exit (1); } while (!ifil.eof()) ifil >> name; cout << name << " "; ifil.close(); return 0; 2/24/2019 CS150 Introduction to Computer Science 1