CISC105 lab03, Fall 2005
(sections 022-025, Instructor: P. Conrad)

Introduction

This lab prepares you for stage 6 of project 1

The main purpose of this lab is to get you ready to do stage 6 of project 1. Stage 6 is worth 10% of project 1. This lab also has a small required component: it teaches you how to work with HTML files, and how to write data to an external file in C.

Learning objectives

By the time you finish this lab, you should be able to do all of the following:

  1. Be able to create a simple HTML file and put it on your webpage.
  2. Show how to use the following HTML elements: html, head, body, title, h1, p, table, tr, th, td.
  3. Show how to write to an external file using the C language.
  4. Show how to catch the error that occurs when an external file cannot be opened.
  5. Show how to write error messages using fprintf(stderr,"...") instead of printf("...");
  6. Show how to name that external file by using a command line argument (argv[1])
  7. Show how to check the number of command line arguments by testing argc.
  8. Show how to print out the name of the current C program using the symbol __FILE__
  9. Show how to combine all of the skills in the above list to create a program that writes out an html file that can be copied to a web page.

Part 1: Creating a cisc105 web page

  1. cd into ~/public_html ("cd ~/public_html") and create a subdirectory called "~/public_html/cisc105". You accomplish this in one of two ways: either by typing
    "mkdir -p ~/public_html/cisc105", or by just typing "mkdir cisc105".

    (The second way works because you are already in ~/public_html).

  2. Now, create an index.html file (using emacs) in this directory containing html code similar to the following:
    
    <html>
      <head>
        <title>Joe Sample's CISC105 Web page</title>
      </head>
    
      <body>
        <h1>Joe Sample's CISC105 Web page</h1>
    
        <p>I had to do this <b>web page</b> for my 
           <a href="http://udel.edu/~pconrad/cisc105"> CISC105 class</a>.
        </p> 
      

    </body> </html>

    Here's what that looks like once it is formatted by a web browser:

    Joe Sample's CISC105 Web page

    I had to do this web page for my CISC105 class.




  3. Once you have created the index.html file, you may need to repeat the "chmod -R a+rx ~/public_html" command in order to get the web page to come up when you use the URL http://udel.edu/~jsample/cisc105 (remember also to subsitute your own UDelNet ID in place of jsample).

  4. Then add a table similar to the following one, but using headings and data values from your "own" project (skyscrapers, movies with Julia Roberts, etc.)


  5. 
    <html>
      <head>
        <title>Joe Sample's CISC105 Web page</title>
      </head>
    
      <body>
        <h1>Joe Sample's CISC105 Web page</h1>
    
        <p>I had to do this <b>web page</b> for my 
           <a href="http://udel.edu/~pconrad/cisc105"> CISC105 class</a>.
        </p> 
      
       <table border="10" cellspacing="2">
        <tr>
          <th>pizza topping</th>
          <th>number of pizzas</th>
          <th>salad dressing</th>
          <th>price of pizza (dollars)</th>
        </tr>
        <tr>
           <td>green peppers</td>
           <td>5</td>
           <td>blue cheese</td>
           <td>$  5.99</td>
        </tr>
    </table>
           

    </body> </html>

Here's what THAT looks like when formatted by a web browser:



Joe Sample's CISC105 Web page

I had to do this web page for my CISC105 class.


pizza topping number of pizzas salad dressing price of pizza (dollars)
green peppers 5 blue cheese $ 5.99



When you are finished, make sure that the page is readable on the web.

 

Part 2: Some Example Programs for writing to files

Just like Part 1 of last week's lab, this part of the lab does not involve anything that you have to turn in for credit. So, you could skip it, and neither your instructor nor your TA would have any way to know.

But you really ought to do it anyway. If you don't, you won't have the information you need to proceed. These example files are there to be your "teachers".

Getting started

To get started, log on to strauss and do the following commands.

Note that in the last command, after the star, there is a space, then a period. That star, that space and that period are really important. The command tells strauss to "copy all the the files from a certain directory on pconrad's web site into my current directory". The period (by itself) means current directory. If you leave it off, you'll get an error message—possibly something about "permission denied" or "target not a directory" or some such thing.

cd ~/cisc105
mkdir lab03
cd lab03
mkdir htmlBuilders
cd htmlBuilders
cp ~pconrad/public_html/cisc105/05F/work/labs/lab03/htmlBuilders/* .

After doing these commands, if you do an ls command to list your files, you'll see something like the following:

> ls
Makefile htmlBuilder1.c htmlBuilder2.c htmlBuilder3.c test.html
>

You can also see these files at the following web link

Using the make command

In this directory, you see lots of .c files, and also a special file called Makefile. Use "cat" or "more" to take a look inside that file for a moment:

cat Makefile
more Makefile

You'll see some comments in the file that tell you a little bit about it. However, all you really need to know about it for now is that it enables you to compile a whole bunch of programs with a single command:

make

Try typing that command. You should see something like the following:

> make
gcc -o htmlBuilder1 htmlBuilder1.c
gcc -o htmlBuilder2 htmlBuilder2.c
gcc -o htmlBuilder3 htmlBuilder3.c
>

The system compiles each one of the .c programs and puts the executable code (the stuff that normally goes into a.out) into a separate file for each program, just like last week. .

Just like last week, you can change which compiler is used by changing the line in the Makefile that says CC=gcc to CC=cc (note the capital and small letters).

You can now run any of these programs by typing a dot and a slash, followed by the name of the program. For example, to run the program that corresponds to htmlBuilder1.c, type:

./htmlBuilder1

and to run the program that corresponds to htmlBuilder2.c, you type:

./htmlBuilder2

Ok, so what should I learn from these programs?

Try running ./htmlBuilder1. It will probably appear as if nothing happened.

> ./htmlBuilder1
>

However, if you now do an ls command to list your files, you'll see that there is a new file in your directory that wasn't there before:

 

> ls
Makefile htmlBuilder1.c htmlBuilder2.c htmlBuilder3.c
htmlBuilder1 htmlBuilder2 htmlBuilder3 test.html
>

Note the new file test.html. Try deleting that file, doing an ls, then running the program htmlBuilder1 again, and doing the ls again, as shown here:

> ls
Makefile htmlBuilder1.c htmlBuilder2.c htmlBuilder3.c
htmlBuilder1 htmlBuilder2 htmlBuilder3 test.html
> rm test.html
rm: remove test.html (yes/no)? yes
> ls
Makefile htmlBuilder1 htmlBuilder1.c htmlBuilder2 htmlBuilder2.c htmlBuilder3 htmlBuilder3.c
> ./htmlBuilder1
> ls
Makefile htmlBuilder1.c htmlBuilder2.c htmlBuilder3.c
htmlBuilder1 htmlBuilder2 htmlBuilder3 test.html
>

Note that the test.html file comes back.

If you look at the source code for this program (htmlBuilder1.c) you can learn all kinds of useful things. Find all the places where the program:

This program always writes a file called "test.html", and what is written to the file is always that same. That is kind of boring. So in htmlBuilder2 I've added code so that the name of the file can be specified on the command line. Look for the following changes in htmlBuilder2.c:

.

Your mission should you choose to accept it...

Your job is to look through all three programs, and understand how they work. You'll need this understanding in order to complete stage 6 of project 1, which is due in two weeks, and is worth 10% of the project, and therefore 1% of your final course grade. So, I hope you will accept this mission!

Your instructor will go over these programs with you in lecture over the coming days, but you can learn quite a bit about them on your own just by reading the code, running the code, and by experimenting. Don't be afraid to make your own copies of these programs and try adding your own variables and your fprintf() function calls. That is the one the best things you can do to become a more skillful programmer works.

How to clean up after yourself

When you are all done, you can type the following command:

make clean

This will get rid of all the executable files. This is good, because they take up a lot of disk space, and you don't need to keep them around unless you are actively working with the programs in this directory. You can bring them back anytime you like by typing make again, so why keep around the executables?

Part 3: Stage 6 of project 1
Adding code to your project to make an HTML table of information about your topic
(Filename: proj1s6.c, 10% of total points for proj1, proj1 100% complete when done)

In this part of the lab, you are going to do stage 6 of project 1. This is NOT REQUIRED for lab03; it is a part of project 1 that is described here for convenience sake.

What to do

First finish up project 1 up thorugh stage 5. Then, cd into your ~/cisc105/proj1 directory (this is where you should be working, NOT ~/public_html/cisc105/proj1), and copy your proj1s5.c file to proj1s6.c.

Then, following the example of htmlBuilder3.c, add code into your program so that after you prompt the user for values and echo those values back to the user, you create an html file that has a table of those values in it.

Take the name of the html file from the command line, following the example of htmlBuilder3.c.

When you script project 1 (if you include stage 6), you should run the program at least twice, specifying different filenames for the generated html file, and different values (e.g. two different skyscrapers, two different movies directed by Steven Speilberg, two different female tennis stars.)

The two filenames should reflect the content, e.g.

You should then copy the generated files to ~/public_html/cisc105/proj1, and make them readable. When the TA looks at your script, he'll also access your web page to make sure that the files there match the ones in your script.

 

Finishing up and Submitting

To submit lab03, you need to do each of the following:

  1. Make sure that your ~/public_html/cisc105/index.html file is readable, and has all the required elements.
  2. Upload the file index.html to WebCT for submission.
  3. Go to the web browser and print out your CISC105 web page (the formatted version, not the source code). Put on it your name, userid, lab section, date, the words "CISC105 lab03", and the date.

NOTE AGAIN: stage 6 of project 1 is due as part of project 1; it is not part of lab03.

Total points for lab03: 25 points.


Copyright 2005, Phillip T. Conrad. Some material contributed by Terry Harvey, used by permission. Permission to copy all or any part of this assignment for non-commercial, non-profit, educational use granted to anyone, provided appropriate credit is given, and this copyright notice is maintained. All other rights reserved.