Programming Fundamentals Lab 10 (File Handling) New Batch
Programming Fundamentals Lab 10 (File Handling) New Batch
Programming Fundamentals Lab 10 (File Handling) New Batch
Graded Lab
Lab 10
Instructions:
Indent your code
Comment your code
Use meaningful variable and function names
Plan your code carefully on a piece of paper before you implement it.
Name of the program should be same as the task name. i.e. the first program should be
Task_4_01.cpp
Upload a zip file on Moodle, zip file should contain all the tasks you have implemented.
Name of zip file should be your UMT-ID e.g. 12003065123.zip
File Handling in C++
File Handling concept in C++ language is used for store a data permanently in
computer. Using file handling we can store our data in Secondary memory (Hard
disk).
Datatype Description
ofstream This is used to create a file and write data on files
ifstream This is used to read data from files
fstream This is used to both read and write data from/to
files
How to achieve File Handling
Naming a file
Opening a file
Closing a file
Function Operation
open() To create a file
close() To close an existing file
get() Read a single character from a file
put() write a single character in file.
read() Read data from file
write() Write data into file.
The function open() can be used to open multiple files that use the same stream object.
Syntax
file-stream-class stream-object;
stream-object.open("filename");
Example
need close() function.
outfile.close();
Meaning Purpose
Mode
ios :: out Write Open the file for write only.
ios :: in read Open the file for read only.
ios :: app Appending Open the file for appending data to
end-of-file.
ios :: ate Appending take us to the end of the file when it
is opened.
Both ios :: app and ios :: ate take us to the end of the file when it is opened. The difference
between the two parameters is that the ios :: app allows us to add data to the end of file only,
while ios :: ate mode permits us to add data or to modify the existing data any where in the file.
The mode can combine two or more parameters using the bitwise OR operator (symbol |)
Example
fstream file;
file.Open("data . txt", ios :: out | ios :: in);
Sample Lab Tasks:
#include <iostream>
#include <fstream>
int main()
{
ofstream fout;
fout.open("file.txt");
int ID = 100;
string name = "Ahmad";
int age = 36;
fout<<ID<<" ";
fout<<name<<" ";
fout<<age<<" ";
fout.close();
ifstream fin;
fin.open("file.txt");
fin>>ID;
fin>>name;
fin>>age;
cout<<ID<<" ";
cout<<name<<" ";
cout<<age<<" ";
}
Writing operations on text files are performed in the same way as we operated with cout:
int main () {
ofstream myfile ("example.txt");
if (myfile.is_open())
{
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Reading from a file can also be performed in the same way that we did with cin:
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}
return 0;
}
Lab Tasks
Task 1:
Write a program to declare an array of user defined size, initialize it with random numbers and write the
elements of array to a file “myArray.txt”.
Task 2:
Write a program to read ten numbers from a file, save them to an array of same size and then print all
numbers on the console screen.
Task 4:
Write a program which reads a disk file “MyCV.txt” (created by yourself previously) line by line and print
it on the console screen.
MyCV.txt is a simple format notepad file containing your profile information.
Task 3:
Write a program, which ask the user:
To enter Name, ID, Grade, Result in Percentage, Age and Address of five students in respective
same sized array (you may hard code data for array).
Save all this data to a file.
Read back from file.
Display on console screen in proper tabular form (using iomanip.h ).