University of California, Santa Cruz

Prof. Paulo Franca - Spring 2000

Introduction

Syllabus

Guidelines for labs

Lab sections

Take quiz & check grades

An ADT to read from files with fixed record sizes

Purpose:

Often you will have to deal with files where each record in the file has the same, fixed size structure. This will be the case during this course. This ADT will be given to you both as an example of ADT implementation and as a helpful tool to speed up your programming assignments.

See the interface (fixfile.h) file

bulletdownload the interface file fixfile.h
bulletdownload the program file fixfile.c

Functionality:

Use this pointer type to refer to the FixFile ADT

typedef struct FixFile *ptfile;

The following functions operate with the ADT FixFile:

FileCreate: Creates a new ADT and links it to a specific file in the disk

usage: ptfile FileCreate(filename char[],int recordsize,char mode[]);

filename is an array of char containing the name of the file to be used. If the file is not in the same directory, you will need to specify the path to it.

recordsize is the size (fixed) of the records in your program. It is recommended that you to not use a number but use sizeof( nameofstructure) instead.

mode determines if you want to use the file for reading ("r") or writing ("w"). You cannot use both at the same time.

the function returns a pointer to a FixFile. This pointer should be kept and used anytime that you need to refer to the same file.

FileGetRecord: Reads the next available record from the file, positions the file for reading the following record.

usage: void FileGetRecord(ptfile file,void *pt_record);

file is the pointer to the FixFile structure that you want to use. You must have obtained a value for this pointer by calling FileCreate.

pt_record is a pointer to a structure that describes your record. You must cast this pointer to a void in order to call this function. The contents of the next record in the file will be read into the memory location pointed by pt_record. No formatting will take place.

FileEOF: Indicates if an End of File condition has occured.

usage: int FileEOF(ptfile file);

file is a pointer to the FixFile that you are using. The function returns 1 if EOF is detected and a 0 otherwise. Keep in mind that EOF does not occur when you read the last record, but instead only when you try to read AFTER the last record in the file.

FilePutFile: Lets you write a record to the file.

usage:

void FilePutRecord(ptfile adtpointer,void *recpointer)

usage is similar to FileGetRecord. FileCreate must have been called with mode "w" or "a".

FileEOF: detects end of file condition

usage: int FileEOF(ptfile adtpointer);

this function returns a 1 if an end of file condition is detected. Note that end of file does not happen when you read the last record. It happens when you try to read AFTER the last record.

FileClose: Closes the file and deallocates memory for the ADT.

usage: void FileClose(ptfile adtpointer);