Lecture091 streams - Streams In C A stream is a sequence...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 1 -- 1 Computer Science I - Martin Hardwick Streams In C++ A stream is a sequence that you either read from or write to. example – cin is a stream that you read from example – cout is a stream that you write to streams support the operators << and >> Streams are also used in C++ to represent files. use the >> operator to read the next item from a file use the << operator to write an item into a file You must declare a file stream before you can use it. cin and cout are declared for you automatically A file to be read is declared as an ifstream object. A file to be written is declared as an ofstream object. You must include <fstream> in a program that uses files. <fstream> defines the ifstream and ofstream objects
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture 1 -- 2 Computer Science I - Martin Hardwick Histogram of Exam Scores (1) #include <iostream> #include <fstream> #include <string> using namespace std; void histobar(string label, int length) //PURPOSE: draw line of histogram //PRECONDITIONS: 0 <= length //POSTCONDITIONS: displays label //     followed by length asterisks { int k; // loop variable cout << label << " "; for (k=1; k<=length; k++) { cout << "*"; } cout << endl; } Read a file of exam scores (0 through 100) and draw a histogram of the numbers of A’s, B’s, C’s, D’s and F’s. Include <fstream> since we are using files. Function histobar displays one line of the histogram line begins with a string  label label is followed by a row of  asterisks note the loop to draw a row  of the correct length
Background image of page 2
Lecture 1 -- 3 Computer Science I - Martin Hardwick Histogram of Exam Scores (2) int main () //PURPOSE: histogram a set of  //     exam grades from a file //PRECONDITIONS: exam grades  //     in file examscores.txt //POSTCONDITIONS: histogram  //     displayed on screen { int A, B, C, // count number D, F; // of each grade int score; // score from file ifstream scorefile; // grade file // initialize grade counts A = 0;  B = 0;  C = 0;   D = 0;  F = 0; // open exam score file scorefile.open("examscores.txt"); First we must declare the input file as an ifstream object since we will be reading the file. the identifier, scorefile, is  the name for the file that  we will use in the program Before we can read a file, we must open it using the open operation. argument:
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/30/2012 for the course CSCI 101 taught by Professor Hardwick during the Fall '08 term at Rensselaer Polytechnic Institute.

Page1 / 18

Lecture091 streams - Streams In C A stream is a sequence...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online