#include <iostream> #include <fstream> #include <string> using namespace std; in
ID: 3591399 • Letter: #
Question
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
//Declare and initialize objects.
int num_data_pts(0), k;
double time, motion, sum(0), max, min;
string filename;
ifstream sensor3;
ofstream report;
// Prompt user for name of input file.
cout << " Enter the name of the input file";
cin >> filename;
//open file and read teh first data point.
sensor3.open(filename.c_str());
if (sensor3.fail())
{
cerr << "Enter opening input file " ;
exit(1) ;
}
// open report file.
report.open("sensor3Report.txt") ;
//While not at the end of the file.
//Read and accumulate information
sensor3 >> time >> motion; //initial input
while ( !sensor3.eof() )
{
num_data_pts++;
if (num_data_pts == 1)
{
max = min = motion;
}
sum +=motion;
if (motion > max)
{
max =motion;
}
if (motion < min)
{
min = motion;
}
sensor3 >> time >> motion; //input next
}
//Set format flags.
report.setf(ios::fixed) ;
report.setf(ios::showpoint) ;
report.precision(2) ;
//print summary information.
report << "Number of sensor readings: "
<< num_data_pts << endl
<<"Average readings : "
<< sum/num_data_pts << endl
<< "Maximum reading: "
<< max << endl
<< "Minimum reading: "
<< min << endl;
//Close file and exit program.
sensor3.close() ;
report.close() ;
return 0 ;
} //end main
---------------------------------------------
The files are
Sensor3.dat
0 132.5
0.1 147.2
0.2 148.3
0.3 157.3
0.4 163.2
0.5 158.2
0.6 169.3
0.7 148.2
0.8 137.6
0.9 135.9
Sensor3Report.txt
Number of sensor readings: 10
Average raeding: 149.77
Maximum reading: 169.30
Minimum reading: 132.50
-------------------------------------------
1.Run the program with an empty data file. If the output is in error, modify the program to correct the error
-------------------------------------------------------------------------------------------------------------------------------------------------------
can you please till me how to run the program with an empty files??
can you also show the output how does it look like in your computer ?? Thank YOU!
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
int main()
{
//Declare and initialize objects.
int num_data_pts(0);
double time, motion, sum(0), max, min;
string filename;
ifstream sensor3;
ofstream report;
// Prompt user for name of input file.
cout << " Enter the name of the input file : ";
cin >> filename;
//open file and read teh first data point.
sensor3.open(filename.c_str());
if (sensor3.fail())
{
cerr << "Error in opening input file ";
exit(1);
}
sensor3.seekg(0, ios::end); // put the "cursor" at the end of the file
int length = sensor3.tellg(); // find the position of the cursor
sensor3.close(); // close your file
if ( length == 0 )
{
cerr << filename << "- is empty" << endl;
cerr << "Please kindly provide the filename , which must contain proper data " << endl;
return -1; // terminates the main program exeuction
}
sensor3.open(filename.c_str()); // re-open the accepted filename - input file stream
// open report file.
report.open("sensor3Report.txt") ;
//While not at the end of the file.
//Read and accumulate information
sensor3 >> time >> motion;
//initial input
while (!sensor3.eof())
{
num_data_pts++;
if (num_data_pts == 1)
{
max = min = motion;
}
sum +=motion;
if (motion > max)
{
max =motion;
}
if (motion < min)
{
min = motion;
}
sensor3 >> time >> motion; //input next
}
//Set format flags.
report.setf(ios::fixed) ;
report.setf(ios::showpoint) ;
report.precision(2) ;
//print summary information.
report << "Number of sensor readings: "
<< num_data_pts << endl
<<"Average readings : "
<< sum/num_data_pts << endl
<< "Maximum reading: "
<< max << endl
<< "Minimum reading: "
<< min << endl;
//Close file and exit program.
sensor3.close() ;
report.close() ;
return 0 ;
} //end main
/*
create an empty file as follows
at unix/linux $ prompt simply type > filename example >emptyfile.txt
now run ls -l emptyfile.txt
lenovo@lenovo-Vbox:~/chegg$ >emptyfile.txt
lenovo@lenovo-Vbox:~/chegg$ ls -l emptyfile.txt
-rw-rw-r-- 1 lenovo lenovo 0 Oct 13 23:15 emptyfile.txt
lenovo@lenovo-Vbox:~/chegg$
Compile the program as follows
lenovo@lenovo-Vbox:~/chegg$ g++ -Wall sensor.cpp
lenovo@lenovo-Vbox:~/chegg$
lenovo@lenovo-Vbox:~/chegg$ ./a.out
Enter the name of the input file : emptyfile.txt
emptyfile.txt- is empty
Please kindly provide the filename , which must contain proper data
lenovo@lenovo-Vbox:~/chegg$ cat emptyfile.txt
lenovo@lenovo-Vbox:~/chegg$ ls -l emptyfile.txt
-rw-rw-r-- 1 lenovo lenovo 0 Oct 13 23:15 emptyfile.txt
lenovo@lenovo-Vbox:~/chegg$ ls -l sensor3.txt
ls: cannot access sensor3.txt: No such file or directory
lenovo@lenovo-Vbox:~/chegg$ ls -l sensor3.data
-rw-rw-r-- 1 lenovo lenovo 98 Oct 13 22:54 sensor3.data
lenovo@lenovo-Vbox:~/chegg$ ./a.out
Enter the name of the input file : sensor3.txt
Error in opening input file
lenovo@lenovo-Vbox:~/chegg$ ./a.out
Enter the name of the input file : sensor3.data
lenovo@lenovo-Vbox:~/chegg$ ls -l sensor3Report.txt
-rw-rw-r-- 1 lenovo lenovo 151 Oct 13 23:19 sensor3Report.txt
lenovo@lenovo-Vbox:~/chegg$ cat sensor3Report.txt
Number of sensor readings: 10
Average readings : 149.77
Maximum reading: 169.30
Minimum reading: 132.50
lenovo@lenovo-Vbox:~/chegg$
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.