Files - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Files

Description:

Mode 'w b' or 'wb ' or 'wb' creates a new binary file for writing ... Closing the file writes the contents of the buffer to secondary storage and de ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 16
Provided by: scie263
Category:
Tags: content | files

less

Transcript and Presenter's Notes

Title: Files


1
Chapter 7
  • Files

2
File Identifiers
  • File Identifier made of name and extension
  • Name can usually be at least eight characters
    long, and usually alpha-numeric characters
  • Extension is usually up to three characters long,
    and used to group files by type
  • Some operating systems are case-sensitive

PROGRAM.C PROGRAM.EXE lab9.c
LAB9.C stdio.h stdlib.h
3
Buffered I/O
  • Secondary storage is accessed in blocks
  • So when we want to access a small part of it, we
    bring the entire block into memory
  • The block is temporarily stored in a buffer
  • ANSI-C has functions that handle buffering so
    that it is transparent to us
  • We keep track of the byte position in the file
    with a file position indicator

4
Opening Files
  • We use the FILE data type to let C know that we
    intend to use a file
  • The function fopen() prepares a file for use
  • FILE fopen(char file_id, char mode)
  • The file_id is the path, file name, and extension
  • The mode is how we intend to use it
  • The return value is the file pointer
  • FILE payfile /Declare a file
    variable/
  • payfile fopen("apayroll.txt", "r") /Open
    the file /

5
File Modes
  • File mode is a text string
  • Mode "r" opens an existing file for reading
  • File position indicator placed at beginning
  • Mode "w" creates a new file for writing
  • File position indicator placed at beginning
  • Mode "a" opens an existing file (or creates a new
    one) for appending
  • File position indicator placed at end

6
File Modes (Cont ..)
  • Mode "r" or "w" or "a" opens a file for both
    reading and writing
  • Mode "rb" or "rb" or "rb" opens an existing
    binary file for reading
  • Mode "wb" or "wb" or "wb" creates a new binary
    file for writing
  • Mode "ab" or "ab" or "ab" opens an existing
    binary file (or creates a new one) for appending

7
Closing a File
  • Closing the file writes the contents of the
    buffer to secondary storage and de-allocates
    memory for the buffer and file pointer
  • The function fclose() closes a file
  • int fclose(FILE file)
  • Files are automatically closed at normal program
    termination, including exit()
  • They are not closed when the program crashes

8
Accessing Files
  • Use fprintf() to write to a file
  • int fprintf(FILE file, char control,
    arguments)
  • Example
  • FILE inFile
  • inFile fopen(data.txt, r)
  • fprintf(inFile, "Pay .2lf\n", pay)
  • Use fscanf() to read from a file
  • int fscanf(FILE file, char control,
    arguments)
  • Example
  • fFILE outFile
  • outFile fopen(results.txt, w)
  • scanf(outFile, "lf", pay)

9
Example of File Access
include ltstdio.hgt include ltstdlib.hgt void
main(void) FILE inFile int i double
one, two inFile fopen("name.txt", "r")
for ( i1 ilt4 i) fscanf(inFile,
"lf lf", one, two) printf("lf lf\n",
one, two) fclose(inFile)
10
Single Character Access
  • Single character access from the keyboard and to
    the screen with getchar() and putchar()
  • With files it is getc() and putc()
  • int getc(FILE file)
  • int putc(int character, FILE file)
  • Both return EOF if an error occurs

11
Function Error Returns
  • Function fscanf() returns EOF if it encounters
    the end of the file
  • Function getc() returns EOF on any error
    (including end-of-file)

12
The End-of-File Function
  • Function feof() checks to see if the end-of-file
    indicator is set
  • int feof(FILE file)
  • The end-of-file indicator is usually set by an
    attempt to read at or beyond the end of the file
  • Returns a nonzero value if the end-of-file
    indicator is set (end-of-file reached), or zero
    if it is not set (end-of-file not reached)

13
Example of using EOF with fscanf()
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • void main(void)
  • FILE inFile
  • int end 1
  • double one, two
  • inFile fopen("name.txt", "r")
  • while ( end ! EOF )
  • end fscanf(inFile, "lf lf", one,
    two)
  • if (end EOF)
  • break
  • printf("lf lf\n", one, two)
  • fclose(inFile)

14
Example of feof()
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • void main(void)
  • FILE inFile
  • int end 0
  • double one, two
  • inFile fopen("name.txt", "r")
  • while ( end 0 )
  • fscanf(inFile, "lf lf", one, two)
  • printf("lf lf\n", first, last)
  • end feof(inFile)
  • if (end ! 0)
  • break
  • fclose(inFile)

15
My favorite
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • void main(void)
  • FILE inFile
  • double one, two
  • inFile fopen("name.txt", "r")
  • while ( fscanf(inFile, lf lf, one, two)
    ! EOF)
  • printf("lf lf\n", first, last)
  • fclose(inFile)
Write a Comment
User Comments (0)
About PowerShow.com