Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab 7: File Input/Output Graham Northup

Similar presentations


Presentation on theme: "Lab 7: File Input/Output Graham Northup"— Presentation transcript:

1 Lab 7: File Input/Output Graham Northup

2 Front Matter This presentation is coming in place of the Functions presentation, which will be coming next week. Functions will probably require the whole period, whereas this presentation is intentionally brief so as to discuss the recent exam.

3 Objectives To understand the concept of streams as provided by the iostream header of the C++ standard library. To be able to read and write files to the filesystem from a program. To effectively format input and output. To understand what cin and cout are.

4 cin and cout Today we’re going to cover part of the “magic incantation” that we started off with, as well as the exact nature of the oft-used cin and cout. #include <iostream> using namespace std; int main() { int i; cout << "Return which exit status? "; cin >> i; return i; } iostream is a header file—it contains declarations of types, functions, and variables; in particular, the iostream header provides input/ouput functionality to your program. Many other header files exist which provide other features; we’ll get to one other shortly. cin and cout are streams—things that you can write to and read from. More precisely, they are variables declared in iostream!

5 Simple Redirection As you might expect, reading from cin and writing from cout usually operate on your terminal, which is why we’ve been conveniently using them to gather data from the user. ...it doesn’t need to be connected to the terminal, however; for example, you can usually tell your shell to redirect input from a file—cin will then read from that file instead, as if you had typed its contents into your terminal (but without displaying what was sent, of course). You can also usually do this with the output, in which case cout will write to the file you name instead. (Use caution with this one, as the previous contents of the file are lost.) It’s even possible to redirect both at the same time—an excellent tool for automatically testing user interaction :)

6 Working with Arbitrary Files
You’re not just stuck with cin and cout, of course; you can also open arbitrary files for reading and writing from your program, too! For this, we’ll need another header file (fstream) that contains the necessary types. #include <iostream> #include <fstream> using namespace std; int main() { string line; ofstream fout("foo.txt"); cout << "Enter a line of text: "; getline(cin, line); fout << line << endl; cout << "Written to foo.txt!\n"; return 0; } This line actually opens the file; the parameter is the name of the file to open. (Again, use caution; any previous contents are lost.) This line actually writes to the file; note that the operators, syntax, etc., are exactly the same as for cout. In fact, just about anything that can be done with cout can be done to fout, and vice versa!

7 Working with Arbitrary Files
#include <iostream> #include <fstream> using namespace std; int main() { string line; ifstream fin("foo.txt"); getline(fin, line); cout << "File contains: " << line << endl; return 0; } The same can be done with input by using the type ifstream; in this case, the file is opened for reading, and can be used analogously to cin.

8 Deferring Open #include <iostream> #include <fstream>
using namespace std; int main() { string line; ifstream fin; /* ...Important things... */ fin.open("foo.txt"); getline(fin, line); cout << "File contains: " << line << endl; return 0; } If, for some reason (like getting input from the user), it is easier to open the file later, it is very much preferred that you use the .open() method on fstreams. The parameter is a filename, as with the constructor, but the behavior is more reliable than declaring the variable in the middle of your program.

9 Checking for Errors The last program, while not crashing, will behave oddly if the file doesn’t exist—all reads from it will “succeed” but return no data. #include <iostream> #include <fstream> using namespace std; int main() { string line; ifstream fin; /* ...Important things... */ fin.open("foo.txt"); if(fin.fail()) { cout << "No file!\n"; return 1; } getline(fin, line); cout << "File contains: " << line << endl; return 0; Especially when opening a file for reading, it’s a good idea to check whether or not it succeeded; if it didn’t, it’s often (not always) because it doesn’t exist. The .fail() method on streams will indicate this and other errors that may occur, including during actual reading and writing (such as trying to read past the end of the file, bad input formatting, if the disk is full, etc.). Checking this after every operation is a little excessive, but overall not bad practice.


Download ppt "Lab 7: File Input/Output Graham Northup"

Similar presentations


Ads by Google