Class Notes (1,051,408)
US (414,376)
CSCI (23)
CSCI 10 (18)
Lecture 20

CSCI 10 Lecture 20: CSCI 10 – Day 20Premium

2 pages59 viewsFall 2016

Department
Computer Science
Course Code
CSCI 10
Professor
Bannister
Lecture
20

This preview shows half of the first page. to view the full 2 pages of the document.
CSCI 10 Day 19
FILE I/O
Announcements:
-Homework 7 is due Friday!
-Midterm grades: Mean = 80% and Median = 86%!
-STS Presentation details on webpage today!
LECTURE
-Final version of grading program: now with files
-Requirement: user should not need to modify code
-User input will look like:
Michael Bannister, 50, 80, 95
*Keep in mind for your program design that some countries use commas
for decimal points…
-Output will look like:
Michael Bannister, 50, 80, 95, xy.z, C // the last two are percentage and
letter grade
Streams
-We dump data into (<<) output streams, e.g. cout
-We pull data out of (>>) input streams, e.g. cin
-Note: streams ignore whitespace
Why do we want files?
-Long-term/permanent storage
-Large amounts of data (more than that would fit in RAM)
-File reading/writing is sequential, must be read front to back
File objects
-Need to #include <fstream>
-In file stream: ifstream
-Out file stream: ofstream
-Basic operations: open, <<, >>, close and fail
-we will use object operations (dot syntax)
-Ways you could fail at opening a file:
-File not found
-Wrong format
-No permission to read (not to some network resources)
Reading from a file
ifstream in_stream;
in_stream.open("input.txt");
if (in_stream.fail()) {
cout << "Failed to open input.txt" << endl;
exit(1);
find more resources at oneclass.com
find more resources at oneclass.com
You're Reading a Preview

Unlock to view full version

Subscribers Only

Loved by over 2.2 million students

Over 90% improved by at least one letter grade.