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

File I/O in C We have already seen how to work with files in C++ using objects (

ID: 3831099 • Letter: F

Question

File I/O in C 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. 3 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. ** remember to add comments throughout your program

Explanation / Answer

//program

#include<stdio.h>
#define KM_TO_MILES 0.621371

int main()
{
   char fileName[20];
   FILE *in,*out;
   //open input file file for reading
   in = fopen("kilometers.txt", "r");
   int km;
   float mile;

   if (!in)
   {
       printf("Not able to open input file ");
       return -1;
   }

   printf("Please enter the name of the output file name: ");
   scanf("%s", fileName);

   //open outputfile for writing

   out = fopen(fileName, "w");

   while (fscanf(in, "%d", &km)!=EOF)
   {
       //convert km to mile and wriyte to file
       mile = km * KM_TO_MILES;
       fprintf(out, "%.2f ", mile);
   }
   fclose(out);
   fclose(in);
}

----------------------------------------------

//kilometrOt.txt file for given input file kilometrs.txt file

0.62 1.86 3.11 4.97 6.21 9.32 12.43 15.53 18.64 26.10 31.07 62.14

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