It is possible for a program to open a file for output prints some result close

It is possible for a program to open a file for

This preview shows page 8 - 14 out of 15 pages.

It is possible for a program to open a file for output, prints some result, close the file, and then open the same file for input.
Image of page 8
9 #include <stdio.h> #define KMS_PER_MILE 1.609 int main(void) { double kms, miles; FILE *infile, *outfile; infile = fopen( " data.txt","r"); outfile = fopen( " result.txt","w"); fscanf(infile, "%lf", &miles); fprintf(outfile, "The distance in miles is %.2f.\n", miles); kms = KMS_PER_MILE * miles; fprintf(outfile, "That equals %.2f kilometers.\n", kms); fclose(infile); fclose(outfile); return (0); } Example 1: Miles to Kilometers conversion using data files To run this program, you need to first create a file using any text editor, such as Notepad, type a double value in the file and save it as data.txt.
Image of page 9
10 Echo Prints vs. Prompts In the last example program, fscanf gets a value for miles from the data file. Because the program input comes from a data file, there is no need to precede this statement with a prompting message. Instead, we follow the call to fscanf with the statement printf(”The distance in miles is %.2f.\n”,miles); This statement echo prints or displays the value just stored in miles . Without it, we would have no easy way of knowing what value fscanf obtained for miles. Whenever you read input from a data file, make sure to use echo print instead of a prompt.
Image of page 10
11 #include <stdio.h> #define KMS_PER_MILE 1.609 int main(void) { double kms, miles; FILE *infile, *outfile; infile = fopen( " data.txt","r"); outfile = fopen( " result.txt","w"); //Scan and echo the distance in miles fscanf(infile, "%lf", &miles); fprintf(outfile, "The distance in miles is %.2f.\n", miles); kms = KMS_PER_MILE * miles; fprintf(outfile, "That equals %.2f kilometers.\n", kms); fclose(infile); fclose(outfile); return (0); } Echo Prints vs. Prompts …
Image of page 11
12 Handling File not found error A common error in using data files is forgetting to create the input file before running the program. Of course this will make the program generate a run-time error. Recall that the fopen function assigns NULL to the file pointer variable if the open operation fails. A common practice is to check the value of the file variable immediately after the fopen statement and stop the program right there if the variable has a NULL value. You can stop a program at any point by calling the exit function. if (infile==NULL) { // to check if input file is opened properly or not printf(“Sorry, input file not found"); exit(1); // terminates the program } Note: exit should be called with an argument of 1. This tells the operating system that the program stops due to an error. 0 (used with return (0) indicates success
Image of page 12
13 #include <stdio.h> #define KMS_PER_MILE 1.609
Image of page 13
Image of page 14

You've reached the end of your free preview.

Want to read all 15 pages?

  • Spring '10
  • baleh
  • Input/output, data files

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes