This document provides an overview of file operations in C++, including opening, reading from, writing to, and deleting files. It discusses header files like <fstream> used for file input/output and stream classes like ifstream and ofstream. Key file operations covered are opening/creating files, writing data using write(), reading data with read(), and deleting files with remove(). Code examples are provided for each operation to demonstrate how to implement file handling in C++ programs.
1. DISCOVER . LEARN . EMPOWERFILE OPERATIONS
UNIVERSITY INSTITUTE OF
COMPUTING
Bachelor of Computer Application
OBJECT ORIENTED PROGRAMMING
LANGUAGE
CODE: CAT-153/SCT-155
2. Content
a) What is File Operation?
b) Header Files in File Operation.
c) Operations in File Handling.
d) Creating/Opening Files.
e) Writing a File.
f) Reading to a File.
g) Deleting a File.
h) Bibliography.
3. What is File Operation?
1. File – An abstract data type.
2. Represents – Storage medium fir storing
i) DATA
ii) INFORMATION
3. In Files we store data i.e. text or binary data
permanently and use these data to read or write in
the form of input output operations by transferring
bytes of data.
4. Header Files in File Operation
The header files used in file operations is <fstream>.
•ofstream: It represents output Stream and this is
used for writing in files.
•ifstream: It represents input Stream and this is used
for reading from files.
•fstream: It represents both output Stream and input
Stream. So it can read from files and write to files.
5. Operations in File Handling
•Creating a file: open()
•Reading data: read()
•Writing new data: write()
•Closing a file: close()
Let us study the above mentioned operations in file handling in details.
6. Creating/Opening File
A file by specifying new path of the file and mode of
operation.
Syntax:
FilePointer.open("Path",ios::mode);
•Example of file opened for
writing: st.open("E:studytonight.txt",ios::out);
•Example of file opened for
reading: st.open("E:studytonight.txt",ios::in);
7. Programme to create a file
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Create and open a text file
ofstream MyFile("filename.txt");
// Write to the file
MyFile << "Files can be tricky, but it is fun enough!";
// Close the file
MyFile.close();
}
8. Writing to a File
It is used to write in a file that has been created.
Example:
MyFile << "Files can be tricky, but it is fun enough!";
(this helps in writing into the file)
9. Program for writing to a file
#include <iostream>
#include<conio>
#include <fstream>
using namespace std;
int main()
{
fstream st; // Step 1: Creating object of fstream class
st.open("E:study.txt",ios::out); // Step 2:Creating new file
if(!st) // Step 3: Checking whether file exist
{
cout<<"File creation failed";
}
else
{
cout<<"New file created";
st<<"Hello"; // Step 4: Writing to file
st.close(); // Step 5: Closing file
}
getch();
return 0;
}
10. Reading to a File
#include <bits/stdc++.h>
using namespace std;
// driver code
int main()
{
// filestream variable file
fstream file;
string word, t, q, filename;
// filename of the file
filename = "file.txt";
11. Reading to a File(Cont.)
// opening file
file.open(filename.c_str());
// extracting words from the file
while (file >> word)
{
// displaying content
cout << word << endl;
}
return 0;
}
12. Deleting a File
The remove function in C/C++ can be used to delete a file. The
function returns 0 if files is deleted successfully, other returns
non zero value.
#include<stdio.h>
int main()
{
if (remove("abc.txt") == 0)
cout<<"Deleted successfully”;
else
cout<<"Unable to delete the file”;
return 0;
}
13. Program for new and delete
#include <iostream>
using namespace std;
int main () {
double* pvalue = NULL; // Pointer initialized with null
pvalue = new double; // Request memory for the variable
*pvalue = 29494.99; // Store value at allocated address
cout << "Value of pvalue : " << *pvalue <<
endl;
delete pvalue; // free up the memory.
return 0;
}
14. Output
If we compile and run above code, this would produce
the following
result –Value of pvalue : 29495