Você está na página 1de 22

PROGRAMMING II Files

Files

Used for permanent storage of data Organization


Sequential Random Binary Text

Types

Data Hierarchy
Harry Potter 169

Angy Green
John Nick John John 01001010

234
File

Brown 789 Case 345 Record

Brown 654 Field Byte ( ACII character J)

1 bit

Files and Streams

Provide communication channel between files and programs C views files as a sequential stream of bytes When a file is opened, a stream is associated with the file

Files and Streams

When a program is executed, three files and their associated streams are opened

Standard input Standard output Standard error

File ends with an EOF marker or at a specific byte number

Files and Streams


Read C How to Program Chapter 11 File Processing Relationship between FILE pointers, FILE structures and File Control Blocks (FCBs)

File Pointer

General Format FILE * pointer-name Eg. FILE *fPtr Keyword FILE establishes a buffer area Pointer indicates beginning of area

Opening / Creating a File

fPtr = fopen(file_name, file_mode);

Example fPtr = fopen(client.dat, r); fPtr = fopen(client.dat, w); fPtr = fopen(client.dat, a);

File Modes
r w Open for reading Create for writing. If file already exists, discard contents Append. Open or create for writing at end of file Open for reading & writing Create for update. If file already exists, discard contents Append. Open or create for update at end of file

a
r+ w+ a+

Opening / Creating a File


If (fPtr == NULL)//results in error Check to determine that file was opened successfully if(fopen(file.txt,a+)==NULL) printf(Error opening file)

Binary vs text File

A text file stores line endings with the \n character though some Windows use a combination of \r and \n characters. End of file may also be treated with the Ctrl-Z character.

Binary vs Text File

Binary files

Most straightforward Assumes nothing about the OS To declare file mode to be binary include a b in the mode string e.g. wb,

Sequential Access Files

Records typically stored in order by the record key field Data stored sequentially one after the other

Sequential Access File


Example of Program while (!feof(stdin)) fscanf(writePtr, "%d%s%f",&acct_no,name,&balance); fprintf(writePtr,"%d\t%s\t%.2f\n",acct_ no,name,balance); rewind(writePtr); fclose(writePtr);

Updating Records

Example 1 Harry 3456.88 2 John 78901.34 Harry to be changed to 1 Harrington 3456.88

Risk exists if data in a sequential file is to be modified

New record has more characters than previous Characters beyond the dot would overwrite beginning of next sequential record Problem: formatted input/output using fprintf/fscanf suggests that records can vary in size Entire file is usually re-written (records before the one requiring update copied to a new file, new record written, records after one requiring update copied to new file

Random Access Files


Individual records normally fixed in length May be accessed directly without searching through other records

Exact location of record can be calculated relative to beginning of file


Data can be inserted without destroying other data

Random Access Files

Uses fwrite and fread instead of fprintf and fscanf Often write records rather than individual fields to files (hence uses structures) See sample program

Writing to Random Access Files

fwrite( void *pOutArea, int elementSize, int count, FILE* sp)

e.g. fwrite(&client1,sizeof(Acct),1,writePtr); pOutArea area in memory where data is currently elementSize how many bytes to be written to the file Count how many of elementSize to write *sp pointer to the file where the data is to be written

Reading from Random Access Files

fread( void *pIntArea, int elementSize, int count, FILE* sp)

fread(&client1, sizeof(Acct),1,writePtr); pInArea pointer to the input area in memory usually a structure elementSize is muliplied by count to determine how many data is to be transferred to memory *sp pointer to the file where the data is

Random Access Files Positioning Functions

Rewind File: rewind


void rewind(FILE* stream) Positions the file pointer to the beginning of the file.

Set Positions: fseek

int fseek(FILE *stream, long offset, int wherefrom);


e.g. fseek(writePtr, (client1.acct_no - 1) * sizeof(Acct), SEEK_SET);

FSEEK

1st parameter is a pointer to an opened file 2nd parameter specifies the number of bytes the position indicator should move (skip) relatively or absolutely (3rd parameter) 3rd parameter specifies the starting point of the seek can either be

SEEK_SET (beginning of file) SEEK_CUR (current position in file) SEEK_END (from the end of the file)

Files

Read C How to Program Chapter 11 File Processing Full menu-based (add, update, delete etc.) transaction processing program

Você também pode gostar