File1. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Introduction to File Input and Output
3.14
2. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Introduction to File Input and
Output
• The programs you have written so far
require you to re-enter data each time the
program runs.
• This is because the data stored in RAM
disappears once the program stops
running or the computer is shut down.
• If a program is to retain data between the
times it runs, it must have a way of saving
it.
3-2
3. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Introduction to File Input and
Output
• Data is saved in a file, which is usually
stored on a computer’s disk.
• Once the data is saved in a file, it will
remain there after the program stops
running.
• The data can then be retrieved and used at
a later time.
3-3
4. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Introduction to File Input and
Output
• There are always three steps that must be
taken when a file is used by a program:
1. The file must be opened. If the file does not
yet exist, opening it means creating it.
2. Data is then saved to the file, read from the
file, or both. (use file for read/write/ both)
3. When the program is finished using the file,
the file must be closed.
3-5
5. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-6
Files: What is Needed
• Use fstream header file for file access
• File stream types:
ifstream for input from a file
ofstream for output to a file
fstream for input from or output to a file
• Define file stream objects:
ifstream infile;
ofstream outfile;
6. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-7
Opening Files
• Create a link between file name (outside the program)
and file stream object (inside the program)
• Use the open member function:
infile.open("inventory.dat");
outfile.open("report.txt");
• Filename may include drive, path info.
• Output file will be created if necessary; existing file
will be erased first
• Input file must exist for open to work
7. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Opening files
• Often, when opening a file, you will need to
specify its location as well as its name.
• For example, on a Windows system the
following statement opens the file
C:datainventory.dat:
– outputFile.open("C:datainventory.dat")
• In this statement, the file C:datainventory.dat is
opened and linked with outputFile.
3-8
8. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-9
Using Files
• Can use output file object and << to send
data to a file:
outfile << "Inventory report";
• Can use input file object and >> to copy
data from file to variables:
infile >> partNum;
infile >> qtyInStock >> qtyOnOrder;
9. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-10
Closing Files
• Use the close member function:
infile.close();
outfile.close();
• Don’t wait for operating system to close
files at program end
– Closing a file causes any unsaved data that
may still be held in a buffer to be saved to its
file.
– Some operating systems limit the number of
files that may be open at one time.
10. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-11
Continued…
11. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-12
12. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-13
Continued…
13. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-14
14. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Testing for File Open Errors
4.16
15. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-16
Testing for File Open Errors
• Can test a file stream object to detect if an
open operation failed:
infile.open("test.txt");
if (!infile)
{
cout << "File open failure!";
}
• Can also use the fail member function
16. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Fail member function
ifstream inputFile;
inputFile.open("customers.txt");
if (inputFile.fail())
{
cout << "Error opening file.n";
}
The fail member function returns true when an
attempted file operation is unsuccessful.
4-17
17. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Using a Loop to Read Data from a File
5.9
18. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-19
Using a Loop to Read
Data from a File
• The stream extraction operator >> returns
true when a value was successfully read,
false otherwise
• Can be tested in a while loop to continue
execution as long as values are read from
the file:
while (inputFile >> number) ...
19. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-20
20. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-21
21. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-22
22. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-23