CS150 Introduction to Computer Science 1

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

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.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
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.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Chapter 9 I/O Streams and Data Files
1 CS161 Introduction to Computer Science Topic #13.
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.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
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.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
1 2/2/05CS250 Introduction to Computer Science II Pointers.
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.
13/15/2016CS150 Introduction to Computer Science 1 Summary  Assignment due on Wednesday, October 29,  Tutor will be in the lab on Tuesday evening,
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Chapter 14: Sequential Access Files
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 7: User-Defined Functions II
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
Chapter 6: Modular Programming
Lecture 9 Files, Pointers
Basic Elements of C++.
Pointers and Pointer-Based Strings
Copyright © 2003 Pearson Education, Inc.
Basic Elements of C++ Chapter 2.
User Defined Functions
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
Programming Funamental slides
Today’s Lecture I/O Streams Tools for File I/O
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.
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
Functions Divide and Conquer
CS150 Introduction to Computer Science 1
Pointers and Pointer-Based Strings
CPS120: Introduction to Computer Science
CHAPTER 4 File Processing.
CS150 Introduction to Computer Science 1
Dynamic Memory.
do/while Selection Structure
What Actions Do We Have Part 1
CS150 Introduction to Computer Science 1
CS250 Introduction to Computer Science II
Reading from and Writing to Files
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Reading from and Writing to Files Part 2
CS150 Introduction to Computer Science 1
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 We have still not finished talking about functions. Today we will finish functions and start talking about reading and writing to files. Don’t forget! Assignment is due on Wednesday, October 29, 2003. 2/21/2019 CS150 Introduction to Computer Science 1

Testing a Program Using Stubs When you have a large program with many functions, it is usually a good idea to create dummy functions or stubs. These will have the proper prototypes, but they will not actually doing anything. It is a good way of testing that the values are being passed to and from the functions correctly. 2/21/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example void computeSum(int, int, int&); int main( void ) { int x, y, sum; x = 4; y = 5; sum = 0; computeSum (x, y, sum); cout << x << y << sum; return 0; } void computeSum(int num1, int num2, int& sum) cout << “Function computeSum entered with the values ”; cout << num1 << “, “ << num2 << “ and “ << sum << endl; sum = 50; 2/21/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Last Word on Functions Arguments can be input (receives) and/or output (sends) When would an argument be both? Can be tricky… How can you identify arguments that are used both for input and output? Do we know any examples? How do we identify them in our comments? 2/21/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example void computeSum(int, int, int&); int main( void ) { int x, y, sum; x = 4; y = 5; sum = 0; computeSum(x, y, sum); cout << x << y << sum; return 0; } // Function: computeSum // Purpose: computes the sum of // two numbers // In: num1, num2 // Out: sum void computeSum(int num1, int num2, int& sum) { sum = num1 + num2; } 2/21/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example void computeSumAndChange(int, int&, int&); int main( void ) { int x, y, sum; x = 4; y = 5; sum = 0; computeSumAndChange(x, y, sum); cout << x << y << sum; return 0; } // Function: computeSumAndChange // Purpose: computes the sum of two // numbers and changes // one of the values // In: num1 // Out: sum // In/Out: num2 void computeSumAndChange(int num1, int& num2, int& sum) { sum = num1 + num2; num2 = 7; } 2/21/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Constants Revisited We know that we can declare constants in C++ as follows: const type identifier = constant; For example: const int Pi = 3.14159; There is another way of defining constants: #define identifier replacement-text #define Pi 3.14159 2/21/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 #define #define is a compiler directive It’s different from a variable or const Form: #define X Y The compiler replaces all instances of X with Y No memory space is created Used in C primarily and in older C++ code Not considered true C++ programming, but it’s useful NOTE: NO SEMICOLON!! 2/21/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/21/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/21/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/21/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/21/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/21/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/21/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/21/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/21/2019 CS150 Introduction to Computer Science 1