Given an initialized variable fileName , write a sequence of statements that cre
ID: 3769280 • Letter: G
Question
Given an initialized variable fileName, write a sequence of statements that create a file whose name is given by the variable and whose content is a single line consisting of "This Is File: " followed by the name of the file. Make sure that the data written to the file has been flushed from its buffer and that any system resources used during the course of running these statements have been released. (Do not concern yourself with any possible exceptions here-- assume they are handled elsewhere.) Needs to be written in C++ format.
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
int aNumber=0;
int numbers=0;
double sum=0.0;
double average=0.0;
ifstream randomFile;
randomFile.open("Random.txt");
if (randomFile.fail())
cout << "failed to read file.";
else
{
while (randomFile >> aNumber)
{
numbers++;
sum+=aNumber;
}
if (numbers>0)
average = sum/numbers;
else
average=0.0;
cout << "Number of numbers: " << numbers << " ";
cout << "Sum of the numbers: " << sum << " ";
cout << "Average of the numbers: " << average;
}
randomFile.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.