Presentation is loading. Please wait.

Presentation is loading. Please wait.

File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object.

Similar presentations


Presentation on theme: "File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object."— Presentation transcript:

1 File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object by Opening the File Read or Write to the File using > Operators Close the File

2 #include using namespace std; int main() { ifstream input; int loops, integer, i; float decimal; string name; input.open("mydata.txt"); input >> loops; for(i= 0 ; i < loops; i++) { input >> integer; input >> decimal; input >> name; cout << integer << " "; cout << decimal << " "; cout << name << endl; } return(0); } mydata.txt file 5 8 9.3 Jon 6 14.335 Bill 0 35.67e9 Mary -23 -4.55 Smith -3 -4e3 xyz 8 9.3 Jon 6 14.335 Bill 0 3.567e+010 Mary -23 -4.55 Smith -3 -4000 xyz Output:

3 Functions Lines of code with a Name() Name() can be executed over and over again in the same program.

4 Functions int main() { … displayVals(); … displayVals(); … displayVals(); return(0); } displayVals() { cout << …; return; /*back to where we left off */ }

5 Function Attributes Function Name: Identifier Used to Call Function Function Parameter(s) or Argument(s): Value(s) Passed into Function for Use by Function Code Function Return Value: Value Returned by Function Back to Calling Function

6 Function Parameters (Arguments) May Pass as Many Parameters as Necessary to Function A Copy of the Value of the Parameter Is Passed to the Function Changing the Value of the Parameter in the Function Does Not Affect the Value of the Original Variable This Is Called Pass-by-Value

7 Declaring a Function Function Prototype: Declaring a Function and How It Is Called Syntactically Used by the Compiler to Signal Syntax Errors Pseudocode Example: return_type function_name(parameter_type parameter_name); Example: double calculate_avg(int totOfItems, int numItems);

8 Functions – Return Values Functions Are Typed According to Their Return Values: void, int, double, etc. Functions Are Declared by Function Prototypes (Declarations) Found Above main() Function Returns a Value to Calling Function via return Statement Standard Functions (e.g., Those in STL) Have Function Prototypes in Header Files

9 Scope: Area of a Program within which a Variable Can Be Referenced Variable Definitions Are Recognized in the Curly Braces in which They Were Defined Variables Declared Outside of Functions Are Recognized from the Point Declaration through the Rest of the Program Variable Scope

10 Function Attributes #include using namespace std; void printNum(int); // function prototype int main() { int myNumber = 7; printNum(myNumber); // function call printNum(9); return 0; } // begin function definition void printNum(int numToPrint) { cout << numToPrint; }

11 Another Function Example #include using namespace std; double calcAverage(int total, int numItems); int main() { int allGrades = 974, numStudents = 10; double avgGrade = 0.0; avgGrade = calcAverage(allGrades,numStudents); cout << avgGrade << endl; return 0; } double calcAverage(int total, int numItems) { return (double) total / numItems; }


Download ppt "File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object."

Similar presentations


Ads by Google