0% found this document useful (0 votes)
9 views19 pages

ITPPA1 File Structures

Coding

Uploaded by

sizwemakgalemele
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views19 pages

ITPPA1 File Structures

Coding

Uploaded by

sizwemakgalemele
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Week 6

File Structures

C++ Programming: Program Design Including Data Structures, Eighth Edition

© 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)

• In this week, you will:


• Learn about File Structures
• Learn about File Types
• Learn about File creating and writing data to a file
• Learn about File opening and reading data to a file
• Learn about File Delimiters
• Learn about File Appening

© 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

• Different Types of files, Word Processors, Spreadsheets etc

© 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

There are two ways to access data in a file, Sequential access


and direct access.

• The Sequential Access File accesses information from the start


of the file to the end of the file. You must read all the data that
comes before it if you want to read a piece of data that is stored
at the very end of the file; you cannot skip directly to the
desired data.
• Direct / random access file: You can jump directly to any piece
of data in the file without reading the data that comes before 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 6
product or service or otherwise on a password-protected website for classroom use.
File creating and writing data to file

To work with files in a C++ program, you must include the


following directive:

#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

Declaring an ofstream object

To write a program that performs an operation on a file, there are


2 things you need:

1. The filename that defines the file on a computer disk drive


2. An internal name identical to the name of a variable

© 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

Declaring an ofstream object

//Example Declaration

ofstream outputFile;

//This statement declares an ofstream object named outputFile.


This statement declares two things:

1. The word ofstream indicates the mode in which we will use


the file. In this case, ofstream indicates that we will be writing
data to the file.
2. The name outputFile is the internal name that we will use to
work with the output file in our code.

© 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

Next, you use the ofstream object to open a file.

Example:

outputFile.open("StudentData.txt");

After this statement is executed, we will be able to write data to


the StudentData.txt file using the ofstream object that is called
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 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.

Assuming, for instance, that the outputFile is an ofstream object,


the following statement writes the string “TestData" to the file:

outputFile << “TestData" << endl;

© 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

• Closing a file disconnects the file from the program. Failure to


close an output file can cause data loss in some systems.
• This occurs because the information that is written to a file is
first written to a buffer, which is a temporary storage area
which is limited in size.
• When the buffer is complete, the operating system of the
machine writes the contents of the buffer into the register.
• This technique improves the efficiency of the machine, since
it is quicker to write data to memory than to write it to a disk.
• Any unsaved data that remains in the buffer is required to
be written to the file by the act of closing an output 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

Example of Creating ofstream object, opening and


closing a File
•#include <iostream>

•#include <fstream>

•using namespace std;

•int main() {

• ofstream myFile; // Declare an ofstream object named myFile

• myFile.open("philosophers.dat"); // Open file named philosphers.txt.

• myFile << "John Locke" << endl; // Write the name of a philosopher to the file.

• 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 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

To read a file which contains strings with spaces, the getline


function is used.

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() {

string name1, name2;

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.

cout << name1 << endl;

cout << name2 << endl;

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 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.

You might also like