SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Downloaden Sie, um offline zu lesen
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. 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.
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
ofstream MyFile("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()
{
fstream st; // Step 1: Creating object of fstream class
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;File creation failed&quot;;
}
else
{
cout&lt;&lt;&quot;New file 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()
{
// filestream variable file
fstream file;
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
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;
}
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;Value of 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

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

structured programming
structured programmingstructured programming
structured programming
 
File handling-c
File handling-cFile handling-c
File handling-c
 
Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming Fundamentals
 
File management
File management File management
File management
 
File handling in C
File handling in CFile handling in C
File handling in C
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 
File Management in Operating Systems
File Management in Operating SystemsFile Management in Operating Systems
File Management in Operating Systems
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Operating Systems 1 (11/12) - Input / Output
Operating Systems 1 (11/12) - Input / OutputOperating Systems 1 (11/12) - Input / Output
Operating Systems 1 (11/12) - Input / Output
 
Protection and Security in Operating Systems
Protection and Security in Operating SystemsProtection and Security in Operating Systems
Protection and Security in Operating Systems
 
Process management os concept
Process management os conceptProcess management os concept
Process management os concept
 
System call
System callSystem call
System call
 
File operations in c
File operations in cFile operations in c
File operations in c
 
Operating system structures
Operating system structuresOperating system structures
Operating system structures
 
Files
FilesFiles
Files
 
I/O Management
I/O ManagementI/O Management
I/O Management
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
File Management – File Concept, access methods, File types and File Operation
File Management – File Concept, access methods,  File types and File OperationFile Management – File Concept, access methods,  File types and File Operation
File Management – File Concept, access methods, File types and File Operation
 
Operating Systems Basics
Operating Systems BasicsOperating Systems Basics
Operating Systems Basics
 
Operating Systems - File Management
Operating Systems -  File ManagementOperating Systems -  File Management
Operating Systems - File Management
 

Ähnlich wie File operations

pointer, structure ,union and intro to file handling
 pointer, structure ,union and intro to file handling pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 
File management
File managementFile management
File managementsumathiv9
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handlingpinkpreet_kaur
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handlingpinkpreet_kaur
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5YOGESH SINGH
 
Diploma ii cfpc- u-5.2 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.2 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.2 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.2 pointer, structure ,union and intro to file handlingRai University
 
File handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptxFile handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptxarmaansohail9356
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operationsTAlha MAlik
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfsudhakargeruganti
 
File management in C++
File management in C++File management in C++
File management in C++apoorvaverma33
 
Topic - File operation.pptx
Topic - File operation.pptxTopic - File operation.pptx
Topic - File operation.pptxAdnan al-emran
 
File Management and manipulation in C++ Programming
File Management and manipulation in C++ ProgrammingFile Management and manipulation in C++ Programming
File Management and manipulation in C++ ProgrammingChereLemma2
 

Ähnlich wie File operations (20)

File Handling
File HandlingFile Handling
File Handling
 
File Handling
File HandlingFile Handling
File Handling
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
File handling in cpp
File handling in cppFile handling in cpp
File handling in cpp
 
pointer, structure ,union and intro to file handling
 pointer, structure ,union and intro to file handling pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
File management
File managementFile management
File management
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 
data file handling
data file handlingdata file handling
data file handling
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
Diploma ii cfpc- u-5.2 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.2 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.2 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.2 pointer, structure ,union and intro to file handling
 
File handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptxFile handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptx
 
Data file handling
Data file handlingData file handling
Data file handling
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operations
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
 
File management in C++
File management in C++File management in C++
File management in C++
 
Topic - File operation.pptx
Topic - File operation.pptxTopic - File operation.pptx
Topic - File operation.pptx
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
 
File Management and manipulation in C++ Programming
File Management and manipulation in C++ ProgrammingFile Management and manipulation in C++ Programming
File Management and manipulation in C++ Programming
 

Kürzlich hochgeladen

Zone Chairperson Role and Responsibilities New updated.pptx
Zone Chairperson Role and Responsibilities New updated.pptxZone Chairperson Role and Responsibilities New updated.pptx
Zone Chairperson Role and Responsibilities New updated.pptxlionnarsimharajumjf
 
SOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdf
SOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdfSOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdf
SOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdfMahamudul Hasan
 
Uncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac FolorunsoUncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac FolorunsoKayode Fayemi
 
Digital collaboration with Microsoft 365 as extension of Drupal
Digital collaboration with Microsoft 365 as extension of DrupalDigital collaboration with Microsoft 365 as extension of Drupal
Digital collaboration with Microsoft 365 as extension of DrupalFabian de Rijk
 
Introduction to Artificial intelligence.
Introduction to Artificial intelligence.Introduction to Artificial intelligence.
Introduction to Artificial intelligence.thamaeteboho94
 
lONG QUESTION ANSWER PAKISTAN STUDIES10.
lONG QUESTION ANSWER PAKISTAN STUDIES10.lONG QUESTION ANSWER PAKISTAN STUDIES10.
lONG QUESTION ANSWER PAKISTAN STUDIES10.lodhisaajjda
 
Jual obat aborsi Jakarta 085657271886 Cytote pil telat bulan penggugur kandun...
Jual obat aborsi Jakarta 085657271886 Cytote pil telat bulan penggugur kandun...Jual obat aborsi Jakarta 085657271886 Cytote pil telat bulan penggugur kandun...
Jual obat aborsi Jakarta 085657271886 Cytote pil telat bulan penggugur kandun...ZurliaSoop
 
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdfAWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdfSkillCertProExams
 
Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...
Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...
Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...David Celestin
 
Report Writing Webinar Training
Report Writing Webinar TrainingReport Writing Webinar Training
Report Writing Webinar TrainingKylaCullinane
 
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...amilabibi1
 
My Presentation "In Your Hands" by Halle Bailey
My Presentation "In Your Hands" by Halle BaileyMy Presentation "In Your Hands" by Halle Bailey
My Presentation "In Your Hands" by Halle Baileyhlharris
 
Dreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio IIIDreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio IIINhPhngng3
 
Unlocking Exploration: Self-Motivated Agents Thrive on Memory-Driven Curiosity
Unlocking Exploration: Self-Motivated Agents Thrive on Memory-Driven CuriosityUnlocking Exploration: Self-Motivated Agents Thrive on Memory-Driven Curiosity
Unlocking Exploration: Self-Motivated Agents Thrive on Memory-Driven CuriosityHung Le
 
Dreaming Marissa Sánchez Music Video Treatment
Dreaming Marissa Sánchez Music Video TreatmentDreaming Marissa Sánchez Music Video Treatment
Dreaming Marissa Sánchez Music Video Treatmentnswingard
 

Kürzlich hochgeladen (17)

Zone Chairperson Role and Responsibilities New updated.pptx
Zone Chairperson Role and Responsibilities New updated.pptxZone Chairperson Role and Responsibilities New updated.pptx
Zone Chairperson Role and Responsibilities New updated.pptx
 
SOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdf
SOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdfSOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdf
SOLID WASTE MANAGEMENT SYSTEM OF FENI PAURASHAVA, BANGLADESH.pdf
 
Uncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac FolorunsoUncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac Folorunso
 
in kuwait௹+918133066128....) @abortion pills for sale in Kuwait City
in kuwait௹+918133066128....) @abortion pills for sale in Kuwait Cityin kuwait௹+918133066128....) @abortion pills for sale in Kuwait City
in kuwait௹+918133066128....) @abortion pills for sale in Kuwait City
 
Digital collaboration with Microsoft 365 as extension of Drupal
Digital collaboration with Microsoft 365 as extension of DrupalDigital collaboration with Microsoft 365 as extension of Drupal
Digital collaboration with Microsoft 365 as extension of Drupal
 
Introduction to Artificial intelligence.
Introduction to Artificial intelligence.Introduction to Artificial intelligence.
Introduction to Artificial intelligence.
 
lONG QUESTION ANSWER PAKISTAN STUDIES10.
lONG QUESTION ANSWER PAKISTAN STUDIES10.lONG QUESTION ANSWER PAKISTAN STUDIES10.
lONG QUESTION ANSWER PAKISTAN STUDIES10.
 
Jual obat aborsi Jakarta 085657271886 Cytote pil telat bulan penggugur kandun...
Jual obat aborsi Jakarta 085657271886 Cytote pil telat bulan penggugur kandun...Jual obat aborsi Jakarta 085657271886 Cytote pil telat bulan penggugur kandun...
Jual obat aborsi Jakarta 085657271886 Cytote pil telat bulan penggugur kandun...
 
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdfAWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
 
Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...
Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...
Proofreading- Basics to Artificial Intelligence Integration - Presentation:Sl...
 
ICT role in 21st century education and it's challenges.pdf
ICT role in 21st century education and it's challenges.pdfICT role in 21st century education and it's challenges.pdf
ICT role in 21st century education and it's challenges.pdf
 
Report Writing Webinar Training
Report Writing Webinar TrainingReport Writing Webinar Training
Report Writing Webinar Training
 
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...
Bring back lost lover in USA, Canada ,Uk ,Australia ,London Lost Love Spell C...
 
My Presentation "In Your Hands" by Halle Bailey
My Presentation "In Your Hands" by Halle BaileyMy Presentation "In Your Hands" by Halle Bailey
My Presentation "In Your Hands" by Halle Bailey
 
Dreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio IIIDreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio III
 
Unlocking Exploration: Self-Motivated Agents Thrive on Memory-Driven Curiosity
Unlocking Exploration: Self-Motivated Agents Thrive on Memory-Driven CuriosityUnlocking Exploration: Self-Motivated Agents Thrive on Memory-Driven Curiosity
Unlocking Exploration: Self-Motivated Agents Thrive on Memory-Driven Curiosity
 
Dreaming Marissa Sánchez Music Video Treatment
Dreaming Marissa Sánchez Music Video TreatmentDreaming Marissa Sánchez Music Video Treatment
Dreaming Marissa Sánchez Music Video Treatment
 

File operations

  • 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 &lt;iostream&gt; #include&lt;conio&gt; #include &lt;fstream&gt; using namespace std; int main() { fstream st; // Step 1: Creating object of fstream class 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;File creation failed&quot;; } else { cout&lt;&lt;&quot;New file created&quot;; st&lt;&lt;&quot;Hello&quot;; // 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 &lt;&lt; &quot;Value of pvalue : &quot; &lt;&lt; *pvalue &lt;&lt; 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