1 |
h07 |
CS16 Su17 |
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 07/18 02:00PM | Tue 07/25 02: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 4, sections 4.3 - 4.5 and Chapter 9, section 9.1 (pages 508-515)(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 }
#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;
}
Writing to the file
Enter class name: name
Enter your id: 123
Reading from the file
name
123