In this project, you have been asked to write a program that after execution it
ID: 3809989 • Letter: I
Question
In this project, you have been asked to write a program that after execution it does the following:
1-Read its own source file
2-Create another text file which is the same as its source file but the text file has line number. An example of running this program on the different source file (the assignment for computation of Body Mass Index) leads to generation of a file as
001: #include
002:
003: int main(void)
004: {
005: float mass, height;
006: float BMI;
007:
008: printf("Enter mass(kg): ");
009: scanf("%f", &mass);
010: printf("Enter height(m): ");
011: scanf("%f", &height);
012:
013: BMI = mass / (height * height);
014: printf("BMI (kilogram per square meter): %.2f ", BMI);
015:
016: return 0;
017: }
Note: In this example, I used a different source file as an input file. It meant to provide you with the format of the output file required to be generated by your program. In your project, the source file is the source file of the program that generates these line numbers.
Explanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
//Input object to read from the program file itself
ifstream fin;
fin.open("Program.cpp");
//Output object to write to the Output.txt file
ofstream fout;
fout.open("Output.txt");
//Character array to read every line
char s[80];
//Variable to keep track of the line number
int i=0;
while(!fin.eof())
{
//Reading the file line-by-line
fin.getline(s, 80);
//Printing the line number with precision 3
fout << setfill('0') << setw(3) << i << ": ";
fout << s << endl;
i++;
}
fin.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.