CSC 107 - Programming for Science - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

CSC 107 - Programming for Science

Description:

... and utility ... FILE *ham, *eggs; // Yes, now we are cooking! ... bob = ham; // Both use same file now. int i = bob; // Gets warning, i's ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 18
Provided by: matthe85
Category:
Tags: csc | and | eggs | ham | programming | science

less

Transcript and Presenter's Notes

Title: CSC 107 - Programming for Science


1
CSC 107 -Programming for Science
  • Lecture 17
  • Data Files

2
Question of the Day
  • Move one matchstick to produce a square

3
Todays Goal
  • Todays lecture discusses handling data stored on
    disk
  • Discuss importance and utility of reading
  • C commands to open and close files, read contents
    of opened files
  • Also discuss data file parsing strategies
  • After todays lecture, you should be ready to
    write programs that reads processes data from a
    file

4
Data Files
  • Computers are fast but dumb
  • Good at performing repetitive or complex tasks
    that combine simple instructions
  • Large memory size good for data sets too large
    for humans
  • Most everything relies on data files
  • Searching through 1TB of saved web pages
  • Processing sensors or lab equipment inputs
  • Evaluating integrity of detailed design data
  • Check models against actual results
  • Automate drawing given description of motion

5
Working with Data Files
  • File access in C requires file pointer
  • Abstraction created to simplify improve
  • Will not worry about details of how they work
  • File pointers are just another data type
  • Variables declared as FILE
  • Our first non-numeric data!
  • Everything today involves input/output
  • includeltstdio.hgt needed to use functions

6
File Pointer Variables
  • Need to be careful using declaring
  • FILE fin // GoodFILE bob, jim //
    Bad! jim lacks powerFILE ham, eggs // Yes,
    now we are cooking!
  • Variables only describe files
  • Assignment to variable does not affect files
  • Variables values meaningful only to C
  • bob ham // Both use same file nowint i
    bob // Gets warning, is value ?

7
Opening a File
  • To use a file, must first open the file
  • FILE fopen(FILENAME, ACCESS)
  • Both arguments must be in quotes
  • FILENAME can include path
  • Path is not required
  • File names may be case-sensitive
  • ACCESS specifies how file will be used
  • r ? file will only be read from
  • Reading starts at beginning of file

8
Opening a File (2)
  • fopen returns NULL when unsuccessful
  • NULL is symbolic constant defined in stdio.h
  • May mean file does not exist, cannot be read, or
    some other reason
  • Using NULL file pointer will crash program
  • Returns some other value on success
  • Value not meaningful to humans, anyway

9
Reading From File
  • First assign file pointer to the opened file
  • int fscanf(FILE, ...) reads from file
  • First argument is file pointer to read from
  • Rest of arguments identical to scanf

10
Reading From File (2)
  • Always been using fscanf
  • Actually used to access keyboard
  • FILE stdin declared assigned automatically
  • stdin used to simplify reading from
    keyboarddouble a, bscanf(lf lf, a, b)
    is just shorthand fordouble a,
    bfscanf(stdin, lf lf, a, b)

11
How Much Data In the Window
  • May not know how much data is in file
  • Some uses require amount of data to vary
  • File formats may change to add/remove data
  • Upgrading sensors may produce more results
  • Do not want to constantly rewrite code
  • Have better things to do with your time
  • Hard-coded values can be difficult to find, fix
  • Reading past end of file can cause problems
  • Results of these reads undefined
  • May cause even more severe problems

12
Checking For End of File
  • Check value returned by fscanf
  • Returns number of data items it was able to read
  • When less than number requested, probably atend
    of fileFILE fin fopen(scores.txt,r)whil
    e ((fscanf(fin, lf, score)) 1) sum
    score count 1printf(Average from d
    scores was .2lf\n, count, sum/count)

13
Checking for End of File (2)
  • feof(FILE) also checks for end of file
  • Returns true if parameter is at end of file
    otherwise returns false
  • Can make easier to read codeFILE fin
    fopen(scores.txt,r)while (!feof(fin))
    fscanf(fin, lf, score) sum score
    count 1printf(Average from d scores was
    .2lf\n, count, sum/count)

14
Better Solution
  • Unless carefully written, using end-of-file
    checks as limit can cause problems
  • Could write record count at start of file
  • Begin by reading in this count
  • for loop then reads in that number of records
  • Could also use sentinel value at end of file
  • Keep reading until sentinel value is read
  • Using sentinel value can cause same problems as
    with keyboard input

15
Lessons from Kindergarten
  • Once finished using a file, close it
  • Done using fclose(FILE) function
  • Must re-open closed file pointer before using
  • Normally closing file is not important
  • Can be helpful if program crashes or file pointer
    reassigned, however
  • Also, some systems allow only one program at a
    time to access a file

16
Your Turn
  • Get into groups and complete daily activity

17
For Next Lecture
  • Start week 7 weekly assignment
  • Start programming assignment 2
  • Will be posted to course web page tomorrow
Write a Comment
User Comments (0)
About PowerShow.com