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

h10: Homework 10: File I/O

ready? assigned due points
true Tue 05/09 12:30PM Thu 05/11 12:30PM

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

MAY ONLY BE TURNED IN IN THE LECTURE LISTED ABOVE AS THE DUE DATE. There is NO MAKEUP for missed assignments.
In place of that, we drop the two lowest scores (if you have zeros, those are the two lowest scores.)


PLEASE MARK YOUR HOMEWORK CLEARLY, REGARDLESS OF IF YOU WRITE IT OUT IN INK OR PENCIL!

1.(18 pts) What member functions do the following in C++11? Show the usual parameters/arguments, if any: Opens a file in ofstream classes:

Opens a file in ifstream classes:

Closes a file in ofstream classes:

Checks to see if opening an input file failed in ifstream classes:

Returns the position in the input file where you want to start reading in ifstream classes:

Returns the position in the output sequence in ofstream classes:

2.(12pts) Assume that the file myData.txt exists and contains only one line that says: “Hello World!”. When users run the following program: (a) What do they see on their screen? and (b) What do they see in the file myData.txt?

#include <iostream>
#include <fstream>
int main () {
	using namespace std;
	ofstream out_stream;
	int x = 55, y =66;
	
	out_stream.open("myData.txt");
	for (int i = 0; i < 5; i++)
		out_stream << x << y << endl;
	out_stream.close();
	
	cout << "File is written";
	return 0;
}

3.(5pts) What line(s) would you change in the program in question 2 (and what would the changes be), if you want the output file to be “myOtherData.txt” and you also wanted to ensure that your program-generated output stream was placed after the end of whatever was in that text file?

4.(5pts) What additional code would you insert in the program in question 2 if you wanted to first check on the existence of the file “myData.txt” and then, if it did not exist, print out a message to the user about the file not existing and subsequently quitting the program? Indicate exactly where you would add this code to the existing code.