File operations

PrabhatKumarChaudhar2 1,105 views 17 slides Apr 05, 2020
Slide 1
Slide 1 of 17
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17

About This Presentation

Brief PPT on File Handling Operations which includes opening, closing, writing, reading of the file. The program done in this is worked on DEV C++. but i advice the viewers and the students who wants to take the idea then please do practice your and your own program as an example so that you will un...


Slide Content

DISCOVER . LEARN. EMPOWERFILE OPERATIONS
UNIVERSITY INSTITUTE OF
COMPUTING
Bachelor of Computer Application
OBJECT ORIENTED PROGRAMMING
LANGUAGE
CODE: CAT-153/SCT-155

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.

What is File Operation?
1.File –An abstract data type.
2.Represents –Storage medium fir storing
i) DATA
ii) INFORMATION
3. InFileswe store data i.e. text or binary data
permanently and use these data to read or write in
the form of input outputoperationsby transferring
bytes of data.

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.

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.

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

Programme to create a file
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Create and open a text file
ofstreamMyFile("filename.txt");
// Write to the file
MyFile<< "Files can be tricky, but it is fun enough!";
// Close the file
MyFile.close();
}

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)

Program for writing to a file
#include &lt;iostream&gt;
#include&lt;conio&gt;
#include &lt;fstream&gt;
using namespace std;
int main()
{
fstreamst; // Step 1: Creating object of fstreamclass
st.open(&quot;E:\study.txt&quot;,ios::out); // Step 2:Creating new file
if(!st) // Step 3: Checking whether file exist
{
cout&lt;&lt;&quot;Filecreation failed&quot;;
}
else
{
cout&lt;&lt;&quot;Newfile created&quot;;
st&lt;&lt;&quot;Hello&quot;; // Step 4: Writing to file
st.close(); // Step 5: Closing file
}
getch();
return 0;
}

Reading to a File
#include <bits/stdc++.h>
using namespace std;
// driver code
int main()
{
// filestreamvariable file
fstreamfile;
string word, t, q, filename;
// filename of the file
filename = "file.txt";

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;
}

Deleting a File
Theremove 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;
}

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&lt;&lt; &quot;Valueof pvalue: &quot; &lt;&lt; *pvalue&lt;&lt;
endl;
delete pvalue; // free up the memory.
return 0;
}

Output
If we compile and run above code, this would produce
the following
result –Value of pvalue: 29495

BIBLIOGRAPHY
•https://www.geeksforgeeks.org/file-handling-c-
classes/
•http://www.cplusplus.com/doc/tutorial/files/
•https://www.studytonight.com/cpp/file-streams-in-
cpp.php

THANKYOU

NAME: PRABHAT KUMAR
CHAUDHARY
UID: 19BCA1172