ITPPA1 File Structures
ITPPA1 File Structures
File Structures
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 1
product or service or otherwise on a password-protected website for classroom
Objectives (1 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 2
product or service or otherwise on a password-protected website for classroom use.
File Structure
• A file is a type of structure for data. It writes the data into a file
when a program needs to save data for later use. At a later
time, the data can be read from the file
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 3
product or service or otherwise on a password-protected website for classroom use.
File Structure
• When a file is used by a program, there are always three steps that must be
taken:
1. Open the file: Opening a file establishes a connection between the program
and the file. Typically, opening an output file generates a file on the disk and
enables the software to write data to it. Opening an input file enables the
software to read the file 's data.
2. File processing: Data is either written to a file (if it is an output file) or read
from a file (if it is an input file) in this stage.
3. Close the file: The file must be closed when the program is finished using the
file. Closing a file disconnects the program from the file.
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 4
product or service or otherwise on a password-protected website for classroom use.
File Types
Basically 2 types
• A text file includes information that has been encoded using a scheme like ASCII
or Unicode as text.
• There is data in a binary file that has not been translated to text. As a result, the
contents of a binary file with a text editor cannot be displayed.
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 5
product or service or otherwise on a password-protected website for classroom use.
File Access Methods
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 6
product or service or otherwise on a password-protected website for classroom use.
File creating and writing data to file
#include <fstream>
Then, you can declare the ofstream object in the function where
you want to open a file and write data to it.
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 7
product or service or otherwise on a password-protected website for classroom use.
File creating and writing data to file
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 8
product or service or otherwise on a password-protected website for classroom use.
File creating and writing data to file
//Example Declaration
ofstream outputFile;
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 9
product or service or otherwise on a password-protected website for classroom use.
File creating and writing data to file
Opening a File
Example:
outputFile.open("StudentData.txt");
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 10
product or service or otherwise on a password-protected website for classroom use.
File creating and writing data to file
Opening a File
After you have generated the ofstream object and opened a file,
you can use the stream insertion operator (< <) to write data
to the file.
We already know how to view data on the computer using the <
< operator with cout. It is used the same way to write data to a
file with an ofstream object.
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 11
product or service or otherwise on a password-protected website for classroom use.
File creating and writing data to file
Closing a File
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 12
product or service or otherwise on a password-protected website for classroom use.
File creating and writing data to file
•#include <fstream>
•int main() {
• myFile << "John Locke" << endl; // Write the name of a philosopher to the file.
• return 0; }
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 13
product or service or otherwise on a password-protected website for classroom use.
Opening and reading data from an input file
To read data from an input file, you first declare an internal name that you will use to
reference the file
U will also need the fstream directive, same as with writing data to an output file
Example:
ifstream inputFile;
An ifstream object called inputFile is declared by this argument. The ifstream indicates that
we can read the file's details. InputFile is the internal name we're going to use to deal with
the output file. Next, to open a file, you use the ifstream object.
Example:
inputFile.open("numbers.txt");
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 14
product or service or otherwise on a password-protected website for classroom use.
Opening and reading data from an input file
If you've created an ifstream object and opened a file, you can use the stream extraction
operator (>>) to read a data item from the file.
You already know how to read input from the keyboard by using the > > operator with cin.
Similarly, the ifstream object is used to read data from a file.
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 15
product or service or otherwise on a password-protected website for classroom use.
Opening and reading data from an input file
I#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int counter, number;
ifstream myFile; //declaring ifstream object
myFile.open("numbers.txt"); //open file for reading
//loop 10 numbers from file
for (counter = 1; counter <= MAX_NUMS; counter++) {
myFile >> number; //read a number from the file
cout << number << endl; //Display the number }
myFile.close(); //close the file
return 0;
} © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 16
product or service or otherwise on a password-protected website for classroom use.
Getline Function
The following statement shows how you can use the getline
function to read a line of input from the file, storing that line of
input in the string variable line:
getline(inputFile, line);
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 17
product or service or otherwise on a password-protected website for classroom use.
Getline Function
Int main() {
ifstream myFile;
myFile.open("philosophers.txt");
getline(myFile, name1); // Read the names from the file into the variables.
getline(myFile, name2);
cout << "Here are the names:" << endl; // Display the names that were read.
return 0;
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 18
product or service or otherwise on a password-protected website for classroom use.
Delimiters and EOF Marker
Continue
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 19
product or service or otherwise on a password-protected website for classroom use.