SlideShare a Scribd company logo
1 of 3
Download to read offline
C++ Help: Filestream
Write a program that reads the values from a file and writes the sum of all multiples of 2
or 5 into another file. Your program must contain separate functions for reading and writing a
file.
Please explain your code-Will give a thumbs up. Thanks!
Solution
num.txt (Save this file inside D:Drive .Then the path of the file pointing to this file is D: um.txt
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
__________________________________________
CPP code
#include
#include
using namespace std;
//Declaring Global variable
int sum=0;
//Function declarations
void readFromFile();
void writeToFile();
/* Function implementation which will
* read the numbers from the file and calculates the sum of
* the numbers which are multiples of 2 or 5
*/
void readFromFile()
{
int number;
//fileInput reference
ifstream dataIn;
//Opening input file
dataIn.open("D: um.txt");
while(dataIn>>number)
{
if(number%2==0 || number%5==0)
{
sum+=number;
}
}
//closing the input stream
dataIn.close();
}
/* Function implementation which will
* display the sum into another text file
*/
void writeToFile()
{
//fileoutput reference
ofstream myfile;
//Opening the file
myfile.open ("D: um1.txt");
//Writing the contents to the file
myfile <<"The Sum of Multiples of 2 or 5 is :"<

More Related Content

Similar to C++ Help FilestreamWrite a program that reads the values from a f.pdf

Question 1 has already been posted to Chegg and I am waiting for the.pdf
Question 1 has already been posted to Chegg and I am waiting for the.pdfQuestion 1 has already been posted to Chegg and I am waiting for the.pdf
Question 1 has already been posted to Chegg and I am waiting for the.pdf
anjandavid
 
Chap 2 structure of c programming dti2143
Chap 2  structure of c programming dti2143Chap 2  structure of c programming dti2143
Chap 2 structure of c programming dti2143
alish sha
 
C programming session 11
C programming session 11C programming session 11
C programming session 11
AjayBahoriya
 

Similar to C++ Help FilestreamWrite a program that reads the values from a f.pdf (20)

Introduction of c language
Introduction of c languageIntroduction of c language
Introduction of c language
 
Srgoc dotnet
Srgoc dotnetSrgoc dotnet
Srgoc dotnet
 
1. introduction to computer
1. introduction to computer1. introduction to computer
1. introduction to computer
 
Unit 2 ppt
Unit 2 pptUnit 2 ppt
Unit 2 ppt
 
Question 1 has already been posted to Chegg and I am waiting for the.pdf
Question 1 has already been posted to Chegg and I am waiting for the.pdfQuestion 1 has already been posted to Chegg and I am waiting for the.pdf
Question 1 has already been posted to Chegg and I am waiting for the.pdf
 
Chap 2 structure of c programming dti2143
Chap 2  structure of c programming dti2143Chap 2  structure of c programming dti2143
Chap 2 structure of c programming dti2143
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
Lecture 1 progrmming with C
Lecture 1 progrmming with C Lecture 1 progrmming with C
Lecture 1 progrmming with C
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
C++ Constructs.pptx
C++ Constructs.pptxC++ Constructs.pptx
C++ Constructs.pptx
 
00 C hello world.pptx
00 C hello world.pptx00 C hello world.pptx
00 C hello world.pptx
 
Chapter3
Chapter3Chapter3
Chapter3
 
Unit 2 introduction to c programming
Unit 2   introduction to c programmingUnit 2   introduction to c programming
Unit 2 introduction to c programming
 
What is turbo c and how it works
What is turbo c and how it worksWhat is turbo c and how it works
What is turbo c and how it works
 
c_pro_introduction.pptx
c_pro_introduction.pptxc_pro_introduction.pptx
c_pro_introduction.pptx
 
C Programming ppt for beginners . Introduction
C Programming ppt for beginners . IntroductionC Programming ppt for beginners . Introduction
C Programming ppt for beginners . Introduction
 
Lecture01
Lecture01Lecture01
Lecture01
 
C programming session 11
C programming session 11C programming session 11
C programming session 11
 
Basics of C Lecture 2[16097].pptx
Basics of C Lecture 2[16097].pptxBasics of C Lecture 2[16097].pptx
Basics of C Lecture 2[16097].pptx
 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptxObject Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
 

More from Mujeeb76

Determine modes of inheritance for a set of phenotypes each of whic.pdf
Determine modes of inheritance for a set of phenotypes each of whic.pdfDetermine modes of inheritance for a set of phenotypes each of whic.pdf
Determine modes of inheritance for a set of phenotypes each of whic.pdf
Mujeeb76
 
(8 points) Assume that you have four (4) ROM chips organized as 16Kx8.pdf
(8 points) Assume that you have four (4) ROM chips organized as 16Kx8.pdf(8 points) Assume that you have four (4) ROM chips organized as 16Kx8.pdf
(8 points) Assume that you have four (4) ROM chips organized as 16Kx8.pdf
Mujeeb76
 

More from Mujeeb76 (12)

About 15 of ethanol that is ingested is metabolized first in the.pdf
About 15 of ethanol that is ingested is metabolized first in the.pdfAbout 15 of ethanol that is ingested is metabolized first in the.pdf
About 15 of ethanol that is ingested is metabolized first in the.pdf
 
Accountants prepare adjusting journal entries for four different typ.pdf
Accountants prepare adjusting journal entries for four different typ.pdfAccountants prepare adjusting journal entries for four different typ.pdf
Accountants prepare adjusting journal entries for four different typ.pdf
 
A contingency table is a table in whichA. the distributions of tw.pdf
A contingency table is a table in whichA. the distributions of tw.pdfA contingency table is a table in whichA. the distributions of tw.pdf
A contingency table is a table in whichA. the distributions of tw.pdf
 
1. NIMS public information is based on three principlesThe Public.pdf
1. NIMS public information is based on three principlesThe Public.pdf1. NIMS public information is based on three principlesThe Public.pdf
1. NIMS public information is based on three principlesThe Public.pdf
 
4 value 10.00 points TB 13-44 If a companys PE ratio suddenly de.pdf
4 value 10.00 points TB 13-44 If a companys PE ratio suddenly de.pdf4 value 10.00 points TB 13-44 If a companys PE ratio suddenly de.pdf
4 value 10.00 points TB 13-44 If a companys PE ratio suddenly de.pdf
 
Ethnocentrism is similar to cultural relativism. True or falseSo.pdf
Ethnocentrism is similar to cultural relativism. True or falseSo.pdfEthnocentrism is similar to cultural relativism. True or falseSo.pdf
Ethnocentrism is similar to cultural relativism. True or falseSo.pdf
 
C. wafer, wind, animals, earthquakes d. gravity, wind, animals, river.pdf
C. wafer, wind, animals, earthquakes d. gravity, wind, animals, river.pdfC. wafer, wind, animals, earthquakes d. gravity, wind, animals, river.pdf
C. wafer, wind, animals, earthquakes d. gravity, wind, animals, river.pdf
 
Determine whether the given function is linear. If the function is l.pdf
Determine whether the given function is linear. If the function is l.pdfDetermine whether the given function is linear. If the function is l.pdf
Determine whether the given function is linear. If the function is l.pdf
 
Determine modes of inheritance for a set of phenotypes each of whic.pdf
Determine modes of inheritance for a set of phenotypes each of whic.pdfDetermine modes of inheritance for a set of phenotypes each of whic.pdf
Determine modes of inheritance for a set of phenotypes each of whic.pdf
 
Create your own example of a simple but true experiment to test the .pdf
Create your own example of a simple but true experiment to test the .pdfCreate your own example of a simple but true experiment to test the .pdf
Create your own example of a simple but true experiment to test the .pdf
 
(8 points) Assume that you have four (4) ROM chips organized as 16Kx8.pdf
(8 points) Assume that you have four (4) ROM chips organized as 16Kx8.pdf(8 points) Assume that you have four (4) ROM chips organized as 16Kx8.pdf
(8 points) Assume that you have four (4) ROM chips organized as 16Kx8.pdf
 
An Overview of Financial Management Please respond to the followin.pdf
An Overview of Financial Management Please respond to the followin.pdfAn Overview of Financial Management Please respond to the followin.pdf
An Overview of Financial Management Please respond to the followin.pdf
 

Recently uploaded

Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 

Recently uploaded (20)

Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 

C++ Help FilestreamWrite a program that reads the values from a f.pdf

  • 1. C++ Help: Filestream Write a program that reads the values from a file and writes the sum of all multiples of 2 or 5 into another file. Your program must contain separate functions for reading and writing a file. Please explain your code-Will give a thumbs up. Thanks! Solution num.txt (Save this file inside D:Drive .Then the path of the file pointing to this file is D: um.txt ) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 __________________________________________ CPP code #include #include using namespace std;
  • 2. //Declaring Global variable int sum=0; //Function declarations void readFromFile(); void writeToFile(); /* Function implementation which will * read the numbers from the file and calculates the sum of * the numbers which are multiples of 2 or 5 */ void readFromFile() { int number; //fileInput reference ifstream dataIn; //Opening input file dataIn.open("D: um.txt"); while(dataIn>>number) { if(number%2==0 || number%5==0) { sum+=number; } } //closing the input stream dataIn.close(); } /* Function implementation which will * display the sum into another text file */ void writeToFile() { //fileoutput reference ofstream myfile;
  • 3. //Opening the file myfile.open ("D: um1.txt"); //Writing the contents to the file myfile <<"The Sum of Multiples of 2 or 5 is :"<