CIS105 Fall 2003 Lab09

For Lab12 we are NOT doing the chapter from the lab manual. Instead, we are going to do a lab that will prepare us for some of the skills we need for the next two projects.

Part One: Writing to files

The following sample program demonstrates how to write output to a file in C. The program opens an output file, and writes some output into that file, and then closes it. You can copy this program into your directory by typing the following. Be sure not to forget the dot at the end!
cp ~pconrad/public_html/cis105/2003.fall/labs/lab12Code/lab12a.c .
/* a program that just demonstrates how to write output to a file */

#include <stdio.h>

int main ()
{
  FILE *outfile;  /* declare a file pointer */
  int x=3;        /* x and y are variables just for demonstration purposes */
  double y=4.5;
  
  /* open the output file */
  
  outfile = fopen("lab12a.dat","w"); /* try to open file for writing */
  if (outfile == NULL)               /* check return status */
    {
      perror("File lab12a.dat could not be opened: "); /* print error */
      exit(-1);
    }
  
  /* write some output to the file */
  
  fprintf(outfile,"This output is going to the file lab12a.dat.\n");
  
  printf("This output is going to the terminal.\n");
  printf("I can still write to the terminal in the same program\n");
  printf("that I am writing to a file.\n");
  
  fprintf(outfile,"I can also print the value of variables to a file.\n");
  fprintf(outfile,"for example, x=%d, y=%.2f\n",x,y);
  fprintf(outfile,"Bye now!\n");
  
  /* close the output file */
  
  fclose(outfile);
  
  return 0;
}


For Part1 of this lab, do the following steps. The result will be a script file lab12a.txt, which you will submit.
  1. Copy the lab12a.c file into your directory.
  2. Compile it, but don't run it yet.
  3. Before you run it, do an ls command, and note that there is no file called lab12a.dat in your directory.
  4. Now run the program. Note the output you get on your screen. Then do another ls command and note that there should now be a file called lab12a.dat in your directory. Use cat lab12a.dat to list the output of this file.
  5. Now edit the lab12a.c file to personalize the message that is going to the file. Make it print your name to the file as part of the message, along with any other pithy comments you want to pass along to your TA and/or instructor.
  6. Use the rm command to delete lab12.dat from your directory. Then, make a script file lab12a.txt in which you
    1. cat and then compile your new (personalized) version of lab12a.c,
    2. use ls to show that there is no lab12a.dat file in your directory,
    3. run the program,
    4. redo the ls command to show that lab12a.dat was created,
    5. and cat the output of your new personalized lab12a.dat.

    You should submit lab12a.txt as part of your submission for lab12. You don't need to submit the .c file, or the .dat file for this part.

Part Two: Writing to multiple files

As part of your projects, you may need to write to multiple files from within the same program. The following program demonstrates how to this. The program also demonstrates how to write to files from functions.

To do this part of the lab, take the following steps.

  1. Copy this file (lab12b.c) into your directory. The form of the command is the same as the command for lab12a.c.
  2. Look over the program and try to understand how it works.
  3. Run the program and observe the two new files that are created in your directory. As with lab12a.c, you should do an ls command before and after you run the program to see what files are created. Use cat or more to see the contents of the files.
  4. Use the command gnuplot lab12b.gnuplot to generate a file lab12b.png. Then, view the file, either by copying it into your public_html directory and using a web browser to view it, or, if you are on an X terminal, using the command xv lab12b.png.
  5. Note that the letter S is missing a piece. Make the necessary changes to the program to fix this.
  6. When you are finished, cat and script your completed program. Your script should include a cat of the resulting files lab12b.dat and lab12b.gnuplot(though NOT the .png file, which you should NOT cat, since it will just fill the script file with garbage). Use lab12b.txt as the name of your script.
  7. As a final step, make a subdirectory called cis105 in your public_html directory (i.e. mkdir ~/public_html/cis105). Copy your lab12b.png file into that subdirectory. Make both the subdirectory and the file world readable, so that the URL http://udel.edu/~youruserid/cis105/lab12b.png allows anyone who wants to (e.g. me and your TA) to see your resulting output file.
You should submit lab12b.txt as part of your submission for lab12. You don't need to submit the .c file, or the .dat file for this part. However, you should also set up the lab12b.png file (with the S fixed) in your web page, under the cis105 subdirectory.

For an A grade:

For an A grade: copy lab12b.c to lab12c.c, and make the file print CIS105 instead of CIS. You should change all the other filenames to be lab12c instead of lab12b, and you should also use the "curved bottom" function to make the bottom of the number 5. Script your program and turn in a script lab12c.txt. Also, put your resulting lab12c.png on your web page, under the cis105 subdirectory.
Phillip T Conrad
Last modified: Fri Nov 21 08:48:08 EST 2003