CS16, 10W, H09 (File input) Total points: ?

Available online at: http://www.cs.ucsb.edu/~pconrad/cs16/10W/homework/H09     (printable PDF)

Accepted: on paper, in LAB (Thursday afternoon Feb 4)

Late Policy: No email submission allowed—and don't "slip it under my door". If you need to make it up, you must do so during office hours, or make an appointment to see me, and you must request this appointment within 48 hours of when the assignment was originally due.

Personal Day/Sick Day policy: Everyone is permitted one "personal day/sick day" when you get to make up a missed homework assignment for free during office hours or via appointment. After that, you may not make up the homework assignment—you can only earn back the points through extra credit opportunities.

(For more details, see the syllabus and the homework policy)


Name: (2 pts)___________________________  UMail address (3 pts)  ______________@umail.ucsb.edu

Lab Section (5 pts) Circle one:         3pm       4pm         5pm           unknown

(Note: For now, circle the lab section you are registered for on GOLD. If you need to request attendance at a different lab section because of an ACTUAL SCHEDULE CONFLICT, please email pconrad@cs.ucsb.edu with details)


This assignment is due IN LAB on Thursday.
It may ONLY be turned in during LAB on Thursday.
Do NOT turn it in early in lecture on Tuesday or Thursday.

Name of your homework partner if you work with another person:



Read Section 3.6 in your Etter textbook.

Then answer these questions:

  1. (5 pts) In addition to using command line arguments, and scanf, there is a third way we can get input into a program—we can read it from a data file.

    According to your textbook, when you access a data file in a program, you need a special kind of variable called a file pointer.

    What is the line of C code that you would need to write to declare a file pointer with the variable name earthquakeData?




  2. (5 pts) There is a function you must call to associate the variable with a file on the disk.

    Suppose you want to read data from a file called "earthquakes.dat"

    What line of code would you write to associate the file "earthquakes.dat" with the file pointer you declared in the previous problem?



    Please turn over for questions to answer


    Continued from other side

  3. The answer to the previous problem involved a certain function. When you call that function, you sometimes get back a value of NULL.
    1. (5 pts) What does it mean when that happens?






    2. (5 pts) Why is it important to check for that by saying something like if (earthquakeData == NULL) after calling that function?





  4. There are various ways to read data from the file after it is opened. One of them is a function discussed in section 3.6.
    1. (5 pts) What is the name of that function?






    2. (5 pts) Write a function call that invokes this function to read a single number from earthquakeData into the variable magnitude.
      Assume that magnitude is of type double.




  5. (10 pts) What function call should your program make when it is finished with the data file associated with the variable earthquakeData?






End of H09