SlideShare a Scribd company logo
0
 Outline:
◦ concept of byte streams
◦ File access methods
◦ Opening and closing files
◦ Reading from files
◦ Write to files
 When a program runs, the data is in the memory, but when it ends or
the computer shuts down, it gets lost. To keep data permanently, we
need to write it in a file.
 A file is a collection of information, usually stored on a computer’s
disk. Information can be saved to files and then later reused.
 All files are assigned a name that is used for identification purposes
by the operating system and the user.
 We use file handling to store data permanently.
1
 Text file: it is a file that stores information in ASCII
characters. in a text file each line of text is terminated with a
special character known as EOL(End of Line) character or
delimiter character.
 Binary file: it is a file that contains information in the same
format as it is held in memory. in a binary file, no delimiters
are used for a line.
 Binary files are faster and easier for programs to read and
write.
2
 Sequential access: with this type of file access one must read
the data in order, much like with a tape whether the data is
really stored on tape or not.
 Random access: this type of file access lets you jump to any
location in the file. Write data to file
3
 A stream is a general term used to name the flow of
data.
 streams act as an interface between files and programs.
4
 File input stream: reads data from disk file to the program.
 File output stream: writes data to the disk from the program.
 The I/O system of C++ contains:
5
ofstream It used to create files and write on files.
ifstream It used to read from files.
fsream Supports both ifstream and ofstream operations.
Step1:Declare a file name variable
Step 2: Associate the file name variable with the disk file name.
Step3:Open the file
Step4:Use the file
Step5:Close the file
6
functions operation
open() To create a file.
close() To close an exsisting file.
get() Read a single character from file
put() Write a single character in file
read() Read data from file
write() Write data into file.
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
I. Declare a file name variable
◦ ifstream input_filename_var; // input file
◦ ofstream output_filename_var; // output file
II. Associate the file name variable with the
disk file name and open it.
◦ input_filename_var.open(“pathname/filename”);
◦ output_filename_var.open(“pathname/filename”);
53
 A file must be opened before you can read from it or write to it.
 Either ofstream or fstream object may be used to open a file for writing.
ifstream object is used to open a file for reading purpose only.
 The following are the different modes in which we can open a file.
54
ios::in opens a text file for reading.
ios::out opens a text file for writing.
ios::app
opens a text file for appending. (appending means to add text
at the end).
ios::ate
opens a file for output and move the read/write control to the
end of the file.
ios::trunc truncates the content before opening a file, if file exists.
ifstream input_filename_var(pathname/filename, ios::in);
ofstream input_filename_var(pathname/filename, ios::out);
Writing to a File:
 You write information to a file from your program using stream insertion operator
(<<) just as you use that operator to output information to the screen.
 The only difference is that you use an ofstream or fstream object instead of
the cout object.
◦ ofstream ofile1;
◦ ofile1 << x << y; // x and y are integers
◦ ofile2 << ch; // ch is a char
◦ ofile3 << “Hi there!” << endl; // literal string
◦ ofile4 << str; // str is a char*
55
 You read information from a file into your program using the stream
extraction operator (>>) just as you use that operator to input
information from the keyboard.
 The only difference is that you use an ifstream or fstream object
instead of the cin object.
◦ ifstream infile1;
◦ ifile1 >> x >> y; // x and y are integers
◦ ifile2 >> ch; // ch is a char
◦ ch = ifile3.get(); // ch is a char
◦ ifile4.getline(buffer, buffer_size) // buffer is char*
56
Reading from a File
IV. Close the file
 When a C++ program terminates it automatically flushes all the
streams, release all the allocated memory and close all the opened
files. But it is always a good practice that a programmer should
close all the opened files before program termination.
◦ input_filename_var.close();
◦ output_filename_var.close();
57
58
59
#include <fstream>
#include <iostream>
using namespace std;
int main ()
{
char data[100];
// open a file in write mode.
ofstream outfile;
outfile.open("afile.dat");
cout << "Writing to the file" << endl;
cout << "Enter your name: ";
cin.getline(data, 100);
// write inputted data into the file.
outfile << data << endl;
cout << "Enter your age: ";
cin >> data;
cin.ignore();
// again write inputted data into the file.
outfile << data << endl;
// close the opened file.
outfile.close();
// open a file in read mode.
ifstream infile;
infile.open("afile.dat");
cout << "Reading from the file" << endl;
infile >> data;
// write the data at the screen.
cout << data << endl;
// again read the data from the file and
display it.
infile >> data;
cout << data << endl;
// close the opened file.
infile.close();
return 0;
}

More Related Content

PPTX
Data file handling in c++
Vineeta Garg
 
PDF
Filepointers1 1215104829397318-9
AbdulghafarStanikzai
 
PPT
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
AzanMehdi
 
PPTX
File management in C++
apoorvaverma33
 
PPTX
basics of file handling
pinkpreet_kaur
 
PPTX
Basics of file handling
pinkpreet_kaur
 
PPT
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Venugopalavarma Raja
 
PPTX
File Handling
TusharBatra27
 
Data file handling in c++
Vineeta Garg
 
Filepointers1 1215104829397318-9
AbdulghafarStanikzai
 
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
AzanMehdi
 
File management in C++
apoorvaverma33
 
basics of file handling
pinkpreet_kaur
 
Basics of file handling
pinkpreet_kaur
 
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Venugopalavarma Raja
 
File Handling
TusharBatra27
 

Similar to Chapter4.pptx (20)

PPT
Filehandlinging cp2
Tanmay Baranwal
 
PPT
File in cpp 2016
Dr .Ahmed Tawwab
 
PPTX
Chapetr 4 C++ file object oriented programming
FiraolGadissa
 
PPT
File Handling In C++(OOPs))
Papu Kumar
 
PPT
7 Data File Handling
Praveen M Jigajinni
 
PDF
Data file handling
Prof. Dr. K. Adisesha
 
PPT
File handling in_c
sanya6900
 
PPTX
Data file handling
TAlha MAlik
 
PPTX
Filesin c++
HalaiHansaika
 
PPTX
pointer, structure ,union and intro to file handling
Rai University
 
PPT
file_handling_in_c.ppt......................................
nadoj47203
 
PPT
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
SanskritiGupta39
 
PPSX
Files in c++
Selvin Josy Bai Somu
 
PDF
Filesinc 130512002619-phpapp01
Rex Joe
 
PDF
Basics of files and its functions with example
Sunil Patel
 
DOCX
File handling in c++
Daniel Nyagechi
 
PDF
file handling c++
Guddu Spy
 
PPTX
Introduction to files management systems
araba8
 
PDF
Files and streams
Pranali Chaudhari
 
PPTX
Cs1123 10 file operations
TAlha MAlik
 
Filehandlinging cp2
Tanmay Baranwal
 
File in cpp 2016
Dr .Ahmed Tawwab
 
Chapetr 4 C++ file object oriented programming
FiraolGadissa
 
File Handling In C++(OOPs))
Papu Kumar
 
7 Data File Handling
Praveen M Jigajinni
 
Data file handling
Prof. Dr. K. Adisesha
 
File handling in_c
sanya6900
 
Data file handling
TAlha MAlik
 
Filesin c++
HalaiHansaika
 
pointer, structure ,union and intro to file handling
Rai University
 
file_handling_in_c.ppt......................................
nadoj47203
 
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
SanskritiGupta39
 
Files in c++
Selvin Josy Bai Somu
 
Filesinc 130512002619-phpapp01
Rex Joe
 
Basics of files and its functions with example
Sunil Patel
 
File handling in c++
Daniel Nyagechi
 
file handling c++
Guddu Spy
 
Introduction to files management systems
araba8
 
Files and streams
Pranali Chaudhari
 
Cs1123 10 file operations
TAlha MAlik
 

Recently uploaded (20)

PPTX
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
PDF
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
PPTX
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
PPTX
Online Cab Booking and Management System.pptx
diptipaneri80
 
PPT
Understanding the Key Components and Parts of a Drone System.ppt
Siva Reddy
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PDF
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PDF
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
PPTX
MULTI LEVEL DATA TRACKING USING COOJA.pptx
dollysharma12ab
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
PPTX
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
PDF
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
PDF
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PDF
Zero carbon Building Design Guidelines V4
BassemOsman1
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
PDF
All chapters of Strength of materials.ppt
girmabiniyam1234
 
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
Online Cab Booking and Management System.pptx
diptipaneri80
 
Understanding the Key Components and Parts of a Drone System.ppt
Siva Reddy
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
MULTI LEVEL DATA TRACKING USING COOJA.pptx
dollysharma12ab
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
Zero carbon Building Design Guidelines V4
BassemOsman1
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
All chapters of Strength of materials.ppt
girmabiniyam1234
 

Chapter4.pptx

  • 1. 0  Outline: ◦ concept of byte streams ◦ File access methods ◦ Opening and closing files ◦ Reading from files ◦ Write to files
  • 2.  When a program runs, the data is in the memory, but when it ends or the computer shuts down, it gets lost. To keep data permanently, we need to write it in a file.  A file is a collection of information, usually stored on a computer’s disk. Information can be saved to files and then later reused.  All files are assigned a name that is used for identification purposes by the operating system and the user.  We use file handling to store data permanently. 1
  • 3.  Text file: it is a file that stores information in ASCII characters. in a text file each line of text is terminated with a special character known as EOL(End of Line) character or delimiter character.  Binary file: it is a file that contains information in the same format as it is held in memory. in a binary file, no delimiters are used for a line.  Binary files are faster and easier for programs to read and write. 2
  • 4.  Sequential access: with this type of file access one must read the data in order, much like with a tape whether the data is really stored on tape or not.  Random access: this type of file access lets you jump to any location in the file. Write data to file 3
  • 5.  A stream is a general term used to name the flow of data.  streams act as an interface between files and programs. 4
  • 6.  File input stream: reads data from disk file to the program.  File output stream: writes data to the disk from the program.  The I/O system of C++ contains: 5 ofstream It used to create files and write on files. ifstream It used to read from files. fsream Supports both ifstream and ofstream operations.
  • 7. Step1:Declare a file name variable Step 2: Associate the file name variable with the disk file name. Step3:Open the file Step4:Use the file Step5:Close the file 6
  • 8. functions operation open() To create a file. close() To close an exsisting file. get() Read a single character from file put() Write a single character in file read() Read data from file write() Write data into file. 7
  • 9. 8
  • 10. 9
  • 11. 10
  • 12. 11
  • 13. 12
  • 14. 13
  • 15. 14
  • 16. 15
  • 17. 16
  • 18. 17
  • 19. 18
  • 20. 19
  • 21. 20
  • 22. 21
  • 23. 22
  • 24. 23
  • 25. 24
  • 26. 25
  • 27. 26
  • 28. 27
  • 29. 28
  • 30. 29
  • 31. 30
  • 32. 31
  • 33. 32
  • 34. 33
  • 35. 34
  • 36. 35
  • 37. 36
  • 38. 37
  • 39. 38
  • 40. 39
  • 41. 40
  • 42. 41
  • 43. 42
  • 44. 43
  • 45. 44
  • 46. 45
  • 47. 46
  • 48. 47
  • 49. 48
  • 50. 49
  • 51. 50
  • 52. 51
  • 53. 52
  • 54. I. Declare a file name variable ◦ ifstream input_filename_var; // input file ◦ ofstream output_filename_var; // output file II. Associate the file name variable with the disk file name and open it. ◦ input_filename_var.open(“pathname/filename”); ◦ output_filename_var.open(“pathname/filename”); 53
  • 55.  A file must be opened before you can read from it or write to it.  Either ofstream or fstream object may be used to open a file for writing. ifstream object is used to open a file for reading purpose only.  The following are the different modes in which we can open a file. 54 ios::in opens a text file for reading. ios::out opens a text file for writing. ios::app opens a text file for appending. (appending means to add text at the end). ios::ate opens a file for output and move the read/write control to the end of the file. ios::trunc truncates the content before opening a file, if file exists. ifstream input_filename_var(pathname/filename, ios::in); ofstream input_filename_var(pathname/filename, ios::out);
  • 56. Writing to a File:  You write information to a file from your program using stream insertion operator (<<) just as you use that operator to output information to the screen.  The only difference is that you use an ofstream or fstream object instead of the cout object. ◦ ofstream ofile1; ◦ ofile1 << x << y; // x and y are integers ◦ ofile2 << ch; // ch is a char ◦ ofile3 << “Hi there!” << endl; // literal string ◦ ofile4 << str; // str is a char* 55
  • 57.  You read information from a file into your program using the stream extraction operator (>>) just as you use that operator to input information from the keyboard.  The only difference is that you use an ifstream or fstream object instead of the cin object. ◦ ifstream infile1; ◦ ifile1 >> x >> y; // x and y are integers ◦ ifile2 >> ch; // ch is a char ◦ ch = ifile3.get(); // ch is a char ◦ ifile4.getline(buffer, buffer_size) // buffer is char* 56 Reading from a File
  • 58. IV. Close the file  When a C++ program terminates it automatically flushes all the streams, release all the allocated memory and close all the opened files. But it is always a good practice that a programmer should close all the opened files before program termination. ◦ input_filename_var.close(); ◦ output_filename_var.close(); 57
  • 59. 58
  • 60. 59 #include <fstream> #include <iostream> using namespace std; int main () { char data[100]; // open a file in write mode. ofstream outfile; outfile.open("afile.dat"); cout << "Writing to the file" << endl; cout << "Enter your name: "; cin.getline(data, 100); // write inputted data into the file. outfile << data << endl; cout << "Enter your age: "; cin >> data; cin.ignore(); // again write inputted data into the file. outfile << data << endl; // close the opened file. outfile.close(); // open a file in read mode. ifstream infile; infile.open("afile.dat"); cout << "Reading from the file" << endl; infile >> data; // write the data at the screen. cout << data << endl; // again read the data from the file and display it. infile >> data; cout << data << endl; // close the opened file. infile.close(); return 0; }