Files
• A file is an external collection of related data treated as a unit.
• The primary purpose of a file is to keep a record of data
• Files are required to store data in more permanent form since the contents of primary
memory are lost when the computer is shut down.
• Files are stored in auxiliary secondary storage devices.
• Two most common forms of secondary storages are:
1. (hard disk, CD and DVD)
2. Tape
Buffer:
• A temporary storage area that holds data as the computer transfers them to/from
memory.
• The primary purpose of a buffer is to synchronize the physical devices (disk,tape) with a
program’s requirement
• The buffer also holds data until it is efficient to write that data on to the storage device.
Filename
• File name is used to read or write auxiliary storage file.
• Every operating system uses a set of rules for naming its file.
Example
abc.txt, xyz.doc etc.
File information table
• A program to read or write file needs to know several pieces of information such as
operating system name, the position of current character and so on.
• C uses a structure called FILE (defined in stdio.h) to store the attributes of a file.
Stream file processing
• A stream is an entity created by the program.
• To use a file in the program, the program’s stream name must be associated with the
operating system’s file name.
• Four steps to processing a file
1. Create the stream
2. Open the which associates the stream name with the file name
3. Process file (read or write data)
4. Close the file
Creating a Stream
• A stream is created when declared.
• To declare a stream, use the FILE type
• FILE type is structure that contains information for reading and writing a file
Syntax:
FILE *ptr;
Opening a file
• Once a stream has been created, it can be associated to a file.
• The standard open function can be used to open the file.
• The file open function returns address of the file type, which is stored in the stream
pointer variable (*ptr).
The file open function creates the stream, which can be referred to by its name.
Using the Stream Name
• The stream pointer is used in all functions that need to access the corresponding file for
input (read) or output (write)
Closing the stream :
• Close the file at the completion of file processing.
• Closing the file breaks the association between stream name and file name
• The stream is no longer available after the closing.
• Use the close function to release the file and destroy the contents of the file.
• C provides standard streams in order to communicate with a terminal
• Streams must be created and associated with their terminal devices just like files. This is
done automatically by ‘C’.
• Three stream pointers are declared and defined in the stdio.h header file.
• Stdin points to standard input stream.
• Stdout points to standard output stream.
• Stderr points to standard error stream.
Text and Binary Streams
• Data is input to and output from a stream.
• A stream can be associated with a physical device such as terminal, or with a file stored
in auxiliary memory.
• C uses two types of streams:
1) Text stream
• A text stream consists of sequence of characters divided into lines with each line
terminated by a newline character(\n)
2) Binary stream
• A binary stream consists of sequence of data values such as integer real or complex using
their memory representation.
Text and Binary File
• A file records data only in a binary format. However, a text file and a binary file are
distinguish from each other.
Text Files
• A file in which data are stored using only characters and is written using a text stream.
• Text files are read and written using input/output functions that convert the characters to
data type as show below:
Binary Files
• A Binary file is a collection of data stored in the internal format of the computer.
• Binary files are read and written using binary streams known as block input/output
functions.
• The data are stored in the file in the same format that they are stored in memory
Creating and Reading and writing text and binary files
File open (fopen)
• The file open function (fopen) serves two purposes:
1) It makes the connection between the physical file and the file stream in the program.
2) It creates a program file structure to store the information needed to process the file.
• Syntax:
fopen(“filename”, “mode”);
• First parameter is filename and second parameter is mode
• A file name is string .the name consist of eight characters and three characters for file
extension
• Ex: 1) text.txt 2) myfile.dat 3) stud.doc
• The file mode is a string that specifies the intend to use the file.
• Example
• spData = fopen(“MYFILE.DAT”, “w”);
• spData = fopen(“A:\\MYFILE.DAT”, “w”);
File open modes
• The mode shows how to use the file: for reading, for writing or for appending.
• C has three different file modes as shown in table:
File Close (fclose)
• File should be closed when no longer required to free system resources, such as buffer
space.
• Use fclose function to close a file and the pointer variable
Syntax:
fclose(spData);
File I/O functions:
getc(), putc() functions are file handling function in C programming language which is used to
read a character from a file (getc) and display on standard output or write into a file
(putc). Please find below the description and syntax for above file handling functions.
getc()/fgetc():
Declaration: intgetc(FILE *fp)
getc functions is used to read a character from a file. In a C program, we read a character as
below.
getc (fp);
putc()/fputc()
Declaration: intputc(int char, FILE *fp)
putc function is used to display a character on standard output or is used to write into a file. In a
C program, we can use putc as below.
putc(char, stdout);
putc(char, fp);
Example:
#include <stdio.h>
int main()
{
charch;
FILE *fp;
if (fp = fopen("test.c", "r"))
{
ch = getc(fp);
while (ch != EOF)
{
putc(ch, stdout);
ch = getc(fp);
}
fclose(fp);
return 0;
}
return 1;
}
Block Input/output Functions
• The block input and output functions are used to read and write data to binary files.
• The block read function is file read(fread).
• The block write function is file write(fwrite).
File Read (fread)
• fread reads a specified number of bytes from a binary file and places them into memory
at the specified location.
Declaration of fread ()
intfread ( void* pInArea,
int element Size,
int count,
FILE* sp);
• pInArea is a pointer to the input area in memory. A generic void pointer is used to allow
any pointer type to be passed to the function.
• elementSize and count are multiplied to determine how much data are to be transferred.
• The size is normally specified using the sizeof operator and the count is normally one
when reading structures.
• Operator and the count is normally one when reading structures.
• The last parameter is the associated stream.
• When fread is called, it transfers the next three integers from the file to the array, inArea.
• fread does not return end of file, it returns the number of elements read
File read (fread)
• Most common use of fread is reading structures(records).
• One advantage of block input/output functions is that one structure at a time can be
transferred.
• Second advantage is that the data need not be formatted.
Example: operation of fread when a structure is being read.
File Write (fwrite
• Writes a specified number of items to a binary file.
Declaration :
intfwrite(void* pOutArea,
intelementsize,
int count,
FILE* sp);
File Write Operation
• Fwrite returns the number of items written.
• For example ,if it writes three integers, it returns 3. The return value, therefore, can be
use to test the write operation.
• If the number of items written is fewer than count, then an error has occurred. The
program should be aborted when we a write error is encountered.
Writing a Structure
Random Access file functions
Positioning Functions
• There are three file positioning functions:
1) Rewind the file (rewind)
2) Current location (ftell)
3) Set position (fseek)
1) Rewind File (rewind) Sets the file position indicator to the beginning of the file.
Declaration: void rewind(FILE* stream);
2) Current Location (ftell) Reports the current position of the file relative to the beginning
of the file.
• If the file position indicator is at the beginning of the file, ftell returns zero.
Declaration longintftell(FILE* stream);
• ftell returns the number of bytes from the beginning of the file.
• If these is a need to know the structure number relative to the first structure, then it must
be calculated by dividing the ftell return value by the size of the structure.
numchar =ftell(sp);
numstruct=numchar/sizeof(STRUCTURE_TYPE);
• Ftell returns – 1, if an error, is encountered.
Set Position (fseek): It positions the file location indicator to a specified byte position in a
file.
Decalonintfseek(FILE* stream, long offset, int wherefrom);
• Stream is a pointer to the open file.
• offset is a signed integer that specifies the number of bytes the position indicator must
move absolutely or relatively.
• C provides three named constants that can be used to specify the starting
point(wherefrom) of the seek.
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
• If wherefrom is SEEK_SET or 0 then the offset is measured absolutely from the
beginning of the file.
• To position the file marker to the next record in a structure file, execute the following
statement:
Fseek(sp,sizeof(STRUCTURE_TYPE),SEEK_CUR);
• If where from is SEEK_CUR or 1 then the displacement is calculated relatively from the
current file position
Syntax
fseek(sp, sizeof(STRUCTURE_TYPR), SEEK_CUR);
• If wherefrom is SEEK_END or 2, the file location indicator is positioned relative to the
end of file.
Syntax
fseek(stuFile, OL, SEEK_END);