Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 1 -- 1Computer Science I - Martin Hardwick Streams In C++ rA stream is a sequence that you either read from or write to. l example – cin is a stream.

Similar presentations


Presentation on theme: "Lecture 1 -- 1Computer Science I - Martin Hardwick Streams In C++ rA stream is a sequence that you either read from or write to. l example – cin is a stream."— Presentation transcript:

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

2 Lecture Computer Science I - Martin Hardwick Histogram of Exam Scores (1) #include using namespace std; void histobar(string label, int length) //PURPOSE: draw line of histogram //PRECONDITIONS: 0 <= length //POSTCONDITIONS: displays label // followed by length asterisks { intk;// loop variable cout << label << " "; for (k=1; k<=length; k++) { cout << "*"; } cout << endl; } rRead a file of exam scores (0 through 100) and draw a histogram of the numbers of As, Bs, Cs, Ds and Fs. rInclude since we are using files. rFunction histobar displays one line of the histogram l line begins with a string label l label is followed by a row of asterisks l note the loop to draw a row of the correct length

3 Lecture 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 { intA, B, C,// count number D, F;// of each grade intscore;// 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"); rFirst we must declare the input file as an ifstream object since we will be reading the file. l the identifier, scorefile, is the name for the file that we will use in the program rBefore we can read a file, we must open it using the open operation. l argument: –name of file in secondary storage l the file should be located in the project folder –otherwise we must use the full path name for the file

4 Lecture Computer Science I - Martin Hardwick Histogram of Exam Scores (3) // loop to read scores from file scorefile >> score; while (!scorefile.eof()) { switch (score/10) { case 0: case 1: case 2: case 3: case 4: case 5: F = F + 1; break; case 6: D = D + 1; break; case 7: C = C + 1; break; case 8: B = B + 1; break; case 9: case 10: A = A + 1; break; rRead from a file using the >> operator. rWe use the standard while loop organization for reading a list of data. l exactly as we did when using cin rHowever, rather than using a sentinel value to mark the end of the data, we use the ifstream operator eof. l no arguments l returns true when the entire file has been read

5 Lecture Computer Science I - Martin Hardwick Histogram of Exam Scores (4) default: cout " << score << endl; } //end switch scorefile >> score; } //end while loop // draw the histogram histobar("A Grades", A); histobar("B Grades", B); histobar("C Grades", C); histobar("D Grades", D); histobar("F Grades", F); return 0; } rThe default case covers incorrect exam scores. l an appropriate message is displayed rThe histogram is produced with five calls to function histobar.

6 Lecture Computer Science I - Martin Hardwick The File examscores.txt File: examscores.txt rNote that the data file contains one incorrect exam score (110). rThe >> operator uses white space (i.e., blanks, tabs, new lines) to separate items in a stream. l there can be any amount of white space between items rConsequently, there can be any number of exam scores per line in the file. l the file can even have blank lines

7 Lecture Computer Science I - Martin Hardwick Results of Running the Program

8 Lecture Computer Science I - Martin Hardwick Writing To A File #include using namespace std; int main () { ofstream outfile;... outfile.open(name.txt);... outfile << This is text << endl;... outfile << setw(10) << x << endl;... outfile.close();... } rYou write to a file just like you write to cout using the << operator. rFirst you must declare and open the file. l if a file with the same name already exists, it is deleted and replaced by the new file rYou should always close a file when you are done writing using the close operation. l it has no arguments

9 Lecture Computer Science I - Martin Hardwick Counting Words In A File (1) #include using namespace std; int main () //PURPOSE: count words in a file //PRECONDITIONS: file name: text.txt //POSTCONDITIONS: rets 0 if success { string word; // word from file ifstream infile; // file to analyze int count; // count of words // Initialize counter variable count = 0; // Open the file to be analyzed infile.open("text.txt"); rWords in a file of text are separated by white space. l hence the >> operator can be used to read a word at a time l read each word into a string variable rThis program reads words from a file and counts them. l include since the program reads from a file l include since the program uses a string variable

10 Lecture Computer Science I - Martin Hardwick Counting Words In A File (2) // Read file and count words infile >> word; while (!infile.eof()) { count = count + 1; infile >> word; } // Display number of words found cout << "The number of words " << "in the file is: " << count << endl; return 0; } rAs each word is read it is counted. rThis is the standard organization using a while loop and the eof operator to read from a file. rThe final word count is displayed on the screen.

11 Lecture Computer Science I - Martin Hardwick Counting Words In A File – Output File text.txt This is a line of text. This is another line of text. This is the last line of text.

12 Lecture Computer Science I - Martin Hardwick Counting Characters In A File (1) #include using namespace std; int main () //PURPOSE: count chars in a file //PRECONDITIONS: file name: text.txt //POSTCONDITIONS: rets 0 if success { charch; // character from file ifstream infile; // file to analyze int count; // count of characters // Initialize counter variable count = 0; // Open the file to be analyzed infile.open("text.txt"); rSuccessive characters in a file of text have no white space between them. l hence the >> operator cannot be used to read characters from a file one by one l need to use a char variable rThis program reads and counts the number of characters in a file. l include since the program reads from a file

13 Lecture Computer Science I - Martin Hardwick Counting Characters In A File (2) // Read file and count characters infile.get(ch); while (!infile.eof()) { count = count + 1; infile.get(ch); } // Display number of chars found cout << Number of characters " << "in the file is: " << count << endl; return 0; } rMust use the get operator for ifstream (and cin) objects. l one argument, which is a char variable l puts the next character from the file in the argument variable rThe rest of the program is exactly like the previous one for counting words in a file.

14 Lecture Computer Science I - Martin Hardwick Counting Characters In A File – Output File text.txt This is a line of text. This is another line of text. This is the last line of text.

15 Lecture Computer Science I - Martin Hardwick Counting Lines In A File (1) #include using namespace std; int main () //PURPOSE: count lines in a file //PRECONDITIONS: file name: text.txt //POSTCONDITIONS: rets 0 if success { string line; // line from file ifstream infile; // file to analyze int count; // count of characters // Initialize counter variable count = 0; // Open the file to be analyzed infile.open("text.txt"); rSuccessive lines in a file of text have \n/endl between them. l hence need a function that reads lines l need to use a string variable rThis program reads and counts the number of lines in a file. l include since the program reads from a file

16 Lecture Computer Science I - Martin Hardwick Counting Characters In A File (2) // Read file and count characters getline(infile, line); while (!infile.eof()) { count = count + 1; getline (infile, line); } // Display number of lines found cout << Number of lines " << "in the file is: " << count << endl; return 0; } rMust use the getline function for ifstream (and cin) objects. l two arguments, the file object and a string variable l puts the next line from the file in the string variable rThe rest of the program is exactly like the previous ones.

17 Lecture Computer Science I - Martin Hardwick Functions For Char Data rThe following functions are available for manipulating char data: l isalpha(ch)TRUE if ch is a letter l isdigit(ch)TRUE if ch is a digit l islower(ch)TRUE if ch is a lowercase letter l isupper(ch)TRUE if ch is an uppercase letter l isspace(ch)TRUE if ch is a blank or other white space l tolower(ch)lowercase letter if ch is an uppercase letter l toupper(ch)uppercase letter if ch is a lowercase letter

18 Lecture Computer Science I - Martin Hardwick Representing Char Data rAn ASCII char value is encoded using 8 bits -- one byte. l therefore, each char variable is addressable in memory rEvery US keyboard character has an 8-bit ASCII code. –you can represent 256 values in 8 bits –there are 128 character codes defined by ASCII –the remaining 128 codes are used for special control and graphics characters rEBCDIC is another encoding of characters (also 8 bits) used by some computers. rISO has defined a third encoding that defines codes for many other languages as well as English. l Often requires more that one byte l Is going to become more and more popular


Download ppt "Lecture 1 -- 1Computer Science I - Martin Hardwick Streams In C++ rA stream is a sequence that you either read from or write to. l example – cin is a stream."

Similar presentations


Ads by Google