1
h07
CS16 W18
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu section
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone"

h07: Chapter 6: File IO

ready? assigned due points
true Tue 02/06 09:00AM Tue 02/13 08:00PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the three lowest scores (if you have zeros, those are the three lowest scores.)


Please:

  • No Staples.
  • No Paperclips.
  • No folded down corners.

Read Chapter 6 on File IO (If you do not have a copy of the textbook yet, there is one on reserve at the library under “COMP000-STAFF - Permanent Reserve”).

PLEASE MARK YOUR HOMEWORK CLEARLY, REGARDLESS OF IF YOU WRITE IT OUT IN INK OR PENCIL!
FOR BEST RESULTS, PRINT THIS PAGE AS A PDF, THEN PRINT THE PDF

    1. (3 pts) What is the output of this program?
    1  #include <iostream>
    2  using namespace std;
    4  int main() {
    5     char first, second;
    6     cout << "Enter a word: ";
    7     first = cin.get();
    8     cin.sync();
    9     second = cin.get();
    10    cout << first << endl;
    11    cout << second << endl;
    12    return 0;
    13  }
    
    (a) First
    (b) Second
    (c) Return first 2 letter of number from the entered word
    2.(3 pts) In C++, all the files are opened in ______________ mode:
    (a) Binary
    (b) Text  
    (c) Can't say
    3.(4 pts) How many streams are automatically created when executing a program:
    (a) 1
    (b) 2
    (c) 3
    4.(12 pts)Please fill the program below to make it output the expected output:
          #include <fstream>
          #include <iostream>
          #include <string>
          using namespace std;
    
          int main ()
          {
             string data;
             _____________ outfile;
             outfile.open("file.dat");
             cout << "Writing to the file" << endl;
             cout << "Enter class name: ";
             ___________________________________________
             outfile << data<< endl;
             cout << "Enter your id: ";
             cin >> data;
             cin.ignore();
             outfile << data<< endl;
             outfile.close();
             ifstream infile;
             cout << "Reading from the file" << endl;
             infile.open("file.dat");
             ___________________________________________
             cout << data << endl;
             ___________________________________________
             cout << data << endl;
             infile.close();
             return 0;
          }
    
    Output
        Writing to the file
        Enter class name: name
        Enter your id: 123
        Reading from the file
        name
        123
    
    5.(4 pts) What is eof() method used for?
    6.(4 pts) The <fstream> header provides three clasees for operating file IO: ifstream, ofstrea and fstream. What are these three used for respectively?
    7.(11 pts) Write number 1 to 100 in a data file NOTES.TXT in C++