COMP102 Lab 7 - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

COMP102 Lab 7

Description:

fin.close(); a.txt. Example. The output will be something like this: ... while (!fin.eof()) = false. Extra and unnecessary EOF character (char value = -1) Explanation ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 12
Provided by: raym168
Category:
Tags: comp102 | fin | lab

less

Transcript and Presenter's Notes

Title: COMP102 Lab 7


1
COMP102 Lab 7
2
File I/O
  • The file operations always follow the following
    sequences
  • Open the file
  • Read/write the file contents
  • Close the file
  • REMEMBER to close the file. Otherwise, the
    contents may be lost!

3
eof()
  • A function which return 1/true if the ifstream
    HAS reached the end of the file
  • ifstream will NOT know it has reached the end
    untill it has tried to read something from the
    file

4
Example
  • Suppose there are 2 characters in the file a.txt
  • What will bethe output of the following code
    segment?

ifstream fin char c fin.open("a.txt") while
(!fin.eof()) fin.get(c) cout ltlt
c cout ltlt "end" ltlt endl fin.close()
ab
a.txt
5
Example
  • The output will be something like this
  • There is an extra space between ab and end
  • The extra space is actually the EOF character
  • Why??

ab end
6
Example
  • Lets add some code to display the value of eof()

ifstream fin char c fin.open("a.txt") while
(!fin.eof()) cout ltlt "BEFORE get() eof()"
ltlt fin.eof() ltlt endl fin.get(c) cout ltlt
"c" ltlt c ltlt endl cout ltlt " AFTER get()
eof()" ltlt fin.eof() ltlt endl cout ltlt "end" ltlt
endl fin.close()
7
Example
  • Output

BEFORE get() eof()0 ca AFTER get()
eof()0 BEFORE get() eof()0 cb AFTER get()
eof()0 BEFORE get() eof()0 c  AFTER get()
eof()1 end
8
Explanation
  • Before each iteration, the condition in while
    loop will be evaluated
  • The loop body will be executed only when the
    condition is true, i.e. eof() 0 or false
  • While a character is successfully read from the
    file stream, the value of eof() will not change,
    even it is the last character of the file
  • ifstream will change the return value of eof()
    only when no more character can be read. In other
    words, an attempt to get a character is required

9
Explanation
  • Therefore, the character collected from get() in
    the loop body may NOT be valid
  • get() will return -1 if no more character can be
    read from the file stream
  • There are a number of ways to verify if the
    character collected from get() is valid or not
  • Add an extra eof() check after get()
  • Use the return value of get()

10
Solution 1 Extra eof() Check
ifstream fin char c fin.open("a.txt") while
(!fin.eof()) fin.get(c) if
(!fin.eof()) cout ltlt c cout ltlt "end" ltlt
endl fin.close()
11
Solution 2 Return Value of get()
ifstream fin char c fin.open("a.txt") while
(!fin.eof()) if (fin.get(c)) cout ltlt
c cout ltlt "end" ltlt endl fin.close()
Write a Comment
User Comments (0)
About PowerShow.com