SlideShare a Scribd company logo
1 of 23
File Handling in C/C++
Objectives
• What is a computer file?
• Filing in programming?
• Steps for file handling in C++
• File and C++ Streams
• Program Skelton for file processing
• Reading a text-file
• Writing a text-file
• References
• Others
◦ Other Functions
◦ Solution of Real Problem
2najeeb.rehman@uog.edu.pk
What is a File?
• A computer file
◦ Collection of bytes
◦ Hold information
◦ Stored permanently on a secondary storage device (e.g., disk)
• Computer Program
◦ A process of step by step instructions to perform specified task and to produce result on given
input.
◦ File can be used to provide input data to a program or receive output data from a program, or
both
• Types of files
◦ Text File
◦ A stream of characters to process sequentially by a computer
◦ Image: A visual presentation of any entity
◦ Media: Audio/Video file 3najeeb.rehman@uog.edu.pk
Why File Handling in programming?
• Convenient way to deal large quantities of data.
• Store data permanently (until file is deleted).
• Avoid typing data into program multiple times.
• Share data between programs.
• Printable reports.
• Programming languages provide significant support for file processing.
• For file handling, we need to know:
◦ how to "connect" file to program
◦ how to tell the program to read data
◦ how to tell the program to write data
◦ error checking and handling EOF
5najeeb.rehman@uog.edu.pk
Contd’…
• Limitations of Console Input and output
• Input from Keyboard
◦ Large data Input
◦ Typos mistakes
◦ Time consuming & inefficient
• Screen Output
◦ Limited view on screen
6najeeb.rehman@uog.edu.pk
File Handling in C++
• C++ supports file handling in an attractive way
• Streams are used to communicate with file
◦ Stream of bytes to do input and output to different devices
• A program can read data from file or write data to file
• File ends with End Of File (EOF) marker.
• Five steps for file handling in C++ Language
I. Include fstream header file
II. Declare file stream variable(s)
III. Associate the file stream variable(s) with the input/output source(s)
IV. Read/Write operations
V. Close the file(s)
7najeeb.rehman@uog.edu.pk
Streams Hierarchy in C++
• ios is the base & abstract class
• istream and ostream inherit ios
• ifstream inherits istream
• ofstream inherits ostream
• iostream inherits istream and ostream
• fstream inherits ifstream, iostream, and ofstream
8
ios
ostreamistream
iostream
ofstreamifstream
fstream
najeeb.rehman@uog.edu.pk
C++ File Stream Functions
Function Description
open()  To open a file to read or write
is_open()  To test file either open or not
eof()  To check in reading a file either marker reach End Of File (EOF)
close()  To close the file
>>  Read data from file in general (operator)
<<  Write data in file in general (operator)
getline()  Reading a single line
9najeeb.rehman@uog.edu.pk
Program Skelton for File Processing
10
#include <fstream> // the header file/class for file stream objects
using namespace std;
int main()
{
//Declare file stream variables such as the following
ifstream my_input_file; //an input file stream object
ofstream my_output_file; // an output file stream object
//Open the files
my_input_file.open("prog.dat"); //open the input file
my_output_file.open("prog.out"); //open the output file
//Code for data manipulation
//Close files
my_input_file.close(); // close the file associated with this stream
my_output_file.close(); // close the file associated with this stream
return 0;
}
najeeb.rehman@uog.edu.pk
Reading from a File
11
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream my_input_file;
my_input_file.open("myData.txt");
if(!(my_input_file.is_open()))
{
cout<<"File cannot open.";
return 0;
}
cout<<"File Contents: n";
char ch;
while(!my_input_file.eof())
{
my_input_file.get(ch); // using get()
function
cout<<ch;
}
my_input_file.close();
return 0;
}
Input File: myData.txt
Reading a text file. Thank You.
Output
File Contents:
Reading a text file. Thank You.
najeeb.rehman@uog.edu.pk
Writing to a File
12
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream my_output_file;
my_output_file.open("myDat.txt");
if(!(my_output_file.is_open()))
{
cout<<"File cannot open.";
return 0;
}
char ch=' ‘;
cout<<"Writing contents to file: n";
while(ch!='.')
{
ch=getche();
my_output_file<<ch;
}
my_output_file.close();
return 0;
}
Sample Output
Writing contents to file:
Trying to write in test file.
Purpose:
This program take input from
user and full stop (.) to end.
Then write the entered data in
a text file.
najeeb.rehman@uog.edu.pk
References
• C++ Programming: From Problem Analysis to Program Design, 5th Edition by D.S. Malik
• C++ How to Program, 8th Edition by Deitel & Deitel
• Cplusplus [Online] http://www.cplusplus.com/
13najeeb.rehman@uog.edu.pk
More I/O Functions &
Sample Programs
14NAJEEB.REHMAN@UOG.EDU.PK
Sample Problem
• Write a program, which reads an input file of employee’s i.e. “employeein.txt”,
add the salary of each employee by 2000, and write the result in a new file
“employeeout.txt”.
15
The sample input file “employeein.txt”
Daud 12000
Akram 15000
Adnan 13000
Afzal 11500
The output file “employeeout.txt”
Name Salary
Daud 14000
Akram 17000
Adnan 15000
Afzal 13500
najeeb.rehman@uog.edu.pk
Analysis & Design
• Input
◦ Employee Names and Salaries
• Output
◦ Employ Name & Updated Salary
• Design of Algorithm
◦ Define input & output stream variables
◦ Open input (employeein.txt) & output (employeeout.txt) files
◦ Get data from input file (Name , Salary) of each employee
◦ Update salary by adding 2000 to original salary
◦ Write Name and Updated Salary to output file of each employee
◦ Close the files
• Test Your program for different input files of same structure
16najeeb.rehman@uog.edu.pk
Solution
17
#include<iostream>
#include<fstream>
#include<string>
#include<conio.h>
using namespace std;
int main()
{
ifstream inData;
ofstream outData;
string name;
int salary;
inData.open("employeein.txt");
outData.open("employeeout.txt");
najeeb.rehman@uog.edu.pk
if(!inData)
{
cout<<"Can't open input file"<<endl;
return 0;
}
if(!outData)
{
cout<<"Can't open Output file"<<endl;
return 0;
}
Solution Contd…
18
outData<<"Name"<<"t"<<"Salary"<<endl;
while(inData)
{
inData>>name;
inData>>salary;
outData<<name<<"t"<<salary+2000<<en
dl;
}
inData.close();
outData.close();
system("Pause");
system("employeeout.txt");
return 0;
}
Input File: employeein.txt
Aamir 12000
Amara 15000
Adnan 13000
Afzal 11500
Output File: employeeout.txt
Name Salary
Aamir 1400
Amara 17000
Adnan 15000
Afzal 13000
najeeb.rehman@uog.edu.pk
More Examples
najeeb.rehman@uog.edu.pk 19
1.//Sample Code#1 Input in file and Display data from file
2.#include<iostream>
3.#include<conio.h>
4.#include<fstream>
5.using namespace std;
6.void InputData();
7.void DisplayData();
8.void main()
9.{
10.InputData();
11.DisplayData();
12.system("pause");
13.} najeeb.rehman@uog.edu.pk 20
13.void InputData() {
14.ofstream out;
15.int x;
16.out.open("test.txt");
17.for(int i=0;i<5;i++) {
18.cin>>x;
19.out<<x<<endl;}
20.out.close();
21.}
23.void DisplayData() {
24.ifstream in;
25.int x;
26.in.open("test.txt");
27.for(int i=0;i<5;i++){
28.in>>x;
29.cout<<x<<endl;}
30.in.close();
31.}
najeeb.rehman@uog.edu.pk 21
1.//single input string and display
2.#include<iostream>
3.#include<conio.h>
4.#include<fstream>
5.#include<string>
6.using namespace std;
7.void main() {
8.//declaration
9.string Name;
10.ofstream out;
11.ifstream in;
12.//inserting single string with
space
13.out.open("test.txt");
14.getline(cin,Name);
15.out<<Name;
16.out.close();
17.// displaying string
18.in.open("test.txt");
19.getline(in,Name);
20.cout<<Name;
21.in.close();
22.system("pause");
23. }
najeeb.rehman@uog.edu.pk 22
1. //Append Mode,Get all data from file
2. #include<iostream>
3. #include<conio.h>
4. #include<fstream>
5. #include<string>
6. using namespace std;
7. void inserStringData();
8. void DisplayAllData();
9. int counter=0;
10.void main()
11.{
12.inserStringData();
13.DisplayAllData();
14.system("pause");
15.}
najeeb.rehman@uog.edu.pk 23
16.void inserStringData()
17.{
18.ofstream out;
19.string name;
20.out.open("test.txt",ios::app);
21.getline(cin,name);
22.out<<name<<endl;
23.out.close();
24.}
25.void DisplayAllData()
26.{
27.ifstream in;
28.string name;
29.in.open("test.txt");
30.while(!in.eof()) {
31.getline(in,name);
32.cout<<name<<endl;
33. }
34.in.close();
35. }
najeeb.rehman@uog.edu.pk 24

More Related Content

What's hot

Python Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaPython Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaEdureka!
 
Computer Essentials - Part 1 (IT Concepts).pptx
Computer Essentials - Part 1 (IT Concepts).pptxComputer Essentials - Part 1 (IT Concepts).pptx
Computer Essentials - Part 1 (IT Concepts).pptxMohammedAlaaMohammed
 
UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CAshim Lamichhane
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ pptKumar
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams Ahmed Farag
 
Overview of c++ language
Overview of c++ language   Overview of c++ language
Overview of c++ language samt7
 
For Loops and Nesting in Python
For Loops and Nesting in PythonFor Loops and Nesting in Python
For Loops and Nesting in Pythonprimeteacher32
 
Fundamentals of Python Programming
Fundamentals of Python ProgrammingFundamentals of Python Programming
Fundamentals of Python ProgrammingKamal Acharya
 
Python, the Language of Science and Engineering for Engineers
Python, the Language of Science and Engineering for EngineersPython, the Language of Science and Engineering for Engineers
Python, the Language of Science and Engineering for EngineersBoey Pak Cheong
 
Python Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaEdureka!
 
Introduction to C Programming - I
Introduction to C Programming - I Introduction to C Programming - I
Introduction to C Programming - I vampugani
 
File handling in C++
File handling in C++File handling in C++
File handling in C++Hitesh Kumar
 

What's hot (20)

File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Python Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaPython Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | Edureka
 
Computer Essentials - Part 1 (IT Concepts).pptx
Computer Essentials - Part 1 (IT Concepts).pptxComputer Essentials - Part 1 (IT Concepts).pptx
Computer Essentials - Part 1 (IT Concepts).pptx
 
COMPUTERS ( types of viruses)
COMPUTERS ( types of viruses)COMPUTERS ( types of viruses)
COMPUTERS ( types of viruses)
 
UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in C
 
File Pointers
File PointersFile Pointers
File Pointers
 
File handling
File handlingFile handling
File handling
 
Files and streams
Files and streamsFiles and streams
Files and streams
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 
ELF
ELFELF
ELF
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
 
Overview of c++ language
Overview of c++ language   Overview of c++ language
Overview of c++ language
 
For Loops and Nesting in Python
For Loops and Nesting in PythonFor Loops and Nesting in Python
For Loops and Nesting in Python
 
Fundamentals of Python Programming
Fundamentals of Python ProgrammingFundamentals of Python Programming
Fundamentals of Python Programming
 
Python, the Language of Science and Engineering for Engineers
Python, the Language of Science and Engineering for EngineersPython, the Language of Science and Engineering for Engineers
Python, the Language of Science and Engineering for Engineers
 
Introduction to c++ ppt
Introduction to c++ pptIntroduction to c++ ppt
Introduction to c++ ppt
 
C# Loops
C# LoopsC# Loops
C# Loops
 
Python Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | Edureka
 
Introduction to C Programming - I
Introduction to C Programming - I Introduction to C Programming - I
Introduction to C Programming - I
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
 

Viewers also liked

file handling, dynamic memory allocation
file handling, dynamic memory allocationfile handling, dynamic memory allocation
file handling, dynamic memory allocationindra Kishor
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++M Hussnain Ali
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its functionFrankie Jones
 
Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output) Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output) Aman Deep
 
File organization techniques
File organization techniquesFile organization techniques
File organization techniquesMeghlal Khan
 
The Internet & Multimedia Integration
The Internet & Multimedia IntegrationThe Internet & Multimedia Integration
The Internet & Multimedia IntegrationJoseph Stabb, ABD
 
The Internet and Multimedia
The Internet and Multimedia The Internet and Multimedia
The Internet and Multimedia CeliaBSeaton
 
File Organization
File OrganizationFile Organization
File OrganizationManyi Man
 
Chemistry Practical Record Full CBSE Class 12
Chemistry Practical Record Full CBSE Class 12 Chemistry Practical Record Full CBSE Class 12
Chemistry Practical Record Full CBSE Class 12 Muhammad Jassim
 
file handling c++
file handling c++file handling c++
file handling c++Guddu Spy
 
12th CBSE Practical File
12th CBSE Practical File12th CBSE Practical File
12th CBSE Practical FileAshwin Francis
 
BASIC COMPUTER ARCHITECTURE
BASIC COMPUTER ARCHITECTURE BASIC COMPUTER ARCHITECTURE
BASIC COMPUTER ARCHITECTURE Himanshu Sharma
 
Basic Computer Architecture
Basic Computer ArchitectureBasic Computer Architecture
Basic Computer ArchitectureYong Heui Cho
 

Viewers also liked (20)

file handling, dynamic memory allocation
file handling, dynamic memory allocationfile handling, dynamic memory allocation
file handling, dynamic memory allocation
 
8 header files
8 header files8 header files
8 header files
 
File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
 
C++ file
C++ fileC++ file
C++ file
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its function
 
Filehandlinging cp2
Filehandlinging cp2Filehandlinging cp2
Filehandlinging cp2
 
Computer Viruses
Computer VirusesComputer Viruses
Computer Viruses
 
Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output) Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)
 
File organization techniques
File organization techniquesFile organization techniques
File organization techniques
 
Files in c++
Files in c++Files in c++
Files in c++
 
The Internet & Multimedia Integration
The Internet & Multimedia IntegrationThe Internet & Multimedia Integration
The Internet & Multimedia Integration
 
The Internet and Multimedia
The Internet and Multimedia The Internet and Multimedia
The Internet and Multimedia
 
File Organization
File OrganizationFile Organization
File Organization
 
Chemistry Practical Record Full CBSE Class 12
Chemistry Practical Record Full CBSE Class 12 Chemistry Practical Record Full CBSE Class 12
Chemistry Practical Record Full CBSE Class 12
 
file handling c++
file handling c++file handling c++
file handling c++
 
12th CBSE Practical File
12th CBSE Practical File12th CBSE Practical File
12th CBSE Practical File
 
Transformer(Class 12 Investigatory Project)
Transformer(Class 12 Investigatory Project)Transformer(Class 12 Investigatory Project)
Transformer(Class 12 Investigatory Project)
 
BASIC COMPUTER ARCHITECTURE
BASIC COMPUTER ARCHITECTURE BASIC COMPUTER ARCHITECTURE
BASIC COMPUTER ARCHITECTURE
 
Basic Computer Architecture
Basic Computer ArchitectureBasic Computer Architecture
Basic Computer Architecture
 

Similar to Pf cs102 programming-8 [file handling] (1)

Similar to Pf cs102 programming-8 [file handling] (1) (20)

Data file handling
Data file handlingData file handling
Data file handling
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Files
 
Data file handling
Data file handlingData file handling
Data file handling
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
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
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
 
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
 
File Handling
File HandlingFile Handling
File Handling
 
File Handling
File HandlingFile Handling
File Handling
 
File handling in_c
File handling in_cFile handling in_c
File handling in_c
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Intro To C++ - Class #21: Files
Intro To C++ - Class #21: FilesIntro To C++ - Class #21: Files
Intro To C++ - Class #21: Files
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
File management
File managementFile management
File management
 
File handling.pptx
File handling.pptxFile handling.pptx
File handling.pptx
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operations
 
Basics of files and its functions with example
Basics of files and its functions with exampleBasics of files and its functions with example
Basics of files and its functions with example
 

Recently uploaded

原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档208367051
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一F sss
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfJohn Sterrett
 
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一F La
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一F La
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degreeyuu sss
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样vhwb25kk
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home ServiceSapana Sha
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024thyngster
 
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degreeyuu sss
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一fhwihughh
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceSapana Sha
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxStephen266013
 
RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.natarajan8993
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 

Recently uploaded (20)

原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdf
 
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
 
Call Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort ServiceCall Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort Service
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
 
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts Service
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docx
 
RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 

Pf cs102 programming-8 [file handling] (1)

  • 2. Objectives • What is a computer file? • Filing in programming? • Steps for file handling in C++ • File and C++ Streams • Program Skelton for file processing • Reading a text-file • Writing a text-file • References • Others ◦ Other Functions ◦ Solution of Real Problem 2najeeb.rehman@uog.edu.pk
  • 3. What is a File? • A computer file ◦ Collection of bytes ◦ Hold information ◦ Stored permanently on a secondary storage device (e.g., disk) • Computer Program ◦ A process of step by step instructions to perform specified task and to produce result on given input. ◦ File can be used to provide input data to a program or receive output data from a program, or both • Types of files ◦ Text File ◦ A stream of characters to process sequentially by a computer ◦ Image: A visual presentation of any entity ◦ Media: Audio/Video file 3najeeb.rehman@uog.edu.pk
  • 4. Why File Handling in programming? • Convenient way to deal large quantities of data. • Store data permanently (until file is deleted). • Avoid typing data into program multiple times. • Share data between programs. • Printable reports. • Programming languages provide significant support for file processing. • For file handling, we need to know: ◦ how to "connect" file to program ◦ how to tell the program to read data ◦ how to tell the program to write data ◦ error checking and handling EOF 5najeeb.rehman@uog.edu.pk
  • 5. Contd’… • Limitations of Console Input and output • Input from Keyboard ◦ Large data Input ◦ Typos mistakes ◦ Time consuming & inefficient • Screen Output ◦ Limited view on screen 6najeeb.rehman@uog.edu.pk
  • 6. File Handling in C++ • C++ supports file handling in an attractive way • Streams are used to communicate with file ◦ Stream of bytes to do input and output to different devices • A program can read data from file or write data to file • File ends with End Of File (EOF) marker. • Five steps for file handling in C++ Language I. Include fstream header file II. Declare file stream variable(s) III. Associate the file stream variable(s) with the input/output source(s) IV. Read/Write operations V. Close the file(s) 7najeeb.rehman@uog.edu.pk
  • 7. Streams Hierarchy in C++ • ios is the base & abstract class • istream and ostream inherit ios • ifstream inherits istream • ofstream inherits ostream • iostream inherits istream and ostream • fstream inherits ifstream, iostream, and ofstream 8 ios ostreamistream iostream ofstreamifstream fstream najeeb.rehman@uog.edu.pk
  • 8. C++ File Stream Functions Function Description open()  To open a file to read or write is_open()  To test file either open or not eof()  To check in reading a file either marker reach End Of File (EOF) close()  To close the file >>  Read data from file in general (operator) <<  Write data in file in general (operator) getline()  Reading a single line 9najeeb.rehman@uog.edu.pk
  • 9. Program Skelton for File Processing 10 #include <fstream> // the header file/class for file stream objects using namespace std; int main() { //Declare file stream variables such as the following ifstream my_input_file; //an input file stream object ofstream my_output_file; // an output file stream object //Open the files my_input_file.open("prog.dat"); //open the input file my_output_file.open("prog.out"); //open the output file //Code for data manipulation //Close files my_input_file.close(); // close the file associated with this stream my_output_file.close(); // close the file associated with this stream return 0; } najeeb.rehman@uog.edu.pk
  • 10. Reading from a File 11 #include<iostream> #include<fstream> using namespace std; int main() { ifstream my_input_file; my_input_file.open("myData.txt"); if(!(my_input_file.is_open())) { cout<<"File cannot open."; return 0; } cout<<"File Contents: n"; char ch; while(!my_input_file.eof()) { my_input_file.get(ch); // using get() function cout<<ch; } my_input_file.close(); return 0; } Input File: myData.txt Reading a text file. Thank You. Output File Contents: Reading a text file. Thank You. najeeb.rehman@uog.edu.pk
  • 11. Writing to a File 12 #include<iostream> #include<fstream> using namespace std; int main() { ofstream my_output_file; my_output_file.open("myDat.txt"); if(!(my_output_file.is_open())) { cout<<"File cannot open."; return 0; } char ch=' ‘; cout<<"Writing contents to file: n"; while(ch!='.') { ch=getche(); my_output_file<<ch; } my_output_file.close(); return 0; } Sample Output Writing contents to file: Trying to write in test file. Purpose: This program take input from user and full stop (.) to end. Then write the entered data in a text file. najeeb.rehman@uog.edu.pk
  • 12. References • C++ Programming: From Problem Analysis to Program Design, 5th Edition by D.S. Malik • C++ How to Program, 8th Edition by Deitel & Deitel • Cplusplus [Online] http://www.cplusplus.com/ 13najeeb.rehman@uog.edu.pk
  • 13. More I/O Functions & Sample Programs 14NAJEEB.REHMAN@UOG.EDU.PK
  • 14. Sample Problem • Write a program, which reads an input file of employee’s i.e. “employeein.txt”, add the salary of each employee by 2000, and write the result in a new file “employeeout.txt”. 15 The sample input file “employeein.txt” Daud 12000 Akram 15000 Adnan 13000 Afzal 11500 The output file “employeeout.txt” Name Salary Daud 14000 Akram 17000 Adnan 15000 Afzal 13500 najeeb.rehman@uog.edu.pk
  • 15. Analysis & Design • Input ◦ Employee Names and Salaries • Output ◦ Employ Name & Updated Salary • Design of Algorithm ◦ Define input & output stream variables ◦ Open input (employeein.txt) & output (employeeout.txt) files ◦ Get data from input file (Name , Salary) of each employee ◦ Update salary by adding 2000 to original salary ◦ Write Name and Updated Salary to output file of each employee ◦ Close the files • Test Your program for different input files of same structure 16najeeb.rehman@uog.edu.pk
  • 16. Solution 17 #include<iostream> #include<fstream> #include<string> #include<conio.h> using namespace std; int main() { ifstream inData; ofstream outData; string name; int salary; inData.open("employeein.txt"); outData.open("employeeout.txt"); najeeb.rehman@uog.edu.pk if(!inData) { cout<<"Can't open input file"<<endl; return 0; } if(!outData) { cout<<"Can't open Output file"<<endl; return 0; }
  • 17. Solution Contd… 18 outData<<"Name"<<"t"<<"Salary"<<endl; while(inData) { inData>>name; inData>>salary; outData<<name<<"t"<<salary+2000<<en dl; } inData.close(); outData.close(); system("Pause"); system("employeeout.txt"); return 0; } Input File: employeein.txt Aamir 12000 Amara 15000 Adnan 13000 Afzal 11500 Output File: employeeout.txt Name Salary Aamir 1400 Amara 17000 Adnan 15000 Afzal 13000 najeeb.rehman@uog.edu.pk
  • 19. 1.//Sample Code#1 Input in file and Display data from file 2.#include<iostream> 3.#include<conio.h> 4.#include<fstream> 5.using namespace std; 6.void InputData(); 7.void DisplayData(); 8.void main() 9.{ 10.InputData(); 11.DisplayData(); 12.system("pause"); 13.} najeeb.rehman@uog.edu.pk 20
  • 20. 13.void InputData() { 14.ofstream out; 15.int x; 16.out.open("test.txt"); 17.for(int i=0;i<5;i++) { 18.cin>>x; 19.out<<x<<endl;} 20.out.close(); 21.} 23.void DisplayData() { 24.ifstream in; 25.int x; 26.in.open("test.txt"); 27.for(int i=0;i<5;i++){ 28.in>>x; 29.cout<<x<<endl;} 30.in.close(); 31.} najeeb.rehman@uog.edu.pk 21
  • 21. 1.//single input string and display 2.#include<iostream> 3.#include<conio.h> 4.#include<fstream> 5.#include<string> 6.using namespace std; 7.void main() { 8.//declaration 9.string Name; 10.ofstream out; 11.ifstream in; 12.//inserting single string with space 13.out.open("test.txt"); 14.getline(cin,Name); 15.out<<Name; 16.out.close(); 17.// displaying string 18.in.open("test.txt"); 19.getline(in,Name); 20.cout<<Name; 21.in.close(); 22.system("pause"); 23. } najeeb.rehman@uog.edu.pk 22
  • 22. 1. //Append Mode,Get all data from file 2. #include<iostream> 3. #include<conio.h> 4. #include<fstream> 5. #include<string> 6. using namespace std; 7. void inserStringData(); 8. void DisplayAllData(); 9. int counter=0; 10.void main() 11.{ 12.inserStringData(); 13.DisplayAllData(); 14.system("pause"); 15.} najeeb.rehman@uog.edu.pk 23
  • 23. 16.void inserStringData() 17.{ 18.ofstream out; 19.string name; 20.out.open("test.txt",ios::app); 21.getline(cin,name); 22.out<<name<<endl; 23.out.close(); 24.} 25.void DisplayAllData() 26.{ 27.ifstream in; 28.string name; 29.in.open("test.txt"); 30.while(!in.eof()) { 31.getline(in,name); 32.cout<<name<<endl; 33. } 34.in.close(); 35. } najeeb.rehman@uog.edu.pk 24

Editor's Notes

  1. American Standard Code for Information Interchange (ASCII) Encoded to some standard like ASCII and etc
  2. unintentional typos cause erroneous results
  3. Use the file stream variables with >>, <<, or other input/output