Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Programming Fundamentals

Graded Lab
Lab 10

Lab Engineer: Ahmad Abduhu

Topic: File Handling


Session: Fall 2018

School of Systems & Technology


UMT Lahore Pakistan
Contents
File Handling................................................................................................................................................3
Lab Objectives:........................................................................................................................................3
Instructions:.............................................................................................................................................3
File Handling in C++.....................................................................................................................................4
Why use File Handling in C++...................................................................................................................4
How to achieve File Handling..................................................................................................................5
Functions use in File Handling.................................................................................................................5
Defining and Opening a File.....................................................................................................................5
Syntax..................................................................................................................................................5
Example...............................................................................................................................................5
Closing a File............................................................................................................................................6
Syntax and Example.............................................................................................................................6
File Opening mode..................................................................................................................................6
Example...............................................................................................................................................6
Sample Lab Tasks:....................................................................................................................................7
Writing and reading an integer, character or a string to and from a file:...........................................7
Writing a Line to a file:.........................................................................................................................7
Reading a Line from a file:...................................................................................................................8
Lab Task.......................................................................................................................................................9
Task 1:.....................................................................................................................................................9
Task 2:.....................................................................................................................................................9
Task 4:.....................................................................................................................................................9
Task 3 (Bonus Task):................................................................................................................................9
File Handling
Lab Objectives:
 Understand the concept of File Handling.
 Implementation of Input/output with files.

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

Why use File Handling in C++


 For permanet storage.
 The transfer of input - data or output - data from one computer to another can be
easily done by using files.
For read and write from a file you need another standard C++ library called  fstream, which
defines three new data types:

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

For achieving file handling in C++ we need follow following steps

 Naming a file

 Opening a file

 Reading data from file

 Writing data into file

 Closing a file

Functions use in File Handling

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.

Defining and Opening a 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

ofstream outfile; // create stream


outfile . open ("data1"); // connect stream to data1
Closing a File
A file must be close after completion of all operation related to file. For closing file we

need close() function.

Syntax and Example

outfile.close();

File Opening mode

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:

Writing and reading an integer, character or a string to and from a file:

#include <iostream>
#include <fstream>

using namespace std;

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 a Line to a file:

Writing operations on text files are performed in the same way as we operated with cout:

// writing on a text file


#include <iostream>
#include <fstream>
using namespace std;

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 a Line from a file:

Reading from a file can also be performed in the same way that we did with cin:

// reading a text file


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}

else cout << "Unable to open file";

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

You might also like