L21 Fileinputoutput
L21 Fileinputoutput
• Data files
– Can be created, updated, and processed by C
programs
– Are used for permanent storage of large
amounts of data
• Storage of data in variables and arrays is only
temporary
The Data Hierarchy
• Data Hierarchy:
– Bit – smallest data item
• Value of 0 or 1
– Byte – 8 bits
• Used to store a character
– Decimal digits, letters, and special symbols
– Field – group of characters conveying meaning
• Example: your name
– Record – group of related fields
• Represented by a struct or a class
• Example: In a payroll system, a record for a particular
employee that contained his/her identification number, name,
address, etc.
The Data Hierarchy
• Data Hierarchy (continued):
– File – group of related records
• Example: payroll file
– Database – group of related files
Sally Black
Tom Blue
Judy Green File
Iris Orange
Randy Red
1 Bit
The Data Hierarchy
• Data files
– Record key
• Identifies a record to facilitate the retrieval of
specific records from a file
– Sequential file
• Records typically sorted by key
Files and Streams
• C views each file as a sequence of bytes
– File ends with the end-of-file marker
• Or, file ends at a specified byte
• Stream created when a file is opened
– Provide communication channel between files and
programs
– Opening a file returns a pointer to a FILE
structure
• Example file pointers:
• stdin - standard input (keyboard)
• stdout - standard output (screen)
• stderr - standard error (screen)
Files and Streams
• Read/Write functions in standard library
– fgetc
• Reads one character from a file
• Takes a FILE pointer as an argument
• fgetc( stdin ) equivalent to getchar()
– fputc
• Writes one character to a file
• Takes a FILE pointer and a character to write as an argument
• fputc( 'a', stdout ) equivalent to putchar( 'a' )
– fgets
• Reads a line from a file
– fputs
• Writes a line to a file
– fscanf / fprintf
• File processing equivalents of scanf and printf
Creating a Sequential Access File
• C imposes no file structure
– No notion of records in a file
– Programmer must provide file structure
• Creating a File
– FILE *myPtr;
• Creates a FILE pointer called myPtr
– myPtr = fopen("myFile.dat",
openmode);
• Function fopen returns a FILE pointer to file specified
• Takes two arguments – file to open and file open mode
• If open fails, NULL returned
– fprintf
• Used to print to a file
• Like printf, except first argument is a FILE pointer (pointer to
the file you want to print in)
Creating a Sequential Access
File
– feof( FILE pointer )
• Returns true if end-of-file indicator (no more data to process) is
set for the specified file
– fclose( FILE pointer )
• Closes specified file
• Performed automatically when program ends
• Good practice to close files explicitly
• Details
– Programs may process no files, one file, or many files
– Each file must have a unique name and should have its
own pointer
Creating a Sequential Access
File
? 3
}byte offsets
}
}
}
}
}
The fptr is at 0
---Leading me along
The fptr is at 18
---my shadow goes back home
The fptr is at 44
---from looking at the moon.
Re-read the test:
---from looking at the moon.
---my shadow goes back home
---Leading me along
The rewind() Function
• Sometimes you might want to reset the file
position indicator and put it at the beginning
of a file. There is a handy C function, called
rewind(), that can be used to rewind the file
position indicator.
• The syntax for the rewind() function is
void rewind(FILE *stream);
• rewind(fptr); is equivalent to this:
(void)fseek(fptr, 0L, SEEK_SET);
More Examples of Disk File I/O
#include <stdio.h>
enum {SUCCESS, FAIL, MAX_NUM = 3};