Upload
easter-sims
View
212
Download
0
Embed Size (px)
Citation preview
© 2000 Scott S Albert
Structured Programming 256Chapter 7
Streams and File I/O
© 2000 Scott S Albert
I/O Overview
• I/O = Input/Output
• In this context it is input to and output from programs
• Input can be from keyboard or a file
• Output can be to display (screen) or a file
• Advantages of file I/O
– permanent copy
– output from one program can be input to another
– input can be automated (rather than entered manually)
© 2000 Scott S Albert
Streams
• Stream: an object that either delivers data to its destination (screen, file, etc.) or that takes data from a source (keyboard, file, etc.)
– it acts as a buffer between the data source and destination
• Input stream: a stream that provides input to a program
• Output stream: a stream that accepts output from a program– cout is an output stream– cin is an input stream
• A stream connects a program to an I/O object– cout connects a program to the screen– cin connects a program to the keyboard
© 2000 Scott S Albert
Binary versus text files• All data and programs are ultimately just zeros and ones
– each digit can have one of two values, hence binary
– bit is one binary digit
– byte is a group of eight bits
• Text files: the bits represent printable characters
– one byte per character for ASCII, the most common code
– for example, C++ source files are text files
– so is any file created with a "text editor"
• Binary files: the bits represent other types of encoded information, such as executable instructions or numeric data
– these files are easily read by the computer but not humans
– they are not "printable" files
• actually, you can print them, but they will be unintelligible
• "printable" means "easily readable by humans when printed"
© 2000 Scott S Albert
Opening a new output file
• The file name is given as a String– file name rules are determined by your operating system
• Opening an output file takes two steps
1. Create a ofstream object associated with the file name String
# include <fstream.h>
2. Use the open method to connect the file name
© 2000 Scott S Albert
Example: opening an output file
To open a file named numbers.dat:
ofstream out_file;
out_file.open(“numbers.dat”);
To write to the file,
Use out_file<<
© 2000 Scott S Albert
Every file has two names
• The code to open the file creates two names for an output file
– the name used by the operating system• numbers.dat in the example
– the stream name• out_file in the example
• C++ programs use the stream name– out_file in the example
© 2000 Scott S Albert
Closing a file
• An Output file should be closed when you are done writing to it
• Use the close method of the class ofstream
• For example, to close the file opened in the previous example:
out_file.close();
• If a program ends normally it will close any files that are open
© 2000 Scott S Albert
If it is done automatically,why explicitly close files?
If a program automatically closes files when it ends normally, why close them with explicit calls to close?
Two reasons:
1. To make sure it is closed if a program ends abnormally (it could get damaged if it is left open).
2. A file open for writing must be closed before it can be opened for reading.
• Although C++ does have a class that opens a file for both reading and writing, we will not discuss this topic
© 2000 Scott S Albert
Warning: overwriting a file
• Opening a file creates an empty file
• Opening a file creates a new file if it does not already exist
• Opening a file that already exists eliminates the old file and creates a new, empty one
– data in the original file is lost
• How to test for the existence of a file and avoid overwriting it will be covered later
© 2000 Scott S Albert
Opening a new input file
• Similar to opening an output file, but replace "output" with "input"
• The file name is given as a String– file name rules are determined by your operating system
• Opening a file takes two steps
1. Creating a ifstream object associated with the file name String
2. Connecting the ifstream to a file name
© 2000 Scott S Albert
Example: opening an input file
To open a file named numbers.dat:
ifstream in_file;
infile.open(“numbers.dat”);
To read from the file,
Use in_file>>
© 2000 Scott S Albert
Input file exceptions
• If (in_file.fail()) cout<<“Error opening file\n”;
in>>x;while(!in.eof()){
cout<<x<<endl;in>>x;
}
Example:reading a file name from the
keyboard
import java.io.*;
public class FileNameDemo{ public static void main(String[] args) throws IOException { System.out.println("Enter file name:"); String fileName = SavitchIn.readLineWord(); DataInputStream inputStream = new DataInputStream(new FileInputStream(fileName));
System.out.println("Reading and summing the nonnegative"); System.out.println("integers in the file " + fileName); int sum = 0; int n = inputStream.readInt(); while (n > 0) { sum = sum + n; System.out.println(n); n = inputStream.readInt(); } System.out.println("End of reading from file."); inputStream.close(); System.out.println("The sum of the numbers is " + sum); }}
FileNameDemo
reading a file name from the keyboard
closing the file
using the file name read from the keyboard
reading data from the file
© 2000 Scott S Albert
Common methodsto test for the end of an input file
• A common programming situation is to read data from an input file but not know how much data the file contains
• In these situations you need to check for the end of the file
• There are two common ways to test for the end of a file:
1. Put a sentinel value at the end of the file and test for it.
2. Test for a special character that signals the end of the file (text files often have such a character).
© 2000 Scott S Albert
Summary• Text files contain strings of printable characters; they look intelligible to
humans when opened in a text editor.
• Binary files contain numbers or data in non-printable codes; they look unintelligible to humans when opened in a text editor.
• C++ can process both binary and text files
• Always check for the end of the file when reading from a file. The way you check for end-of-file depends on the method you use to read from the file.
• A file name can be read from the keyboard into a String variable and the variable used in place of a file name.
• The class fstream has methods to test if a file exists and if it is read- and/or write-enabled.