SlideShare a Scribd company logo
1 of 4
<pre><div class="text_to_html">Filing
- Basic Operation
- Open
- Read/Write
- Close
- Header File
fstream
- Read
- ifstream
- Write
- ofstream
- Modes
- in
- input
- out
- output
- app
- append
******************************************************
Example 1: Writing data into a file
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ofstream out;
out.open("C14.txt", ios::app);
char array[25];
cin >> array;
out << array << endl;
out.close();
return 0;
}
*****************************************************
Example 2: Reading from File
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ifstream in;
in.open("C14.txt", ios::in);
char array[25];
while(!in.eof()){
in >> array;
cout << array << endl;
}
in.close();
return 0;
}
*****************************************************
Example 3: Reading data on file using functions
#include <iostream>
#include <fstream>
using namespace std;
void readData(ifstream &in){
char array[25];
while(!in.eof()){
in >> array;
cout << array << endl;
}
}
int main(){
ifstream in;
in.open("C14.txt", ios::in);
readData(in);
in.close();
return 0;
}
*****************************************************
Example 4: Writing data into a file using functions
#include <iostream>
#include <fstream>
using namespace std;
void writeData(ofstream &out, char array[]){
out << array << endl;
}
int main(){
ofstream out;
out.open("C14.txt", ios::app);
char array[25];
cin >> array;
writeData(out, array);
out.close();
return 0;
}
*****************************************************
Example 5: Reading and writing the attriubtes of ColdDrink
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
struct ColdDrink{
int price;
char name[10];
float quantity;
};
void writeData(ofstream &out, ColdDrink d){
out << d.name << "t";
out << d.price << "t";
out << d.quantity << endl;
}
void readData(ifstream &in, ColdDrink &d){
in >> d.name;
in >> d.price;
in >> d.quantity;
}
int main(){
ofstream output;
output.open("ColdDrink.txt", ios::out);
ifstream input;
input.open("ColdDrink.txt", ios::in);
ColdDrink d1;
strcpy(d1.name, "Fanta");
d1.price = 45;
d1.quantity = 0.5f;
writeData(output, d1);
ColdDrink cd;
readData(input, cd);
cout << "Name of the Drink: " << cd.name << endl;
cout << "Price of the Drink: " << cd.price << endl;
cout << "Quantity of the Drink: " << cd.quantity << endl;
output.close();
input.close();
return 0;
}</div></pre>
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
struct ColdDrink{
int price;
char name[10];
float quantity;
int expire;
};
void writeData(ofstream &write, ColdDrink &d){
write << d.name << "t";
write << d.price << "t";
write << d.quantity << endl;
write << d.expire << endl;
}
void readData(ifstream &read, ColdDrink &d){
read >> d.name;
read >> d.price;
read >> d.quantity;
read >> d.expire;
}
int main(){
ofstream writeintofile;
writeintofile.open("ColdDrink.txt", ios::out);
ifstream readintofile;
readintofile.open("ColdDrink.txt", ios::in);
ColdDrink drink;
strcpy(drink.name, "PEPSI");
drink.price = 45;
drink.quantity = 0.5f;
drink.expire = 2018;
writeData(writeintofile, drink);
ColdDrink colddrink;
readData(readintofile, colddrink);
cout << "Name of the Drink: " << colddrink.name << endl;
cout << "Price of the Drink: " <<colddrink .price << endl;
cout << "Quantity of the Drink: " << colddrink.quantity << endl;
cout << "expiry of the Drink: " << colddrink.expire << endl;
writeintofile.close();
readintofile.close();
return 0;
}

More Related Content

What's hot

File Transfer Through Sockets
File Transfer Through SocketsFile Transfer Through Sockets
File Transfer Through Socketsadil raja
 
Redis as a message queue
Redis as a message queueRedis as a message queue
Redis as a message queueBrandon Lamb
 
Naughty And Nice Bash Features
Naughty And Nice Bash FeaturesNaughty And Nice Bash Features
Naughty And Nice Bash FeaturesNati Cohen
 
QA Fest 2019. Saar Rachamim. Developing Tools, While Testing
QA Fest 2019. Saar Rachamim. Developing Tools, While TestingQA Fest 2019. Saar Rachamim. Developing Tools, While Testing
QA Fest 2019. Saar Rachamim. Developing Tools, While TestingQAFest
 
The Ring programming language version 1.7 book - Part 44 of 196
The Ring programming language version 1.7 book - Part 44 of 196The Ring programming language version 1.7 book - Part 44 of 196
The Ring programming language version 1.7 book - Part 44 of 196Mahmoud Samir Fayed
 
jSession #4 - Maciej Puchalski - Zaawansowany retrofit
jSession #4 - Maciej Puchalski - Zaawansowany retrofitjSession #4 - Maciej Puchalski - Zaawansowany retrofit
jSession #4 - Maciej Puchalski - Zaawansowany retrofitjSession
 
Compact ordered dict__k_lab_meeting_
Compact ordered dict__k_lab_meeting_Compact ordered dict__k_lab_meeting_
Compact ordered dict__k_lab_meeting_miki koganei
 
Commit2015 kharchenko - python generators - ext
Commit2015   kharchenko - python generators - extCommit2015   kharchenko - python generators - ext
Commit2015 kharchenko - python generators - extMaxym Kharchenko
 
Data File Handiling File POINTERS IN C++
Data File Handiling File POINTERS IN C++Data File Handiling File POINTERS IN C++
Data File Handiling File POINTERS IN C++subham sahu
 
お題でGroovyプログラミング: Part A
お題でGroovyプログラミング: Part Aお題でGroovyプログラミング: Part A
お題でGroovyプログラミング: Part AKazuchika Sekiya
 
MongoDB: How it Works
MongoDB: How it WorksMongoDB: How it Works
MongoDB: How it WorksMike Dirolf
 

What's hot (20)

Cpp lab 13_pres
Cpp lab 13_presCpp lab 13_pres
Cpp lab 13_pres
 
File Transfer Through Sockets
File Transfer Through SocketsFile Transfer Through Sockets
File Transfer Through Sockets
 
Redis as a message queue
Redis as a message queueRedis as a message queue
Redis as a message queue
 
Vcs28
Vcs28Vcs28
Vcs28
 
Naughty And Nice Bash Features
Naughty And Nice Bash FeaturesNaughty And Nice Bash Features
Naughty And Nice Bash Features
 
10 이중포인터를이용한삽입
10 이중포인터를이용한삽입10 이중포인터를이용한삽입
10 이중포인터를이용한삽입
 
QA Fest 2019. Saar Rachamim. Developing Tools, While Testing
QA Fest 2019. Saar Rachamim. Developing Tools, While TestingQA Fest 2019. Saar Rachamim. Developing Tools, While Testing
QA Fest 2019. Saar Rachamim. Developing Tools, While Testing
 
working with files
working with filesworking with files
working with files
 
The Ring programming language version 1.7 book - Part 44 of 196
The Ring programming language version 1.7 book - Part 44 of 196The Ring programming language version 1.7 book - Part 44 of 196
The Ring programming language version 1.7 book - Part 44 of 196
 
File Handling Program
File Handling ProgramFile Handling Program
File Handling Program
 
jSession #4 - Maciej Puchalski - Zaawansowany retrofit
jSession #4 - Maciej Puchalski - Zaawansowany retrofitjSession #4 - Maciej Puchalski - Zaawansowany retrofit
jSession #4 - Maciej Puchalski - Zaawansowany retrofit
 
Bash Scripting Workshop
Bash Scripting WorkshopBash Scripting Workshop
Bash Scripting Workshop
 
Compact ordered dict__k_lab_meeting_
Compact ordered dict__k_lab_meeting_Compact ordered dict__k_lab_meeting_
Compact ordered dict__k_lab_meeting_
 
Commit2015 kharchenko - python generators - ext
Commit2015   kharchenko - python generators - extCommit2015   kharchenko - python generators - ext
Commit2015 kharchenko - python generators - ext
 
Data File Handiling File POINTERS IN C++
Data File Handiling File POINTERS IN C++Data File Handiling File POINTERS IN C++
Data File Handiling File POINTERS IN C++
 
お題でGroovyプログラミング: Part A
お題でGroovyプログラミング: Part Aお題でGroovyプログラミング: Part A
お題でGroovyプログラミング: Part A
 
Finch + Finagle OAuth2
Finch + Finagle OAuth2Finch + Finagle OAuth2
Finch + Finagle OAuth2
 
Crawler 2
Crawler 2Crawler 2
Crawler 2
 
MongoDB: How it Works
MongoDB: How it WorksMongoDB: How it Works
MongoDB: How it Works
 
08 php-files
08 php-files08 php-files
08 php-files
 

Similar to File handling complete programs in c++

file handling final3333.pptx
file handling final3333.pptxfile handling final3333.pptx
file handling final3333.pptxradhushri
 
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bfinalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bChereCheek752
 
Computer science project work
Computer science project workComputer science project work
Computer science project workrahulchamp2345
 
File & Exception Handling in C++.pptx
File & Exception Handling in C++.pptxFile & Exception Handling in C++.pptx
File & Exception Handling in C++.pptxRutujaTandalwade
 
Sample file processing
Sample file processingSample file processing
Sample file processingIssay Meii
 
Chpater29 operation-on-file
Chpater29 operation-on-fileChpater29 operation-on-file
Chpater29 operation-on-fileDeepak Singh
 
hasbngclic687.ppt
hasbngclic687.ppthasbngclic687.ppt
hasbngclic687.pptHEMAHEMS5
 
Python Google Cloud Function with CORS
Python Google Cloud Function with CORSPython Google Cloud Function with CORS
Python Google Cloud Function with CORSRapidValue
 
c++ project on restaurant billing
c++ project on restaurant billing c++ project on restaurant billing
c++ project on restaurant billing Swakriti Rathore
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd StudyChris Ohk
 
Python Code Camp for Professionals 3/4
Python Code Camp for Professionals 3/4Python Code Camp for Professionals 3/4
Python Code Camp for Professionals 3/4DEVCON
 
Hack an ASP .NET website? Hard, but possible!
Hack an ASP .NET website? Hard, but possible! Hack an ASP .NET website? Hard, but possible!
Hack an ASP .NET website? Hard, but possible! Vladimir Kochetkov
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxssuser454af01
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++Shyam Gupta
 

Similar to File handling complete programs in c++ (20)

Supermarket
SupermarketSupermarket
Supermarket
 
File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
 
file handling final3333.pptx
file handling final3333.pptxfile handling final3333.pptx
file handling final3333.pptx
 
C++ files and streams
C++ files and streamsC++ files and streams
C++ files and streams
 
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bfinalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
 
Computer science project work
Computer science project workComputer science project work
Computer science project work
 
File & Exception Handling in C++.pptx
File & Exception Handling in C++.pptxFile & Exception Handling in C++.pptx
File & Exception Handling in C++.pptx
 
Sample file processing
Sample file processingSample file processing
Sample file processing
 
Chpater29 operation-on-file
Chpater29 operation-on-fileChpater29 operation-on-file
Chpater29 operation-on-file
 
hasbngclic687.ppt
hasbngclic687.ppthasbngclic687.ppt
hasbngclic687.ppt
 
Python build your security tools.pdf
Python build your security tools.pdfPython build your security tools.pdf
Python build your security tools.pdf
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Cmptr ass
Cmptr assCmptr ass
Cmptr ass
 
Python Google Cloud Function with CORS
Python Google Cloud Function with CORSPython Google Cloud Function with CORS
Python Google Cloud Function with CORS
 
c++ project on restaurant billing
c++ project on restaurant billing c++ project on restaurant billing
c++ project on restaurant billing
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 
Python Code Camp for Professionals 3/4
Python Code Camp for Professionals 3/4Python Code Camp for Professionals 3/4
Python Code Camp for Professionals 3/4
 
Hack an ASP .NET website? Hard, but possible!
Hack an ASP .NET website? Hard, but possible! Hack an ASP .NET website? Hard, but possible!
Hack an ASP .NET website? Hard, but possible!
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 

Recently uploaded

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 

Recently uploaded (20)

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 

File handling complete programs in c++

  • 1. <pre><div class="text_to_html">Filing - Basic Operation - Open - Read/Write - Close - Header File fstream - Read - ifstream - Write - ofstream - Modes - in - input - out - output - app - append ****************************************************** Example 1: Writing data into a file #include <iostream> #include <fstream> using namespace std; int main(){ ofstream out; out.open("C14.txt", ios::app); char array[25]; cin >> array; out << array << endl; out.close(); return 0; } ***************************************************** Example 2: Reading from File #include <iostream> #include <fstream> using namespace std; int main(){ ifstream in; in.open("C14.txt", ios::in); char array[25]; while(!in.eof()){ in >> array; cout << array << endl; } in.close(); return 0; } ***************************************************** Example 3: Reading data on file using functions #include <iostream> #include <fstream> using namespace std; void readData(ifstream &in){ char array[25]; while(!in.eof()){ in >> array; cout << array << endl;
  • 2. } } int main(){ ifstream in; in.open("C14.txt", ios::in); readData(in); in.close(); return 0; } ***************************************************** Example 4: Writing data into a file using functions #include <iostream> #include <fstream> using namespace std; void writeData(ofstream &out, char array[]){ out << array << endl; } int main(){ ofstream out; out.open("C14.txt", ios::app); char array[25]; cin >> array; writeData(out, array); out.close(); return 0; } ***************************************************** Example 5: Reading and writing the attriubtes of ColdDrink #include <iostream> #include <fstream> #include <string.h> using namespace std; struct ColdDrink{ int price; char name[10]; float quantity; }; void writeData(ofstream &out, ColdDrink d){ out << d.name << "t"; out << d.price << "t"; out << d.quantity << endl; } void readData(ifstream &in, ColdDrink &d){ in >> d.name; in >> d.price; in >> d.quantity; } int main(){ ofstream output; output.open("ColdDrink.txt", ios::out); ifstream input; input.open("ColdDrink.txt", ios::in); ColdDrink d1; strcpy(d1.name, "Fanta");
  • 3. d1.price = 45; d1.quantity = 0.5f; writeData(output, d1); ColdDrink cd; readData(input, cd); cout << "Name of the Drink: " << cd.name << endl; cout << "Price of the Drink: " << cd.price << endl; cout << "Quantity of the Drink: " << cd.quantity << endl; output.close(); input.close(); return 0; }</div></pre> #include <iostream> #include <fstream> #include <string.h> using namespace std; struct ColdDrink{ int price; char name[10]; float quantity; int expire; }; void writeData(ofstream &write, ColdDrink &d){ write << d.name << "t"; write << d.price << "t"; write << d.quantity << endl; write << d.expire << endl; } void readData(ifstream &read, ColdDrink &d){ read >> d.name; read >> d.price; read >> d.quantity; read >> d.expire; } int main(){ ofstream writeintofile; writeintofile.open("ColdDrink.txt", ios::out); ifstream readintofile;
  • 4. readintofile.open("ColdDrink.txt", ios::in); ColdDrink drink; strcpy(drink.name, "PEPSI"); drink.price = 45; drink.quantity = 0.5f; drink.expire = 2018; writeData(writeintofile, drink); ColdDrink colddrink; readData(readintofile, colddrink); cout << "Name of the Drink: " << colddrink.name << endl; cout << "Price of the Drink: " <<colddrink .price << endl; cout << "Quantity of the Drink: " << colddrink.quantity << endl; cout << "expiry of the Drink: " << colddrink.expire << endl; writeintofile.close(); readintofile.close(); return 0; }