Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We have already seen how to work with files in C++ using objects (i.e., OOP). Th

ID: 3831920 • Letter: W

Question

We have already seen how to work with files in C++ using objects (i.e., OOP). The C programming language, on the other hand, does not directly support OOP, but does have constructs that can support file I/O. One major difference in between the two languages is in the way program input and output is handled, both to the standard I/O devices (i.e., keyboard and screen) and to and from files. C makes use of a FILE data structure that helps us to store our data permanently on a secondary storage device. Recall, that at a minimum, four steps must be done when working with files in C: Declare the FILE pointer variable. Open the file, using the file name and mode. Process the file, such as reading from or writing to the file. Close the file. Constants in C are defined differently than what is done in C++. In C, you must define a constant using the #define construct followed by the constant name and value without a terminating semi-colon as in the following for the constant PI: #define PI 3.14159 Suppose you received a file from a European friend with various distances in kilometers called kilometers .txt with the following values (each separated by a space): 1 3 5 8 10 15 20 25 30 42 50 100. (For this exercise, go ahead and create this file.) You would like to convert these values to miles and store them in an output file. So for this lab component, write a C program called LabEC_B.c that does the following: 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 <stdlib.h>
#define KM_TO_MILES 0.621371

int main()
{
FILE *fp = fopen("kilometers.txt", "r");
if(fp)
{
char filename[100];
printf("Enter a output file name: ");
scanf("%s", filename);
FILE *fp1 = fopen(filename, "w");
if(fp1)
{
int km;
while(fscanf(fp, "%d", &km) != EOF)
{
fprintf(fp1, "%.2f ", km*KM_TO_MILES);
}
fclose(fp1);
}
fclose(fp);

}
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote