C Programming - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

C Programming

Description:

contain printable characters and control characters. organized into lines. system may convert or remove some input characters ... Contain a series of characters ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 16
Provided by: Kenn194
Category:

less

Transcript and Presenter's Notes

Title: C Programming


1
C Programming
  • File Processing
  • Part II

2
Text processing
  • A file can be opened and processed as either text
    file or binary file
  • Text files
  • contain printable characters and control
    characters
  • organized into lines
  • system may convert or remove some input
    characters
  • system may insert or convert some output
    characters

3
Binary Files
  • Contain a series of characters that require no
    translation
  • Binary files are very similar to arrays of
    structures, except the structures are in a disk
    file rather than in an array in memory.
  • Binary files have two features that distinguish
    them from text files
  • You can jump instantly to any structure in the
    file, which provides random access as in an
    array.
  • You can change the contents of a structure
    anywhere in the file at any time.
  • Binary files also usually have faster read and
    write times than text files, because a binary
    image of the record is stored directly from
    memory to disk (or vice versa).
  • In a text file, everything has to be converted
    back and forth to text, and this takes time.

4
fopen
  • fopen mode determines if file is handled as text
    or binary
  • add "b" to mode for binary processing
  • fp2 fopen( fileb, "rb" )
  • omit "b" for text processing
  • fp1 fopen( filea, "r" )
  • Same input file can be processed as text or
    binary, but characters read may be different
  • Use binary when you want no translation to be
    done

5
Record processing, fread
  • fread function reads up to a requested number of
    items of a specified size
  • Format
  • size_t fread( buffer, size, count, fp )
  • Returns the number of items actually read, if
    less than requested must use feof or ferror
    function to determine if end-of-file or error
    occurred
  • buffer
  • character array to receive the input
  • size
  • size in bytes of each item
  • count
  • maximum number of items to be read
  • fp
  • file pointer to the input file

6
fread example
  • char cal80, flo5100
  • file filea
  • filea fopen( "...", "rb" )
  • ...
  • / read one 80-byte record /
  • i fread( cal, 80, 1, filea )
  • ...
  • / read five 100-byte records /
  • i fread( flo, 5, 100, filea )

7
Record processing, fwrite
  • fwrite function writes a requested number of
    items of a specified size
  • Format
  • size_t fwrite( buffer, size, count, fp )
  • Returns the number of items actually written, if
    less than requested error occurred
  • buffer
  • character array containing data to be written
  • size
  • size in bytes of each item
  • count
  • number of items to write
  • fp
  • file pointer to the open output file

8
fwrite example
  • char cal80, flo5100
  • file filea
  • fileb fopen( "...", "wb" )
  • ...build data in buffers...
  • / write one 80-byte record /
  • i fwrite( cal, 80, 1, fileb )
  • ...
  • / write five 100-byte records /
  • i fwrite( flo, 5, 100, fileb )

9
File position
  • Bytes in file numbered from zero for first byte
  • File position is where the next input or output
    operation will take place
  • File position advances with each byte read or
    written
  • ftell function returns current file position
  • fseek function sets file position for next input
    or output
  • Files opened in append mode always write at end
    of file regardless of previous file position

10
  • fseek, set file position
  • fseek function sets file position for next input
    or output operation
  • Format int fseek( fileptr, offset, origin )
  • Returns zero if successful, not zero otherwise
  • fileptr
  • open file pointer
  • offset
  • signed long integer added to determine new
    position
  • origin
  • constant specify how offset is to be used to
    determine new position, SEEK_SET
  • offset is relative to beginning of file (should
    be positive)
  • SEEK_CUR
  • offset is relative to current position ( or -)
  • SEEK_END
  • offset is relative to end of file ( or -)
  • Use to randomly read file
  • Use to write to desired slot
  • Use to reposition to remembered place (from index
    or checkpoint)
  • Use to browse file forward and backward

11
  • Set file pointer from beginning, SEEK_SET
  • SEEK_SET sets file position from the beginning of
    the file
  • Example
  • long newp
  • FILE filea
  • char joe4
  • filea fopen( ... , "rb" )
  • ...
  • newp 16175
  • fseek( filea, newp, SEEK_SET )
  • fscanf( filea, "3c", joe )

12
Set file pointer from current, SEEK_CUR
  • SEEK_CUR sets the new position relative to the
    current file position. The offset can be positive
    to move forward, or negative to move backward.
  • long newp
  • FILE filea
  • char sam5
  • ...
  • filea fopen( ... , "rb" )
  • ...
  • newp -8
  • fseek( filea, newp, SEEK_CUR )
  • fread( sam, 1, 4, filea )

13
Set file pointer from end, SEEK_END
  • The following example sets the file position and
    reads the last 8 bytes of the file.
  • long newp
  • FILE filea
  • char sam9
  • ...
  • filea fopen( ... , "rb" )
  • ...
  • newp 7
  • fseek( filea, newp, SEEK_END )
  • fread( sam, 1, 8, filea )

14
  • include ltstdio.hgt
  • struct rec
  • int x,y,z
  • int main()
  • int i,j
  • FILE f
  • struct rec r
  • ffopen("junk","w")
  • if (!f)
  • return 1
  • for (i1ilt10 i)
  • r.xi
  • fwrite(r,sizeof(struct rec),1,f)

ffopen("junk","r") if (!f)
return 1 for (i1ilt10 i)
fread(r,sizeof(struct rec),1,f)
printf("d\n",r.x) fclose(f)
printf("\n")
15
  • ffopen("junk","r")
  • if (!f)
  • return 1
  • for (i9 igt0 i--)
  • fseek(f,sizeof(struct rec)i,SEEK_SET)
  • fread(r,sizeof(struct rec),1,f)
  • printf("d\n",r.x)
  • fclose(f)
  • printf("\n")
Write a Comment
User Comments (0)
About PowerShow.com