Use C-programming to write this program: Define a constant called KM_TO_MILES wi
ID: 3833605 • Letter: U
Question
Use C-programming to write this program:Define a constant called KM_TO_MILES with the value 0.621371. Assuming the name of the input file is called kilometers.txt, open this file for reading, being sure to check if there was an error opening the file. Prompt the user for the name of the output file and open the user-specified file name for writing, being sure to check if there was an error opening the file. Now, read all of the kilometer values from the file until the end-of-file (i.e., EOF) using fscanf. Then, for each value, write the appropriate mile conversion to the output file, where each mile conversion is on a separate line in the file, using fprintf. Be sure to format these mile values to two decimal places (recall that formatting with fprintf is the same as is done with printf). Close both of the files. Use C-programming to write this program:
Define a constant called KM_TO_MILES with the value 0.621371. Assuming the name of the input file is called kilometers.txt, open this file for reading, being sure to check if there was an error opening the file. Prompt the user for the name of the output file and open the user-specified file name for writing, being sure to check if there was an error opening the file. Now, read all of the kilometer values from the file until the end-of-file (i.e., EOF) using fscanf. Then, for each value, write the appropriate mile conversion to the output file, where each mile conversion is on a separate line in the file, using fprintf. Be sure to format these mile values to two decimal places (recall that formatting with fprintf is the same as is done with printf). Close both of the files. Use C-programming to write this program:
Define a constant called KM_TO_MILES with the value 0.621371. Assuming the name of the input file is called kilometers.txt, open this file for reading, being sure to check if there was an error opening the file. Prompt the user for the name of the output file and open the user-specified file name for writing, being sure to check if there was an error opening the file. Now, read all of the kilometer values from the file until the end-of-file (i.e., EOF) using fscanf. Then, for each value, write the appropriate mile conversion to the output file, where each mile conversion is on a separate line in the file, using fprintf. Be sure to format these mile values to two decimal places (recall that formatting with fprintf is the same as is done with printf). Close both of the files.
Explanation / Answer
#include<stdio.h>
#include<string.h>
#define KM_TO_MILES 0.621371
int main()
{
char outfile[100];
FILE *fp1,*fp2;
int line[10],mile=0,len,i;
//open kilometers file to read
fp1=fopen("kilometers.txt","r");
//if not found then print error message
if(fp1==NULL)
printf("Error while opening the file... ");
else
{
//ask the user to enter output file
printf("Enter outfile: ");
//store it into outfile
gets(outfile);
//open file to write converted miles into this file
fp2=fopen(outfile,"w");
if(fp2==NULL)
printf("Error while opening the file... ");
else
{
while(fgets(line,sizeof line,fp1)!=NULL)
{
mile=0;
//finding length of the line
len=strlen(line);
for(i=0;i<len-1;i++)
{
//multiply with constant conversion
mile=mile+line[i]*KM_TO_MILES;
}
//writting into specified file
fprintf(fp2,"%s",mile);
}
}
fclose(fp2);
}
fclose(fp1);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.