Lab 6 - 2 Programs - (Lec. 6)
Lab 6 - Program #1 - Write one number to a text file.
// This program writes one number two times to a text file. (see output below)
// The program uses the setprecision and fixed manipulators to format file output.
// The first output simply writes the number to file, without formatting decimal places.
// The second output formats the number to 2 places to the right of the decimal point.
Fill in the blanks - then enter the code and run the program and see if correct.
#include <iostream>
(1.) Include other libraries that may
be needed.
using namespace std;
int main()
{
fstream dataFile;
double number = 17.816392;
(2.) Write one statement to open
in output mode.
The file name is: values.txt
(3.) Format for fixed-point notation.
(4.) Write number to the file.
(5.) Format output to 2 decimal places.
(6.) Write number to the file.
cout << "Data has been written to file.\n";
(7.) Close the file.
return 0;
}
(8.) Open the text file to see the output.
Please fill in the blanks
C++ code for above problem
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
int main()
{
fstream dataFile;
double number = 17.816392;
ofstream myfile;
myfile.open("values.txt");
myfile << number << endl;
myfile << setprecision(4) << number
<< endl;
cout << "Data has been written to
file.\n";
myfile.close();
return 0;
}
Mention in comments if any mistakes or errors are found Thank you.
Get Answers For Free
Most questions answered within 1 hours.