Write a program that reads the values from a file and writes
Solution
Here is the program as per your requiremensts.
#include <iostream.h> // must include to run a c++ file.
#include <fstream.h> // must for filesteam to deal with disk files.
#include <string.h> // string functions to be handled
#include <stdlib.h> // standard library functions.
int main () // main function from where execution starts
{
char ns[20]; //character array or string variable declaration.
int n; // declaration of integer variable
long int sum =0; // declaring and initializing
ifstream inputfile (\"Input.txt\"); // opening input file named input.txt into the variable inputfile
ofstream outputfile(\"Output.txt\"); // opening output file named output.txt into the variable outputfile.
if(inputfile != NULL) // if the input file is opened
{
while ( !inputfile.eof() ) // iterate through the input file.
{
inputfile >> ns;
n = atoi(ns);
if(n % 2 == 0 || n % 5 == 0)
sum = sum + n;
}
if(outputfile != NULL) // if the output file is opened
{
outputfile << sum ;
}
else
{
cout << \"\ Unable to open the output file.\" ;
}
}
else
{
cout << \"\ Unable to open the input file.\";
}
inputfile.close();
outputfile.close();
return 0;
}
Input:
4
6
5
2
10
25
Output:
52
