lecture11 files
TRANSCRIPT
-
7/28/2019 Lecture11 Files
1/22
PROGRAMMING II
Files
-
7/28/2019 Lecture11 Files
2/22
Files Used for permanent storage of data
Organization
Sequential
Random
Types
Binary
Text
-
7/28/2019 Lecture11 Files
3/22
Angy Green 234
John Brown 789
Nick Case 345
John Brown 654
Harry Potter 169
File
Record
J o h n Field
01001010 Byte ( ACII character J)
1 bit
Data Hierarchy
-
7/28/2019 Lecture11 Files
4/22
Files and Streams Provide communication channel
between files and programs
C views files as a sequential stream ofbytes
When a file is opened, a stream isassociated with the file
-
7/28/2019 Lecture11 Files
5/22
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 aspecific byte number
-
7/28/2019 Lecture11 Files
6/22
Files and Streams Read C How to Program
Chapter 11 File Processing
Relationship between FILE pointers,FILE structures and File Control Blocks(FCBs)
-
7/28/2019 Lecture11 Files
7/22
File Pointer General Format
FILE * pointer-name
Eg. FILE *fPtr
Keyword FILE establishes a buffer area
Pointer indicates beginning of area
-
7/28/2019 Lecture11 Files
8/22
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);
-
7/28/2019 Lecture11 Files
9/22
File Modesr Open for readingw Create for writing. If file already
exists, discard contentsa Append. Open or create for
writing at end of file
r+ Open for reading & writing
w+ Create for update. If file alreadyexists, discard contents
a+ Append. Open or create for
update at end of file
-
7/28/2019 Lecture11 Files
10/22
Opening / Creating a File If (fPtr == NULL)//results in error
Check to determine that file wasopened successfully
if(fopen(file.txt,a+)==NULL)
printf(Error opening file)
-
7/28/2019 Lecture11 Files
11/22
Binary vs text FileA 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 theCtrl-Z character.
-
7/28/2019 Lecture11 Files
12/22
Binary vs Text File Binary files
Most straightforward
Assumes nothing about the OS
To declare file mode to be binary include abin the mode string e.g. wb,
-
7/28/2019 Lecture11 Files
13/22
Sequential Access Files Records typically stored in order by the
record key field
Data stored sequentially one after theother
-
7/28/2019 Lecture11 Files
14/22
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);
http://localhost/var/www/apps/conversion/tmp/scratch_2/lec11_seq_file.chttp://localhost/var/www/apps/conversion/tmp/scratch_2/lec11_seq_file.c -
7/28/2019 Lecture11 Files
15/22
Updating Records Risk exists if data in a sequential file is to be modified Example
1 Harry 3456.88
2 John 78901.34Harry to be changed to1 Harrington 3456.88
New record has more characters than previous
Characters beyond the dot would overwrite beginning of next sequentialrecord
Problem: formatted input/output using fprintf/fscanf suggests that recordscan 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 updatecopied to new file
-
7/28/2019 Lecture11 Files
16/22
Random Access Files
Individual records normally fixed in length
May be accessed directly without searching
through other records
Exact location of record can be calculatedrelative to beginning of file
Data can be inserted without destroying otherdata
-
7/28/2019 Lecture11 Files
17/22
Random Access Files
Uses fwrite and fread instead of fprintfand fscanf
Often write records rather thanindividual fields to files (hence uses structures)
See sample program
http://localhost/var/www/apps/conversion/tmp/scratch_2/lec11_rand_file.chttp://localhost/var/www/apps/conversion/tmp/scratch_2/lec11_rand_file.c -
7/28/2019 Lecture11 Files
18/22
Writing to Random AccessFiles
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
-
7/28/2019 Lecture11 Files
19/22
Reading from Random AccessFiles
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 isto be transferred to memory
*sp pointer to the file where the data is
-
7/28/2019 Lecture11 Files
20/22
Random Access FilesPositioning Functions
Rewind File: rewind
void rewind(FILE* stream)
Positions the file pointer to the beginning ofthe file.
Set Positions: fseek int fseek(FILE *stream, long offset, int
wherefrom);
e.g. fseek(writePtr, (client1.acct_no - 1) * sizeof(Acct), SEEK_SET);
-
7/28/2019 Lecture11 Files
21/22
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 ofthe seek can either be
SEEK_SET (beginning of file)
SEEK_CUR (current position in file)
SEEK_END (from the end of the file)
-
7/28/2019 Lecture11 Files
22/22
Files
Read C How to Program
Chapter 11 File Processing
Full menu-based (add, update, deleteetc.) transaction processing program