NOTE: this is C programming language In this project, you have been asked to wri
ID: 3810011 • Letter: N
Question
NOTE: this is C programming language
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 <stdio.h>
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 <stdio.h>#include <stdlib.h>
#include <string.h>
int main(void)
{
float mass, height;
float BMI;
printf("Enter mass(kg): ");
scanf("%f", &mass);
printf("Enter height(m): ");
scanf("%f", &height);
BMI = mass / (height * height);
printf("BMI (kilogram per square meter): %.2f ", BMI);
FILE *fp;
FILE *fw;
char c;
char line [ 2048 ];
int counter = 1;
fp = fopen(__FILE__,"r");
fw = fopen("C:/Users/aditya/Desktop/ouput.txt","w");
while ( fgets ( line, sizeof line, fp ) != NULL ) /* read a line */
{
printf("%d: %s",counter,line);
fprintf(fw,"%d: %s",counter,line);
counter ++;
}
fclose(fp);
fclose(fw);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.