Fundamental File Processing Operations - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Fundamental File Processing Operations

Description:

Fundamental File Processing Operations. Physical and logical files. Physical files. name, location ... int _read( int handle, void *buffer, unsigned int count ) ... – PowerPoint PPT presentation

Number of Views:558
Avg rating:3.0/5.0
Slides: 24
Provided by: csSung
Category:

less

Transcript and Presenter's Notes

Title: Fundamental File Processing Operations


1
Fundamental File Processing Operations
2
Physical and logical files
  • Physical files
  • name, location (set of bytes in a disk)
  • Logical files
  • handle (descriptor), pointer (variables in a
    program)

FILE fp int fd fp fopen(fname, r) fd
_open(fname, _O_WRONLY )
3
Physical and Logical Files
File Handle
Program
Name Access Mode
File Descriptor Tbl
OS
Location other info.
File Allocation TBL
Disk
..
block
4
File Operations
  • Open
  • Close
  • Read
  • Write
  • Seek

5
Open a File
  • Makes it ready for use in a program
  • two options
  • open an existing file
  • create a new file (deleting any existing
    contents)
  • Position at the beginning of the file

6
Close a File
  • Releases the logical file for reuse
  • flushes buffers that have been modified since the
    file opened

OS
DISK
Program
buffer
block
variable
7
Reading and Writing
  • Low level I/O
  • ltio.hgt
  • High level I/O
  • C Streams, ltstdio.hgt
  • C Streams, ltiostream.hgt, ltfstream.hgt

8
Low Level I/O
  • int _read( int handle, void buffer, unsigned int
    count )
  • int _write( int handle, const void buffer,
    unsigned int count )
  • handle Handle of file into which data is written
  • buffer Data to be written
  • count Number of bytes

9
C Streams
  • Stream a file or some other source or consumer
    of data
  • fopen, fclose
  • fread, fwrite, fget, fput, fscanf, fprintf
  • stdin, stdout, stderr
  • printf, scanf

10
C Streams
  • fstream public iostream
  • cin, cout
  • predefined stream objects
  • ios
  • in, out, nocreate, noreplace,
  • binary (access mode)
  • ltlt (output), gtgt (input)
  • see p23 and Appendix C

11
C stream example
  • // listc.cpp
  • // program using C streams to read characters
    from a file
  • // and write them to the terminal screen
  • include ltstdio.hgt
  • main( )
  • char ch FILE file // file descriptor
  • char filename20
  • printf("Enter the name of the file ") // Step
    1
  • gets(filename) // Step 2
  • file fopen(filename, "r") // Step 3
  • while (fread(ch, 1, 1, file) ! 0) // Step
    4a
  • fwrite(ch, 1, 1, stdout) // Step 4b
  • fclose(file) // Step 5

12
C Stream Example
  • // listcpp.cpp
  • // list contents of file using C stream classes
  • include ltfstream.hgt
  • void main ()
  • char ch fstream file // declare fstream
    unattached
  • char filename20
  • cout ltlt"Enter the name of the file " // Step 1
  • ltltflush // force output
  • cin gtgt filename // Step 2
  • file . open(filename, iosin) // Step 3
  • file . unsetf (iosskipws)// include white
    space in read

while (1) file gtgt ch // Step 4a if
(file.fail()) break cout ltlt ch // Step 4b
file . close() // Step 5
13
Detecting EOF
  • In C streams
  • fread returns the number of full items actually
    read, which may be less than count if an error
    occurs or if the end of the file is encountered
    before reaching count. Use the feof or ferror
    function to distinguish a read error from an
    end-of-file condition. If size or count is 0,
    fread returns 0 and the buffer contents are
    unchanged.
  • In C streams
  • eof() or fail()

14
Seeking
  • Seeking, Search, Retrieval, Matching
  • move to a certain position in a file

When opening
r/w pointer
EOF
15
Seeking with C Streams
  • int fseek( FILE stream, long offset, int origin
    )
  • long ftell( FILE stream)
  • offset byte offset
  • origin SEEK_SET, SEEK_CUR, SEEK_END
  • to view a file as a very large array of bytes

16
Seeking with C streams
  • Two file pointers
  • get pointer
  • put pointer
  • file.seekg( byte_offset, origin)
  • origin iosbeg, ioscur, iosend

17
Special Characters
  • CTR-Z EOF
  • CR-LF, LF EOL
  • binary file, text file

18
Directory Structure
  • Directory
  • a tree-structured organization of files
  • absolute pathname
  • relative pathname
  • current directory
  • special directory name
  • . current
  • .. parent

19
Physical devices files
  • A file
  • a sequence of bytes without any implementation of
    how or where they are stored, or where they
    originate

keyborad
disk
Handles (indexes of file description table)
tape
Files (streams)
console
network
20
Standard I/O devices
  • stdin
  • stdout
  • stderr

21
I/O redirection pipe
  • lt input redirection
  • gt output redirection (gtgt)
  • gt error redirection
  • pipe
  • ls wc

22
File-related Header files
  • stdio.h C stream
  • iostream.h, fstream.h C stream
  • fcntl.h, file.h file operations

23
HW 2
  • Ex 3, 6, 7, 8, 9, 10
  • Programming
  • Ex 14, 15.
Write a Comment
User Comments (0)
About PowerShow.com