SlideShare una empresa de Scribd logo
1 de 60
Descargar para leer sin conexión
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;
}

Más contenido relacionado

Similar a File Handling in C

VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5YOGESH SINGH
 
File handling in_c
File handling in_cFile handling in_c
File handling in_csanya6900
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams Ahmed Farag
 
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 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
 
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++
File Handling in c++File Handling in c++
File Handling in c++SoniKirtan
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++Shyam Gupta
 
Files in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceFiles in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceanuvayalil5525
 
INput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptxINput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptxAssadLeo1
 
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
 

Similar a File Handling in C (20)

VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
File handling
File handlingFile handling
File handling
 
File handling in_c
File handling in_cFile handling in_c
File handling in_c
 
Filehandlinging cp2
Filehandlinging cp2Filehandlinging cp2
Filehandlinging cp2
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 
Chapter - 5.pptx
Chapter - 5.pptxChapter - 5.pptx
Chapter - 5.pptx
 
File handling
File handlingFile handling
File handling
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
 
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 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
 
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
 
Data file handling
Data file handlingData file handling
Data file handling
 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
 
File Handling in c++
File Handling in c++File Handling in c++
File Handling in c++
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Files in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceFiles in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for reference
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
INput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptxINput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptx
 
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
 

Último

Submerged Combustion, Explosion Flame Combustion, Pulsating Combustion, and E...
Submerged Combustion, Explosion Flame Combustion, Pulsating Combustion, and E...Submerged Combustion, Explosion Flame Combustion, Pulsating Combustion, and E...
Submerged Combustion, Explosion Flame Combustion, Pulsating Combustion, and E...Ayisha586983
 
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptx
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptxTriangulation survey (Basic Mine Surveying)_MI10412MI.pptx
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptxRomil Mishra
 
AntColonyOptimizationManetNetworkAODV.pptx
AntColonyOptimizationManetNetworkAODV.pptxAntColonyOptimizationManetNetworkAODV.pptx
AntColonyOptimizationManetNetworkAODV.pptxLina Kadam
 
Introduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxIntroduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxPoonam60376
 
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.elesangwon
 
High Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMS
High Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMSHigh Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMS
High Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMSsandhya757531
 
Structural Integrity Assessment Standards in Nigeria by Engr Nimot Muili
Structural Integrity Assessment Standards in Nigeria by Engr Nimot MuiliStructural Integrity Assessment Standards in Nigeria by Engr Nimot Muili
Structural Integrity Assessment Standards in Nigeria by Engr Nimot MuiliNimot Muili
 
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfModule-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfManish Kumar
 
TEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACHTEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACHSneha Padhiar
 
A brief look at visionOS - How to develop app on Apple's Vision Pro
A brief look at visionOS - How to develop app on Apple's Vision ProA brief look at visionOS - How to develop app on Apple's Vision Pro
A brief look at visionOS - How to develop app on Apple's Vision ProRay Yuan Liu
 
Cost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based questionCost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based questionSneha Padhiar
 
Uk-NO1 kala jadu karne wale ka contact number kala jadu karne wale baba kala ...
Uk-NO1 kala jadu karne wale ka contact number kala jadu karne wale baba kala ...Uk-NO1 kala jadu karne wale ka contact number kala jadu karne wale baba kala ...
Uk-NO1 kala jadu karne wale ka contact number kala jadu karne wale baba kala ...Amil baba
 
ADM100 Running Book for sap basis domain study
ADM100 Running Book for sap basis domain studyADM100 Running Book for sap basis domain study
ADM100 Running Book for sap basis domain studydhruvamdhruvil123
 
Prach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism CommunityPrach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism Communityprachaibot
 
Comprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdfComprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdfalene1
 
Indian Tradition, Culture & Societies.pdf
Indian Tradition, Culture & Societies.pdfIndian Tradition, Culture & Societies.pdf
Indian Tradition, Culture & Societies.pdfalokitpathak01
 
Analysis and Evaluation of Dal Lake Biomass for Conversion to Fuel/Green fert...
Analysis and Evaluation of Dal Lake Biomass for Conversion to Fuel/Green fert...Analysis and Evaluation of Dal Lake Biomass for Conversion to Fuel/Green fert...
Analysis and Evaluation of Dal Lake Biomass for Conversion to Fuel/Green fert...arifengg7
 
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENTFUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENTSneha Padhiar
 

Último (20)

Submerged Combustion, Explosion Flame Combustion, Pulsating Combustion, and E...
Submerged Combustion, Explosion Flame Combustion, Pulsating Combustion, and E...Submerged Combustion, Explosion Flame Combustion, Pulsating Combustion, and E...
Submerged Combustion, Explosion Flame Combustion, Pulsating Combustion, and E...
 
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptx
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptxTriangulation survey (Basic Mine Surveying)_MI10412MI.pptx
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptx
 
AntColonyOptimizationManetNetworkAODV.pptx
AntColonyOptimizationManetNetworkAODV.pptxAntColonyOptimizationManetNetworkAODV.pptx
AntColonyOptimizationManetNetworkAODV.pptx
 
Introduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxIntroduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptx
 
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
 
High Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMS
High Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMSHigh Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMS
High Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMS
 
Structural Integrity Assessment Standards in Nigeria by Engr Nimot Muili
Structural Integrity Assessment Standards in Nigeria by Engr Nimot MuiliStructural Integrity Assessment Standards in Nigeria by Engr Nimot Muili
Structural Integrity Assessment Standards in Nigeria by Engr Nimot Muili
 
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfModule-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
 
ASME-B31.4-2019-estandar para diseño de ductos
ASME-B31.4-2019-estandar para diseño de ductosASME-B31.4-2019-estandar para diseño de ductos
ASME-B31.4-2019-estandar para diseño de ductos
 
TEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACHTEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACH
 
A brief look at visionOS - How to develop app on Apple's Vision Pro
A brief look at visionOS - How to develop app on Apple's Vision ProA brief look at visionOS - How to develop app on Apple's Vision Pro
A brief look at visionOS - How to develop app on Apple's Vision Pro
 
Cost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based questionCost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based question
 
Uk-NO1 kala jadu karne wale ka contact number kala jadu karne wale baba kala ...
Uk-NO1 kala jadu karne wale ka contact number kala jadu karne wale baba kala ...Uk-NO1 kala jadu karne wale ka contact number kala jadu karne wale baba kala ...
Uk-NO1 kala jadu karne wale ka contact number kala jadu karne wale baba kala ...
 
Versatile Engineering Construction Firms
Versatile Engineering Construction FirmsVersatile Engineering Construction Firms
Versatile Engineering Construction Firms
 
ADM100 Running Book for sap basis domain study
ADM100 Running Book for sap basis domain studyADM100 Running Book for sap basis domain study
ADM100 Running Book for sap basis domain study
 
Prach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism CommunityPrach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism Community
 
Comprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdfComprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdf
 
Indian Tradition, Culture & Societies.pdf
Indian Tradition, Culture & Societies.pdfIndian Tradition, Culture & Societies.pdf
Indian Tradition, Culture & Societies.pdf
 
Analysis and Evaluation of Dal Lake Biomass for Conversion to Fuel/Green fert...
Analysis and Evaluation of Dal Lake Biomass for Conversion to Fuel/Green fert...Analysis and Evaluation of Dal Lake Biomass for Conversion to Fuel/Green fert...
Analysis and Evaluation of Dal Lake Biomass for Conversion to Fuel/Green fert...
 
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENTFUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
 

File Handling in C

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