CSC231m Lecture 15 - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

CSC231m Lecture 15

Description:

3. Data vs. Records. An array is a collection of the same type of data, like an array of integers. ... a line of text from a file and retain the line terminator. ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 29
Provided by: markhutch
Category:

less

Transcript and Presenter's Notes

Title: CSC231m Lecture 15


1
CSC-231m Lecture 15
  • Files

2
Todays Topics
  • All about files.
  • What files really look like
  • How to do Homework 8.

3
Data vs. Records
  • An array is a collection of the same type of
    data, like an array of integers.
  • A record is a heterogeneous collection of
    different types of data, like a name, price, and
    quantity for a vending machine item.
  • Records can be implemented in three ways in
    MATLAB
  • As a cell array.
  • As a structure.
  • As a simple file.

4
A Simple File
5
What It Looks Like
  • Black ? 1.50? 10ltcrgt
  • Brown ? 1.75? 10ltcrgt
  • Red ? 2.00? 10ltcrgt
  • Orange? 2.25? 1ltcrgt
  • Yellow? 2.50? 10ltcrgt
  • Green ? 2.75? 5ltcrgt
  • lteofgt

6
Another Simple File, And Format
  • 101
  • 1
  • 166
  • 33
  • 227
  • 58
  • 229
  • 41
  • 46
  • 101ltcrgt
  • 1ltcrgt
  • 166ltcrgt
  • 33ltcrgt
  • 227ltcrgt
  • 58ltcrgt
  • 229ltcrgt
  • 41ltcrgt
  • 46ltcrgt
  • lteofgt

7
Delimiting Fields
  • Most languages support width-delimited fields,
    that is, each field is a known and fixed width.
    MATLAB does not.
  • MATLAB needs fields delimited by various means
  • \n so each field is on a different line.
  • \t to each field is delimited with a tab.
  • Etc.
  • The bottom line is that you need some means of
    knowing when one field stops and the next begins.

8
Delimiting atm.dat
  • You could have each balance on a separate line
  • 300.00
  • 500.00
  • 2500.00
  • You could have each balance on one line with tab
    delimiting
  • 300.00 500.00 2500.00
  • You could have the same with only space
    delimiting
  • 300.00 500.00 2500.00
  • In each case, fscanf() should properly read each
    value.

9
Delimiting Issues
  • Function fscanf() reads until it encounters white
    space
  • The end of a line.
  • A tab character.
  • A space.
  • The important thing is that you only read in one
    value at a time.

10
Opening Files
  • The fopen() function returns a file ID, so it
    must be part of an expression
  • fid fopen (file, access)
  • The filename can be a literal in single quotes or
    a variable name.
  • Access can be r for read, w for write, and
    a for append (write to end of file).
  • fid fopen (sFileName, r)
  • fid fopen (this.dat, w)

11
Reading From Files
  • Use fscanf() to read in single values or an array
    of like values (HW 8 Part C).
  • Use fgetl() (get line) to read in a line of text
    from a file (HW 8 Part A).
  • Use fgets() (get string) to read in a line of
    text from a file and retain the line terminator.
  • Use textread() or textscan() to read in lines of
    records from a file (HW 8 Part B).

12
Writing To Files
  • We only need fprintf() to write to files.
  • We do not assign it to a variable as we do with
    sprintf().
  • fprintf() needs three things
  • The open file ID.
  • A string literal with placeholders.
  • Variables corresponding to the placeholders.
  • fprintf (fid, d d\n, iThis, iThat)

13
Closing Files
  • Always close an open file.
  • Function fclose() only needs one thing, the open
    file ID
  • fclose (fid)

14
HW 8A Reading
  • We can only read from a file that exists.
  • To create a file, either use the editor to create
    a file, OR, use the write or append portion of
    this program.
  • Once a file exists, we read in each line using
    fgetl() to get one line.
  • Function fgetl() reads in the line as a string
  • sOut fgetl (fid)

15
HW 8A Writing
  • We can write to a new file (overwriting the file
    if it already exists), or we can append to an
    existing file (add onto the end).
  • In this assignment we write to create a file and
    we append to expand a file.
  • In each case, we use fprintf() to write to the
    file.
  • What distinguishes the operation is how we OPEN
    the file with fopen()
  • fid fopen (myfile, w)
  • Or fid fopen (myfile, a)

16
Your Files
  • As written the file looks like
  • These are my fat friends
  • John Vanilla 234
  • Sam Chocolate 185
  • Debbie Chocolate Chip 156
  • Monty Python Anthrax Ripple 278
  • Please come up with something better, like names,
    phone numbers, favorite drinks, age, pets,
    favorite color, etc.

17
Your Data
  • I used sName, sFlavor, and iWeight, and my
    sprintf function used -15s -15s d.
  • Use appropriate field names and placeholders
  • sWhatever is a string that uses s or -nns
  • fSomething is a float that uses f or .2f
  • iThis is an integer that uses d

18
HW 8A Procedure
  • Get my hw8a2.m file and edit it.
  • Using W
  • Create a file with three records.
  • Using R
  • Read the file.
  • Using A
  • Add two records to the file.
  • Using R
  • Read the file again.
  • Done

19
HW 8B Reading
  • Here we are reading from a file of records.
  • You need file student.txt from my directory.
  • We need to use the textscan() function to get one
    line at a time.
  • As we read it, we parse it into separate strings
    corresponding to placeholders.
  • Then we convert elements of the cell array into
    strings, convert to numbers, compare to values of
    interest, and report results.

20
HW 8B Processing
  • We read in a line.
  • We parse into a cell array.
  • We extract items from that array.
  • We convert those items to numbers.
  • We see if they match our criteria.
  • If they do, we print them out.
  • If they dont, we discard and read the next line.
  • This continues until we get to the end of the
    file.

21
Finding Other People
  • Numeric fields (class, gpa, units), or
    single-character fields (gender) are easy to deal
    with.
  • String fields are harder, and require another
    function
  • if (strcmp (sMaj, MUS) 1)
  • Not
  • if (sMaj MUS)

22
End-Of-File lteofgt
  • Function feof() tests for end of file.
  • It returns 1 when the eof is reached (true) and 0
    otherwise (false).

23
HW 8C Step 1
  • Remember to create the atm.dat file using your
    editor. It needs to be four lines long
  • 300.00ltcrgt
  • 500.00ltcrgt
  • 2500.00ltcrgt
  • lteofgt
  • Where ltcrgt does not show up, nor does the
    end-of-file marker lteofgt.

24
HW 8C Reading (Originally)
  • fid fopen (atm.dat, r)
  • dChckBal fscanf (fid, f)
  • dSaveBal fscanf (fid, f)
  • dVisaBal fscanf (fid, f)
  • fclose (fid)

25
Fix for HW8 Part C
  • fscanf() wants to read an array.
  • If the file does not exist, the program fails to
    run.
  • Fix
  • fid fopen (atm.dat, r)
  • if (fid -1) File exists
  • adBal fscanf (fid, f f f)
  • dChckBal adBal(1)
  • dSaveBal adBal(2)
  • dVisaBal adBal(3)
  • fclose (fid)
  • else
  • dChckBal 300.00
  • dSaveBal 500.00
  • dVisaBal 2500.00
  • end

26
HW 8C Writing
  • fid fopen (atm.dat, w)
  • fprintf (fid, f\n, dChckBal)
  • fprintf (fid, f\n, dSaveBal)
  • fprintf (fid, f\n, dVisaBal)
  • fclose (fid)

27
Other HW8 Fixes
  • Part A
  • Now
  • cWhatever input ()
  • Fix
  • cWhatever input (, s)
  • Part B
  • Now
  • if (iCls 1 iGpa lt 200)
  • fGPA str2num(char(stuff11)) / 100.0
  • Fix
  • if (iCls 1 iGpa lt 200)
  • fGPA iGpa / 100.0

28
Todays Lessons
  • You should be able to open and close files.
  • You should be able to read from and write to
    different types of text files.
  • You should better understand Homework 8.
Write a Comment
User Comments (0)
About PowerShow.com