0% found this document useful (0 votes)
20 views

Lab 11 File Handling

The document provides an overview of file handling in programming, detailing how data is stored, accessed, and manipulated using files. It explains the types of files (text and binary), file access methods, and the process of creating, opening, and closing file objects in C++. Additionally, it includes code examples for writing to and reading from files, as well as using loops for processing file data.

Uploaded by

ayyaniqbalmirza
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lab 11 File Handling

The document provides an overview of file handling in programming, detailing how data is stored, accessed, and manipulated using files. It explains the types of files (text and binary), file access methods, and the process of creating, opening, and closing file objects in C++. Additionally, it includes code examples for writing to and reading from files, as well as using loops for processing file data.

Uploaded by

ayyaniqbalmirza
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

File Handling

Concept • Commercial software that you use


• When a program needs to save on a day-to-day basis store data in
data for later use, it writes the files .
data in a file. The data can then 1) Word processors:
be read from the file at a later
time. Word processing programs are used
to write letters, memos, reports,and
• If a program is to retain data other documents. The documents
between the times it runs, it are then saved in files so they can
must have a way of saving it. be edited and printed.
Data is saved in a file, which is Image editors: Images
usually stored on a computer’s Spreadsheets: Num data
disk.
Games: User info
Web browsers: Cookies
Types of Files
• In general, there are two types of files: text and binary
• A text file contains data that has been encoded as text, using a
scheme such as ASCII or Unicode.
• Even if the file contains numbers, those numbers are stored in the file
as a series of characters.
• As a result, the file may be opened and viewed in a text editor such
as Notepad
• A binary file contains data that has not been converted to text. Thus,
you cannot view the contents of a binary file with a text editor
File Access Methods
• There are two general ways to access data stored in a file: sequential
access and direct access
Filenames and File Stream Objects
• Each operating system has its own rules for naming files. Many
systems, including Windows ,support the use of filename extensions,
which are short sequences of characters that apparat the end of a
filename preceded by a period (known as a “dot”).
• For example, the files have the extensions .jpg, .txt, and .doc. The
extension usually indicates the type of data stored in the file.
• In order for a program to work with a file on the computer’s disk, the
program must create a file stream object in memory
• A file stream object is an object that is associated with a specific file and
provides a way for the program to work with that file. It is called a
“stream” object because a file can be thought of as a stream of data
• File stream objects work very much like the cin and cout objects. A
stream of data may be sent to cout, which causes values to be displayed
on the screen.
• A stream of data may be read from the keyboard by cin, and stored in
variables. Likewise, streams of data may be sent to a file stream object,
which writes the data to a file
Setting Up a Program for File
Input/Output
• Just as cin and cout require the iostream file to be included in the
program, C++ file access requires another header file. The file fstream
contains all the declarations necessary for file operations
• It is included with the following statement:
#include <fstream>
Creating a File Object and Opening a
File
• Before data can be written to or • The first statement defines an
read from a file, the following ifstream object named inputFile.
things must happen: The second statement calls the
1) A file stream object must object’s open member function,
be created passing the string
2)The file must be opened "Customers.txt" as an argument
and linked to the file stream object
• The following code shows an
example of opening a file for input
(reading).
ifstream inputFile;

inputFile.open("Customers.txt");
Creating a File Object and Opening a
File
• The following code shows an example of opening a file for output
(writing).
ofstream outputFile;
outputFile.open("Employees.txt");
• The first statement defines an ofstream object named outputFile. The
second statement calls the object’s open member function, passing
the string "Employees.txt" as an argument. In this statement, the
open member function creates the Employees.txt file and links it with
the outputFile object
Creating a File Object and Opening a
File
• Often, when opening a file, you will need to specify its path as well as
its name. For example, on a Windows system the following statement
opens the file C:\data\inventory.txt:
inputFile.open("C:\\data\\inventory.txt")
• In this statement, the file C:\data\inventory.txt is opened and linked
with inputFile
• To define a file stream object and open a file in one statement.
ifstream inputFile("Customers.txt");
ofstream outputFile("Employees.txt");
Closing a File
• The opposite of opening a file is closing it. Although a program’s files
are automatically closed when the program shuts down, it is a good
programming practice to write statements that close them
• Calling the file stream object’s close member function closes a file.
Here is an example:
inputFile.close();
Writing Data to a File
// This program writes data to a file.
#include <iostream>
#include <fstream>
using namespace std; // Close the file
outputFile.close();
int main()
{ cout << "Done.\n";
ofstream outputFile;
outputFile.open("demofile.txt"); return 0;
}
cout << "Now writing data to the file.\n";

// Write four names to the file.


outputFile << "Bach\n"; Program Screen Output:
outputFile << "Beethoven\n"; Now writing data to the file.
outputFile << "Mozart\n";
outputFile << "Schubert\n"; Done.
File Output
writes user input to a file
// This program writes user input to a cin >> number2;
file. cout << "One more time. Enter a
#include <iostream> number: ";
#include <fstream> cin >> number3;
using namespace std;
// Write the numbers to the file.
int main() outputFile << number1 << endl;
{
ofstream outputFile;
outputFile << number2 << endl;
int number1, number2, number3; outputFile << number3 << endl;
cout << "The numbers were saved
// Open an output file. to a file.\n";
outputFile.open("Numbers.txt");
// Close the file
// Get three numbers from the user. outputFile.close();
cout << "Enter a number: "; cout << "Done.\n";
cin >> number1; return 0;
cout << "Enter another number: "; }
Reading Data from a File
// This program reads data from a file.
#include <iostream>
#include <fstream> inputFile >> name; // Read name 3
#include <string> from the file
using namespace std;
cout << name << endl; // Display
int main() name 3
{
ifstream inputFile;
string name; inputFile.close(); // Close the file
inputFile.open("Friends.txt"); return 0;
cout << "Reading data from the file.\n";
}
inputFile >> name; // Read name 1
from the file
cout << name << endl; // Display Program Output:
name 1 Reading data from the file.
inputFile >> name; // Read name 2 Joe
from the file
cout << name << endl; // Display Chris
name 2 Geri
The Read Position

• Keep in mind that when the >> operator extracts data from a file, it
expects to read pieces of data that are separated by whitespace
characters (spaces, tabs, or newlines)
Reading Numeric Data From a
Text
file.
File
// This program reads numbers from a // Calculate the sum of the numbers.
#include <iostream> sum = value1 + value2 + value3;
#include <fstream>
using namespace std; // Display the three numbers.
int main() cout << "Here are the numbers:\n"
{ << value1 << " " << value2
ifstream inFile; << " " << value3 << endl;
int value1, value2, value3, sum;
// Open the file. // Display the sum of the numbers.
inFile.open("NumericData.txt"); cout << "Their sum is: " << sum << endl;
return 0;
// Read the three numbers from the file.
}
inFile >> value1;
inFile >> value2; Program Output
inFile >> value3; Here are the numbers:
10 20 30
// Close the file.
inFile.close(); Their sum is: 60
Using Loops to Process Files
Using Loops to Process Files
// This program reads data from a file. // Close the file.
#include <iostream> inputFile.close();
#include <fstream> return 0;
using namespace std; }

int main()
{
ifstream inputFile;
int number;
// Open the file.
inputFile.open("ListOfNumbers.txt");
// Read the numbers from the file and
// display them.
while (inputFile >> number)
{
cout << number << endl;
}
Lab Task
Good Luck

You might also like