SlideShare a Scribd company logo
File Handling
IN C++
What Is File?
A file is a collection on information, usually
stored on a computer's disk. Information
can be saved to files and then later reused.
The following classes in C++
have access to file input and
output functions:
 fstream
 Ifstream
 ofstream
File_handling in c++ and its use cases.pptx
DIFFERENT FILE OPERATIONS
 OPENING A FILE
 CLOSING A FILE
 READING FROM A FILE
 WRITING ON A FILE
 CHECKING FOR END OF FILE
OPENING A FILE
1.By using the CONSTRUCTOR of the stream class.
ifstream transaction(“xyz.txt");
ofstream result("result.02");
2.By using the open() function of the stream class
ifstream transaction;
transaction.open(“xyz.txt");
Checking For Successful File Opening
ifstream transaction("sale.txt");
if (transcation == NULL)
{
cout<<"unable to open sales.txt";
cin.get(); //waits for the operator to press any key
exit(0);
}
Reading from a file
// Print each character read from the file
char ch;
while (file.get(ch))
{
cout << ch ;
}
WRITE TO A FILE
// Method 1: Using the << operator
outputFile << "Hello, world!" << endl;
outputFile << "This is a new line." << endl;
// Method 2: Using the write() function const char* message =
"Another way to write to a file.";
outputFile.write(message, strlen(message));
Closing of File
Stream name.close();
e.g., transaction.close();
File Mode Parameters
MODES MEANINGS
 ios::app Append to end-of file
 ios::ate Goto end of file on opening
 ios::binary Binary file
 ios::in Open existing file for reading
 ios::nocreate Open fails if file doesn't exist
 ios::noreplace Open fails if file already exists
 ios::out Open existing file for writing
 ios::trunc Deletes contents if it exists
The mode can combine two or more modes using bit wise or (l)
Types of Files
 The two basic types of files are
1.Text File
&
2.Binary File
Text File Binary File
 When using a text file, we
write out separately each of
the pieces of data about a
given record.
 The text file will be readable
by an editor.
 for the text file we will use
the usual output
operator(<<) and will output
each of the pieces of the
record separately.
 with the text file we will read
each of the pieces of record
from the file separately,
using the usual input
operator(>>)
 When using a Binary file we
write whole record data to
the file at once.
 But the numbers in the
binary file will not be
readable in this way.
 For the binary file we will use
write to write to the file.
 With the binary file we will
use the read function to read
a whole record.
File_handling in c++ and its use cases.pptx

More Related Content

PPTX
File Handling in c++
PDF
10301032-230705131251-299420ab.pdfile handling
PPTX
Data file handling in c++
PDF
Filepointers1 1215104829397318-9
PPTX
File management in C++
PPT
Filehandlinging cp2
PPTX
Unit iv
DOCX
Filehandling
File Handling in c++
10301032-230705131251-299420ab.pdfile handling
Data file handling in c++
Filepointers1 1215104829397318-9
File management in C++
Filehandlinging cp2
Unit iv
Filehandling

Similar to File_handling in c++ and its use cases.pptx (20)

PDF
Chapter28 data-file-handling
PPTX
Chapter4.pptx
PDF
Data file handling
PPTX
File Handling
PPT
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
PPT
File Handling In C++(OOPs))
PPTX
File handling.pptx
PPTX
Data file operations in C++ Base
PPTX
Filesin c++
PPT
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
PPTX
File handling in C by Faixan
PPTX
Cs1123 10 file operations
PPSX
Files in c++
PDF
Filesinc 130512002619-phpapp01
PPTX
File Handling
PPT
File handling in_c
PPTX
pointer, structure ,union and intro to file handling
PPT
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
Chapter28 data-file-handling
Chapter4.pptx
Data file handling
File Handling
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
File Handling In C++(OOPs))
File handling.pptx
Data file operations in C++ Base
Filesin c++
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
File handling in C by Faixan
Cs1123 10 file operations
Files in c++
Filesinc 130512002619-phpapp01
File Handling
File handling in_c
pointer, structure ,union and intro to file handling
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
Ad

More from ZenLooper (12)

PPTX
Lecture 09 Exception Handling(1 ) in c++.pptx
PPTX
set identities and their examples outlined.pptx
PPTX
mathematical induction and stuff Induction.pptx
PPTX
sets and their introduction and their exercises.pptx
PDF
Managing External Environment and Organizational Culture.pdf
PDF
Planning Work Activities and their benefits.pdf
PDF
Managers as Decision Makersstoiiiiii.pdf
PPTX
rules of inference in discrete structures
PPTX
Arguments in discreate structures and stuff
PPTX
Laws of Logic in Discrete Structures and their applications
PPTX
Discreate Truth tables and laws of logic
PPTX
discrete structures and their introduction
Lecture 09 Exception Handling(1 ) in c++.pptx
set identities and their examples outlined.pptx
mathematical induction and stuff Induction.pptx
sets and their introduction and their exercises.pptx
Managing External Environment and Organizational Culture.pdf
Planning Work Activities and their benefits.pdf
Managers as Decision Makersstoiiiiii.pdf
rules of inference in discrete structures
Arguments in discreate structures and stuff
Laws of Logic in Discrete Structures and their applications
Discreate Truth tables and laws of logic
discrete structures and their introduction
Ad

Recently uploaded (20)

PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Computing-Curriculum for Schools in Ghana
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Pre independence Education in Inndia.pdf
PDF
Classroom Observation Tools for Teachers
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPH.pptx obstetrics and gynecology in nursing
Sports Quiz easy sports quiz sports quiz
Insiders guide to clinical Medicine.pdf
Cell Structure & Organelles in detailed.
STATICS OF THE RIGID BODIES Hibbelers.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Cell Types and Its function , kingdom of life
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Basic Mud Logging Guide for educational purpose
Microbial diseases, their pathogenesis and prophylaxis
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Complications of Minimal Access Surgery at WLH
Computing-Curriculum for Schools in Ghana
102 student loan defaulters named and shamed – Is someone you know on the list?
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Pre independence Education in Inndia.pdf
Classroom Observation Tools for Teachers
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
human mycosis Human fungal infections are called human mycosis..pptx

File_handling in c++ and its use cases.pptx

  • 2. What Is File? A file is a collection on information, usually stored on a computer's disk. Information can be saved to files and then later reused.
  • 3. The following classes in C++ have access to file input and output functions:  fstream  Ifstream  ofstream
  • 5. DIFFERENT FILE OPERATIONS  OPENING A FILE  CLOSING A FILE  READING FROM A FILE  WRITING ON A FILE  CHECKING FOR END OF FILE
  • 6. OPENING A FILE 1.By using the CONSTRUCTOR of the stream class. ifstream transaction(“xyz.txt"); ofstream result("result.02"); 2.By using the open() function of the stream class ifstream transaction; transaction.open(“xyz.txt");
  • 7. Checking For Successful File Opening ifstream transaction("sale.txt"); if (transcation == NULL) { cout<<"unable to open sales.txt"; cin.get(); //waits for the operator to press any key exit(0); }
  • 8. Reading from a file // Print each character read from the file char ch; while (file.get(ch)) { cout << ch ; }
  • 9. WRITE TO A FILE // Method 1: Using the << operator outputFile << "Hello, world!" << endl; outputFile << "This is a new line." << endl; // Method 2: Using the write() function const char* message = "Another way to write to a file."; outputFile.write(message, strlen(message));
  • 10. Closing of File Stream name.close(); e.g., transaction.close();
  • 11. File Mode Parameters MODES MEANINGS  ios::app Append to end-of file  ios::ate Goto end of file on opening  ios::binary Binary file  ios::in Open existing file for reading  ios::nocreate Open fails if file doesn't exist  ios::noreplace Open fails if file already exists  ios::out Open existing file for writing  ios::trunc Deletes contents if it exists The mode can combine two or more modes using bit wise or (l)
  • 12. Types of Files  The two basic types of files are 1.Text File & 2.Binary File
  • 13. Text File Binary File  When using a text file, we write out separately each of the pieces of data about a given record.  The text file will be readable by an editor.  for the text file we will use the usual output operator(<<) and will output each of the pieces of the record separately.  with the text file we will read each of the pieces of record from the file separately, using the usual input operator(>>)  When using a Binary file we write whole record data to the file at once.  But the numbers in the binary file will not be readable in this way.  For the binary file we will use write to write to the file.  With the binary file we will use the read function to read a whole record.