LAB11OOP
LAB11OOP
LAB11OOP
Analysis
of data
in Lab Modern Ethics Individua
Report Tool and l and
Viva / Usage Safety Teamwor
Quiz / Lab k
Name Reg. No Performanc
e
1
2
EXPERIMENT NO 11
Objectives:
To learn about handling file in C++, reading and writing to
a file.
Equipment required:
Visual Studio/ Dev C+
+
Description:
When a program needs to save data for later use, it writes the data in a
file. The data can then be read from the file at a later time.
If a program is to retain data between the times it runs, it must have a
way of saving it. Data is saved in a file, which is usually stored on a
computer’s disk.
Commercial software that you use on a day-to-day basis store data in files .
1)Word processors:
Word processing programs are used to write letters, memos, documents.
reports,and other The documents are then saved in files so
they can be edited and printed.
Image editors: Images
Spreadsheets: Num data
Games: User info
Web browsers: Cookies
Type of file:
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
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>
Before data can be written to or read from a file, the following things must
happen:
6
7
8
The following code shows an example of opening a file for input (reading).
ifstream inputFile;
inputFile.open("Customers.txt"
);
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.
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:
inputFile.close();
9
10
Lab Tasks:
CODE:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
class FileWriter {
private:
public:
lines.push_back(line);
std::ofstream outFile(fileName);
if (outFile.is_open()) {
outFile.close();
11
} else {
std::cerr << "Failed to open file: " << fileName << std::endl;
};
int main() {
FileWriter fileWriter("output_file.txt");
fileWriter.writeToFile();
return 0;
OUTPUT:
12
2. Open a file and write user input numbers in the file.
Code:
#include <iostream>
#include <fstream>
int main() {
if (!outFile) {
cout << "Error: Could not open the file." << endl;
return 1;
int numInputs;
cout << "How many numbers do you want to write to the file? ";
cout << "Enter " << numInputs << " numbers:" << endl;
int number;
13
outFile << number << endl; // Write the number to the file
cout << "Numbers successfully written to " << labtask02.txt << "!" << endl;
return 0;
Output:
3. Open a file and Read Data from the file, display the contents on the console.
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
14
int main() {
string fileName = "labtask02.txt"; // File name to read from
ifstream inFile(fileName); // Create an input file stream
if (!inFile) {
cerr << "Error: Could not open the file." << endl;
return 1;
}
cout << "Contents of the file '" << fileName << "':" << endl;
string line;
while (getline(inFile, line)) { // Read each line from the file
cout << line << endl; // Display the line on the console
}
Output:
4. Open a file Read numeric data from the file and display the largest number in those written
numbers
Code:
#include <iostream>
#include <fstream>
#include <limits>
using namespace std;
int main() {
string fileName = "labtask02.txt"; // Updated file name
ifstream inFile(fileName); // Create an input file stream
15
return 1;
}
int number;
int largest = numeric_limits<int>::min(); // Initialize to the smallest integer
5. Use while loop to read data from file and display the contents.
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string fileName = "labtask02.txt"; // Name of the file to read
ifstream inFile(fileName); // Open the file for reading
16
string line;
cout << "Reading data from the file..." << endl;
Output:
17