File IO - PowerPoint PPT Presentation

About This Presentation
Title:

File IO

Description:

outs.open('outdata.dat'); // open the output file. COMP104 Lecture 20 / Slide 9 ... outs.close(); cout 'Number of lines copied: ' count endl; ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 14
Provided by: andrew184
Category:
Tags: file | outs

less

Transcript and Presenter's Notes

Title: File IO


1
  • Programming
  • File I/O

2
Using Input/Output Files (Review)
  • A computer file
  • is stored secondary storage devices (hard drive,
    CD)
  • can be used to provide input data or receive
    output data, or both
  • must be opened before using

3
Using Input/Output Files
  • stream - a sequence of characters
  • where is it from (input)?
  • where does it go (output)?
  • interactive (iostream)
  • ? cin - input stream associated with keyboard
  • ? cout - output stream associated with display
  • file (fstream)
  • ? ifstream - defines new input stream (normally
    associated with a file)
  • ? ofstream - defines new output stream (normally
    associated with a file)

4
Example
  • When you type I am Peter on keyboard, we say
    that I am Peter is an input stream (of
    characters).
  • A C program can be considered simply as a long
    stream of characters, an input stream from a
    file.
  • e.g. include ltiostream.hgt void main() cout ltlt
    Hello Mary! ltlt endl return 0

5
File-Related Functions
  • include ltfstreamgt
  • xxx.open(fname)
  • connects stream xxx to the external file fname
  • xxx.get(ch)
  • Gets the next character from the input stream xxx
    and places it in the character variable ch
  • xxx.put(ch)
  • Puts the character ch into the output stream xxx
  • xxx.eof()
  • tests for end-of-file condition
  • xxx.close()
  • disconnects the stream and closes the file

6
ltlt and gtgt Example 1
  • You can read and write integers, doubles, chars,
    etc. from files just like cin gtgt and cout ltlt
  • include ltiostreamgt
  • include ltfstreamgt
  • using namespace std
  • void main()
  • ifstream fin
  • int A4, r
  • fin.open("file1.dat") // read data file of four
    integers
  • for(r0 rlt4 r) // into array
  • fin gtgt Ar
  • fin.close()
  • ofstream fout
  • fout.open("file2.dat") // write data file
  • for(r3 rgt0 r--) // with numbers reversed
  • fout ltlt Ar ltlt ' '
  • fout.close()

7
File I/O Example 1
  • file1.dat
  • 1 2 3 4(eof)
  • file2.dat
  • 4 3 2 1 (eof)

8
File I/O Example 2
  • // Copies indata.dat to outdata.dat
  • // and counts the number of lines.
  • // Prints file to screen too.
  • include ltiostreamgt
  • include ltfstreamgt
  • using namespace std
  • void main()
  • ifstream ins
  • ofstream outs
  • int count0
  • char next
  • ins.open("indata.dat") // open the input file
  • outs.open("outdata.dat") // open the output file

9
File I/O Example 2
  • while(true) // loop for each line
  • while(true) // loop to read each char on line
  • ins.get(next)
  • if(ins.eof() next '\n')
  • break
  • cout ltlt next // echo next char on display
  • outs ltlt next // copy next char to file
  • count
  • cout ltlt endl // echo '\n' on display
  • if(ins.eof())
  • break
  • outs ltlt endl // copy '\n' to file
  • ins.close()
  • outs.close()
  • cout ltlt "Number of lines copied " ltlt count ltlt
    endl

10
File I/O Example 2
  • indata.dat
  • a b c
  • top10 methods to count spaces
  • 1 3(eof)
  • outdata.dat
  • a b c
  • top10 methods to count spaces
  • 1 3(eof)
  • Output to screen
  • a b c
  • top10 methods to count spaces
  • 1 3
  • Number of lines copied 4

11
File I/O Example 3
  • // Counts the number of blanks on each line of
    indata.dat.
  • // Outputs each line, and number of blanks on
    each line.
  • include ltiostreamgt
  • include ltfstreamgt
  • using namespace std
  • void main()
  • ifstream ins
  • int count
  • char next
  • ins.open("indata.dat") // open the file

12
File I/O Example 3
  • while(true) // loop to read each line
  • count 0
  • while(true) // loop to count spaces on
    line
  • ins.get(next) // read next character
  • if(ins.eof() next '\n') // line done?
  • break // go to next line if line done
  • cout ltlt next // otherwise output character
  • if(next' ') // increment if blank
  • count
  • cout ltlt endl
  • cout ltlt "Blanks " ltlt count ltlt endl
  • if(ins.eof()) // done?
  • break
  • ins.close()

13
File I/O Example 3
  • indata.dat
  • a b c
  • top10 methods to count spaces
  • 1 3(eof)
  • Output to screen
  • a b c
  • Blanks 2
  • top10 methods to count spaces
  • Blanks 4
  • Blanks 0
  • 1 3
  • Blanks 3
Write a Comment
User Comments (0)
About PowerShow.com