File IO Part II - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

File IO Part II

Description:

more flexible - can add or take away data in file without having to change count ... 'while (infile)' is going to skip the loop altogether and go on, whereas... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 12
Provided by: Deb79
Category:
Tags: file | part

less

Transcript and Presenter's Notes

Title: File IO Part II


1
File I/O Part II
  • Sentinel logic again!

2
Processing a file's data
  • you must know how the data is arranged in the
    file, or you can't read it
  • different ways to process all the data in the
    file
  • one is put a count in the beginning that tells
    you how many pieces there are
  • the other is to put in some indicator - a
    sentinel - at the end of the data

3
Counter controlled reading
  • infile gtgt howmany
  • ct 0
  • while (ct lt howmany)
  • infile gtgt data
  • // process data
  • ct
  • // don't forget to put the count in the file! And
  • // make sure it's right!!

4
Sentinel controlled reading
  • more flexible - can add or take away data in file
    without having to change count
  • either use literal sentinel number OR
  • use the file stream state itself as the event to
    end the loop

5
Literal Sentinel value
  • infile gtgt data // priming read, remember?
  • while (data ! sentinel value)
  • // process data
  • infile gtgt data
  • // don't forget to put the sentinel value in
  • // there!!

6
File stream sentinel event
  • infile gtgt data
  • while (infile) // the name is true while
  • // the stream is ok
  • // process data
  • infile gtgt data
  • // does not require ANY special value at the
  • // end of the file, handles ANY amount of data

7
why not use eof()?
  • some people like to use
  • while (! infile.eof())
  • which means to continue while the file stream
    hasn't set the eof flag to true yet
  • this flag is set ONLY when an attempt is made to
    read PAST the end of the file
  • other things can make the file stream fail that
    will NOT set the eof flag!

8
Suppose...
  • suppose the input file didn't open, so the stream
    is in a fail state from the start
  • "while (infile)" is going to skip the loop
    altogether and go on, whereas...
  • "while (!infile.eof())" is going to fall into the
    loop and never get out, because the stream is
    failed, and the input statements in the loop are
    never executed! so eof () cannot ever become
    true!

9
Suppose ...
  • suppose there is data of the wrong type in the
    file, like reading letters into an numeric
    variable
  • this makes the stream fail but does NOT set the
    eof() flag to true
  • so the loop "while (!infile.eof())" will be an
    infinite loop - again, input statements will be
    ignored because the data was bad, but eof() will
    never be true because we can't read and thus
    can't get past the end of file

10
Fail States - why do you get one?
  • input files
  • can't open - input file doesn't exist
  • data of alphabetic type read into numeric
    variable
  • trying to read past end of file
  • output files
  • can't open because the PATH does not exist
  • can't write because there is no room on the device

11
Errors in sentinel logic
  • // This is an ERROR!
  • while (infile)
  • infile gtgt data
  • // process data
  • it will process the last piece of data TWICE!
  • Why?
Write a Comment
User Comments (0)
About PowerShow.com