1 Lab Session-VIII CSIT-121 Fall 2000 w Formatted Output w Call by Reference w Lab Exercises w File Handling w Lab Exercises.

Slides:



Advertisements
Similar presentations
INFSY 307 C++ Powerpoint 6 File I/O. Stream and File I/O Function Overloading.
Advertisements

File streams Chapter , ,
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
1 Lab Session-8 CSIT-121 Fall 2003 w Call by Reference w Lab Exercise 1 w Lab Exercise for Demo w Practice Problems.
1 Lab Session-XI CSIT121 Fall 2000 b Arrays and Their Usage b Finding the largest element b Lab Exercise 11-A b Searching for some value b Lab Exercise.
1 Lecture 19 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 Lab Session-4 CSIT121 Fall 2004 Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
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.
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.
1 11/8/06CS150 Introduction to Computer Science 1 More Functions! page 343 November 8, 2006.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
1 Lecture 7 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
1 Lab Session-VIII CSIT-121 Spring 2002 w Formatted Output w Call by Reference w Lab Exercises w File Handling w Lab Exercises.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
Programming is instructing a computer to perform a task for you with the help of a programming language.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Streams, Files, and Formatting Chapter Standard Input/Output Streams t Stream is a sequence of characters t Working with cin and cout t Streams.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
1 Lab Session-8 CSIT-121 Spring 2005 Call by Reference Lab Exercise for Demo Practice Problems.
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Chapter 10C++ for Java Programmers1 Chapter 10 Input/Output.
Streams, Files, and Formatting Chapter Standard Input/Output Streams t Stream is a sequence of characters t Working with cin and cout t Streams.
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.
File I/O in C++ II. Open() function Open() is a member function in each classes ( fstream, ifstream, ofstream) Void fstream :: open ( const char *filename,
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
1 Simple File I/O Chapter 11 Switch Statement Chapter 12.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
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,
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
CS116 SENEM KUMOVA METİN. Outline What is C++ ? Some features of C++ Input and Output Operations Manipulators C++ header files Namespaces and scope resolution.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter Topics The Basics of a C++ Program Data Types
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Chapter 9 Scope, Lifetime, and More on Functions
CSCE 206 Lab Structured Programming in C
Standard Input/Output Stream
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Formatted Input, Output & File Input, Output
Reading from and Writing to Files
CSCE 206 Lab Structured Programming in C
File I/O in C++ II.
Reading from and Writing to Files
Presentation transcript:

1 Lab Session-VIII CSIT-121 Fall 2000 w Formatted Output w Call by Reference w Lab Exercises w File Handling w Lab Exercises

2 Formatted Output w Include to format output w You can control w width cout<<setw(12) for next value only w precision cout<<setprecision(4) w showpoint cout<<showpoint w fixed/scientific cout<<fixed w So that your floating point numbers are displayed as desired.

3 Call By Reference w Call by reference means passing the original variable to the function for changing w For example compute_sum_and_mean(int k, int j, int l, int& sum, float& mean) w It has three INPUT arguments and two REFERENCE arguments that are used to output the sum and mean

4 Lab Exercise 8-1 w Using formatted output, try printing out the constants as below, with 3 digits to the right of decimal point and total width of 9 w const float VAL1= w const float VAL2= w const float VAL3= w Remember to close all workspaces and start fresh for each exercise

5 Lab Exercise 8-2 w For reference arguments exercise, develop a program that calls a function to order two integer values in the increasing numerical order. w Function name order_values(int& num1, int&num2) w when passed values (x,y), it orders the same as (y,x) if it finds y to be less than x. w The function is void as it returns the values through reference arguments

6 File Handling w Files are handled easily by including fstream and opening files w If the input file is not there, it will be created by the myfile.open() call w The standard error checking function calls are now given. Please include the same in your exercises

7 File Handling Sample Program w #include w using namespace std; w int main() w {

8 File Handling Sample Program w ifstream my_infile; w ofstream my_outfile; w string filename; w cout<<"Enter a file name to open: "; w cin>>filename; w my_infile.open (filename.c_str()); w

9 File Handling Sample Program w if (my_infile.fail ()) w { cerr << "*** ERROR: Cannot open " << filename << " for input." << endl; w return EXIT_FAILURE; w } w else cerr<<"success"<<endl; w return 0; w }

10 File Handling for Output Files w my_outfile.open (“out.dat”); w if (my_outfile.fail ()) w { w cerr << "*** ERROR: Cannot open " << “out.dat” w << " for output." << endl; w return EXIT_FAILURE; w }

11 Lab Exercise 8-3 w For a simple program, let us get each character from a file and just display it on the screen. w For this purpose, we need to develop a small program w A code segment is here to give you sufficient start-up

12 Lab Exercise 8-3 w char next; w my_infile.get(next); w while (!my_infile.eof()) w { w cout.put(next); w my_infile.get(next) w }