FILES - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

FILES

Description:

the programmer must declare. a new variable of type FILE. FILE *infile, *outfile; fopen ... Any structure put on a file is put there by the programmer. ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 38
Provided by: ou25
Category:
Tags: files | programmer

less

Transcript and Presenter's Notes

Title: FILES


1
FILES
  • 10.2

2
FILES IN C
  • A file is viewed as a stream
  • of bytes by a C program.
  • Any structure put on a file
  • is put there by the programmer

3
FILE
  • A file is a structure
  • in which C keeps
  • information about a file.

4
3 Files are opened
  • When a C program is executed
  • 3 Files are automatically opened
  • 1. Standard input (stdin)
  • which is usually input from the keyboard
  • 2. Standard output (stdout)
  • which is usually output to the terminal
  • 3. Standard error (stderr)
  • which is usually terminal output

5
stdio.h
  • The identifier FILE
  • is declared in the file
  • stdio.h
  • with a typedef declaration

6
stdio.h
  • The header file
  • stdio.h
  • must be included in programs
  • using external files

7
FILE
  • When an external file
  • is to be opened
  • for use in a program
  • the programmer must declare
  • a new variable of type FILE
  • FILE infile, outfile

8
fopen ( )
  • Makes a file available for use
  • by the program.
  • After the declaration, infile and outfile
  • the file needs to be connected with the file on
    the disk
  • Infile fopen ( fname, r )

9
File Modes
  • r open an existing file for reading only
  • w open a new file for writing only
  • a open a file for appending
  • (writing at the end of the file)
  • r open an existing file for update
  • (reading and writing)
  • w open (create) a new file for update
  • a open a (new or existing) file for
  • reading and appending

10
fclose ( )
  • At the end of a program execution,
  • the connection between the file
  • and the program should be broken.
  • This is done by closing the file
  • with the C library function
  • fclose ( ).
  • Example
  • fclose ( infile )

11
fclose
  • The parameter fclose ( )
  • is the FILE variable that references
  • the file to close.
  • The function fclose ( ) returns an integer.

12
(No Transcript)
13
(No Transcript)
14
Error Handling with fopen ( ) and fclose ( )
  • These library functions have facilities
  • to report errors
  • when they occur
  • while opening or closing a file

15
fopen - Error Handling
  • No error - returns pointer to FILE structure
  • error - NULL pointer returned

16
fclose - Error Handling
  • No Error - 0 returned (closed successful)
  • Error - EOF returned

17
Random versus Sequential Access in Files (10.7)
  • Sequential X------------gt
  • opened
  • reading or writing starts at beginning
  • pointer proceeds through
  • to end of file

18
RANDOM ACCESS
  • Reading of the records in any order
  • 3 ----gt 15 ----gt 1
  • fseek ( ) moves filepointer to byte position
  • fseek (fp, offset, start)

19
RANDOM ACCESS
  • ftell ( ) returns current byte offset from the
    beginning of the file
  • fseek (fp, (long) 0 , 0 )

20
(No Transcript)
21
(No Transcript)
22
(No Transcript)
23
(No Transcript)
24
(No Transcript)
25
(No Transcript)
26
Close Look at stdio.h (10.8)
  • Contains facilities for input and output
  • This file differs for each computer systems
    because the input and output library functions
    are different.

27
A file contains at least 3 members
  • 1. A member that counts the number of characters
    still to be processed before the buffer needs to
    be refreshed.

28
A file contains at least 3 members
  • 2. The address of the Buffer

29
A file contains at least 3 members
  • 3. The pointer into the current position in the
    buffer

30
Preprocessor-defined constants in stdio.h
  • define
  • EOF
  • NULL
  • BUFSIZ size of default buffer

31
Typedef declaration
  • Typedef struct
  • short count
  • number of unprocessed characters
  • cells in the buffer
  • char bufaddr
  • the address of the buffer
  • char curptr
  • pointer to the current position of the buffer

32
(No Transcript)
33
(No Transcript)
34
Things to Remember
  • 1. Input and output may be either buffered or
    line buffered.
  • 2. A file is a stream of bytes to a C program.
    Any structure put on a file is put there by the
    programmer.
  • 3. When a C program is executed, three files are
    automatically opened standard input, output and
    error.

35
Things to Remember
  • 4. The return value from a call to a library
    function usually indicates when an error had
    occurred.
  • 5. The utilities getc() and getchar() return a
    value of type int. Using type char will, at best,
    produce nonportable code.
  • 6. A FILE is a structure that C uses to keep
    track of the current position in a file. One FILE
    structure is kept up to date for every open file
    in the program.

36
Review Exam 3
  • Input and Output C 10.1 10.6
  • Files C 10.2, 10.7, 10.8
  • Group Project
  • Windows Programming

37
Group Project Proposal due this Friday
  • Format Power Point Presentation
  • Submit Floppy Disk Hard Copy
  • Present In Class
  • Who Involve all group members
  • Length 5 minutes
  • Dress Informal / Formal optional
Write a Comment
User Comments (0)
About PowerShow.com