160 likes | 165 Views
Learn about testing programs using stubs, reading and writing to files, and working with constants in C++. Includes examples and syntax.
E N D
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. 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. 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; } 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? CS150 Introduction to Computer Science 1
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; } Example CS150 Introduction to Computer Science 1
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; } Example 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 • For example: #define Pi 3.14159 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!! 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. 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 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 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”); 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! 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 • Examples: • ofil << ch; • Make sure to use correct file pointer! 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 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; } CS150 Introduction to Computer Science 1