disk files for i/o your variable (of type ifstream) your variable (of type ofstream) disk file...
TRANSCRIPT
![Page 1: Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649f455503460f94c666e3/html5/thumbnails/1.jpg)
Disk Files for I/O
your variable
(of type ifstream)
your variable
(of type ofstream)
disk file“myInfile.dat”
disk file“myOut.dat”
executingprogram
input data output data
#include <fstream>
1
![Page 2: Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649f455503460f94c666e3/html5/thumbnails/2.jpg)
Disk I/O
To use disk I/O
Access #include <fstream> Choose valid identifiers for your file
streams and declare them Open the files and associate them with
disk names
2
![Page 3: Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649f455503460f94c666e3/html5/thumbnails/3.jpg)
Disk I/O, cont...
Use your file stream identifiers in your I/O statements(using >> and << , manipulators)
Close the files
3
![Page 4: Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649f455503460f94c666e3/html5/thumbnails/4.jpg)
Disk I/O Statements
#include <fstream>
ifstream myInfile; // Declarations
ofstream myOutfile;
myInfile.open(“myIn.dat”); // Open files
myOutfile.open(“myOut.dat”);
myInfile.close(); // Close files
myOutfile.close();
4
![Page 5: Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649f455503460f94c666e3/html5/thumbnails/5.jpg)
Opening a File
Opening a file Associates the C++ identifier for your file
with the physical(disk) name for the file– If the input file does not exist on disk,
open is not successful– If the output file does not exist on
disk, a new file with that name is created
– If the output file already exists, it is erased
5
![Page 6: Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649f455503460f94c666e3/html5/thumbnails/6.jpg)
Opening a File
Opening a file Places a file reading marker at the very
beginning of the file, pointing to the first character in the file
6
![Page 7: Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649f455503460f94c666e3/html5/thumbnails/7.jpg)
Stream Fail State
When a stream enters the fail state, Further I/O operations using that stream
have no effect at all The computer does not automatically
halt the program or give any error message
7
![Page 8: Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649f455503460f94c666e3/html5/thumbnails/8.jpg)
Stream Fail State
Possible reasons for entering fail state include: Invalid input data (often the wrong type) Opening an input file that doesn’t exist Opening an output file on a disk that is
already full or is write-protected
8
![Page 9: Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649f455503460f94c666e3/html5/thumbnails/9.jpg)
Run Time File Name Entry
#include <string>// Contains conversion function c_str
ifstream inFile;string fileName;
cout << “Enter input file name: “ << endl; // Prompt
cin >> fileName;
// Convert string fileName to a C string typeinFile.open(fileName.c_str());
9
![Page 10: Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649f455503460f94c666e3/html5/thumbnails/10.jpg)
10
Names in Multiple FormatsProblem
You are beginning to work on a problem that needs to output names in several formats along with the corresponding social security number.
As a start, you decide to write a short C++ program that inputs a social security number and a single name and displays it in the different formats, so you can be certain that all of your string expressions are correct.
![Page 11: Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649f455503460f94c666e3/html5/thumbnails/11.jpg)
11
Algorithm
Main Module Level 0
Open files
Get social security number
Get name
Write data in proper formats
Close files
Open Files Level 1
inData.open("name.dat")
outData.open("name.out")
![Page 12: Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649f455503460f94c666e3/html5/thumbnails/12.jpg)
12
Get Name
Get first nameGet middle name or initialGet last name
![Page 13: Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649f455503460f94c666e3/html5/thumbnails/13.jpg)
13
Write Data in Proper FormatsWrite first name, blank, middle name, blank, last name, blank, social security numberWrite last name, comma, first name, blank, middle name, blank, social security numberWrite last name, comma, blank, first name, blank, middle initial, period, blank, social security numberWrite first name, blank, middle initial, period, blank, last name
![Page 14: Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649f455503460f94c666e3/html5/thumbnails/14.jpg)
14
Middle initial Level 2
Set initial to middleName.substr(0, 1) + period
Close files
inData.close()
outData.close()
![Page 15: Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649f455503460f94c666e3/html5/thumbnails/15.jpg)
15
C++ Program//*************************************************************// Format Names program// This program reads in a social security number, a first name// a middle name or initial, and a last name from file inData. // The name is written to file outData in three formats: // 1. First name, middle name, last name, and social security
// number.// 2. last name, first name, middle name, and social // security number// 3. last name, first name, middle initial, and social// security number// 4. First name, middle initial, last name//*************************************************************
![Page 16: Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649f455503460f94c666e3/html5/thumbnails/16.jpg)
16
#include <fstream> // Access ofstream
#include <string> // Access stringusing namespace std;
int main(){ // Declare and open files ifstream inData; ofstream outData; inData.open("name.dat"); outData.open("name.out");
![Page 17: Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649f455503460f94c666e3/html5/thumbnails/17.jpg)
17
// Declare variables string socialNum; // Social security number
string firstName; // First name string lastName; // Last name
string middleName; // Middle name string initial; // Middle initial
![Page 18: Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649f455503460f94c666e3/html5/thumbnails/18.jpg)
18
// Read in data from file inData inData >> socialNum >> firstName >> middleName
>> lastName; // Access middle initial and append a period
initial = middleName.substr(0, 1) + '.';
![Page 19: Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649f455503460f94c666e3/html5/thumbnails/19.jpg)
19
// Output information in required formats outData << firstName << ' ' << middleName << ' '
<< lastName << ' ' << socialNum << endl;
outData << lastName << ", " << firstName << ' '
<< middleName << ' ' << socialNum << endl;
outData << lastName << ", " << firstName << ' '
<< initial << ' ' << socialNum << endl;
outData << firstName << ' ' << initial << ' '
<< lastName;
![Page 20: Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input](https://reader035.vdocuments.net/reader035/viewer/2022071705/56649f455503460f94c666e3/html5/thumbnails/20.jpg)
20
// Close files inData.close(); outData.close(); return 0;
}