Department of Computer and Information Science, School of Science, IUPUI - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Department of Computer and Information Science, School of Science, IUPUI

Description:

Title: No Slide Title Author: Technology Last modified by: Michele Roberts Created Date: 12/2/1999 9:53:42 PM Document presentation format: On-screen Show – PowerPoint PPT presentation

Number of Views:129
Avg rating:3.0/5.0
Slides: 15
Provided by: Techn
Category:

less

Transcript and Presenter's Notes

Title: Department of Computer and Information Science, School of Science, IUPUI


1
Department of Computer and Information
Science,School of Science, IUPUI
Input/Output Streams
File IO
  • Dale Roberts, Lecturer
  • Computer Science, IUPUI
  • E-mail droberts_at_cs.iupui.edu

2
File Streams
  • Provides high level support for file operations
    in C
  • Consists of three components
  • ifstream - allows only input
  • ofstream - allows only output
  • fstream - allows both input and output

3
Files -- fstream class
  • Inherits from fstreambase and iostream classes
  • fstreambase inherits from ios class
  • iostream inherits from istream and ostream
    classes
  • ifstream inherits from fstreambase and istream
    classes
  • ofstream inherits from fstreambase and ostream
    classes

4
Class Hierarchies
ios
fstreambase
istream
ostream
ifstream
ofstream
iostream
fstream
5
File Stream Functions
  • Contains the following functions
  • open() - opens the stream
  • close() - closes the stream
  • attach() - attaches the stream to file descriptor
  • setbuf() - set the stream buffer
  • rdbuf() - returns the pointer to stream buffer
  • str() - returns a pointer to the buffer array

6
File Streams - Syntax
  • Syntax
  • fstream()
  • fstream(const char sFileName, int nMode, int
    nProt 0664)
  • fstream(filedesc fdif)

7
File Processing Modes
  • nMode can be any of the following
  • iosin - input processing
  • iosout - output processing
  • iostrunc - discard the file contents
  • iosnocreate - the file must exist in order to
    write
  • iosnoreplace - cannot rewrite to an existing
    file
  • iosbinay - open the file in binary mode
  • iosapp - append the new data to the contents of
    the file
  • iosate - open the file for output and move to
    the end
  • iosiniosout - both input and output

8
File Protection Modes
  • nProt can be one of the following
  • filbufshcompat - compatibility share mode
  • filebufsh_none - no sharing
  • filebufsh_read - read sharing
  • filebufsh_write - write sharing
  • filebufsh_readfilebufsh_write - both read
    and write sharing

9
File Streams - Example
  • fstream my_file
  • fstream myfile(cs265.txt, iosin)
  • fstream myfile(cs265.txt, iosout)
  • const int nMAX_SIZE 256 char bufnMAX_SIZE
  • filedesc fdesc my_file.fd()
  • fstream fs(fdesc)

10
File Streams - open()
  • Opens a file as a stream
  • Syntax
  • void open(const char sfile, int nMode, int nProt
    0664)
  • Usage
  • fstream my_file my_file.open(cs265.txt,
    iosout)

11
File Streams - close()
  • Close the opened file.
  • The streams error flag is cleared unless the
    close fails.
  • Syntax
  • void close()
  • Usage
  • my_file.close()

12
File I/O - Example
  • includeltiostream.hgt includeltfstream.hgt
  • main() // fIn and fOut are objects of
    class fstream fstream fIn, fOut int
    nCnt 0 //for byte count char cCh
  • //Open the file copy.in for reading
    fIn.open("copy.in", iosin)
  • //Open the file copy.out for writing
    fOut.open("copy.out", iosout)

13
File I/O Example contd
  • //Until the end of file is reached, read from
    //the file and write it to the out file and
    echo on the screen. while(fIn.get(cCh))
    fOut.put(cCh) cout.put(cCh) //echo on
    terminal nCnt
  • //Write the final byte count cout ltlt "
    " ltlt nCnt ltlt " " ltlt endl fOut ltlt nCnt
  • //Close the files fIn.close()
    fOut.close()
  • return 0

14
File I/O - Files
  • copy.in
  • abcd efg hi j
  • copy.out
  • abcd efg hi j 14
Write a Comment
User Comments (0)
About PowerShow.com