You are on page 1of 11

Types of file

1.Text file
2.Binary file
1. Text file:

A Text file is a file in which data are stored using only


characters. A text file is written using a text stream.

2.Binary file:

Binary file is a collection of data stored in the internal format


of the computer.
File handling functions(file operations:

c language has 8 different categories of file


Handling functions.
1.File close\open
2.character input\output
3.Formatted input\output
4.Line input\output
5.Block input\output
6.File positioning function\ random access functions
7.System file operations
8.File status functions.
File open/close functions:
a.fopen( ):
fopen( ) is used to open a file
Prototype:
File *fopen(const char * filename, count char *mode);
b.fclose( )
fclose ( ) is used to close an opened file
Prototype:
int fclose(const char *filename);
Random access functions:

For randomly processing data in disk files, we need to


set file pointer position to the desired\required position.
File positioning functions are:
rewind( )
ftell( )
fseek( )
rewind( ):
The rewind ( ) will set the file position to the beginning of the
file .
Prototype:
void rewind(FILE *stream);

ftell( ):
ftell will give the current position/location of the file, relative to
the beginning of the file.
Prototype:
long int ftell(FILE *stream);
fseek( ):
fseek ( ) will move the file pointer to the required position in a
file.
Prototype:
llong int ftell(FILE *stream, long offset ,int fromcohere);

Block input /output functions:


C language uses the block input and output functions to read and
write binary files, the data are transferred just as they found in
memory.
File read-fread:
file read –fread reads a specified number of bytes from a
binary file and places them into a memory at the specified
location.

int fread(void *PInArea, int element size, int count, FILE *sp);
PInArea is a pointer to the input area in memory.
File write -fwrite:
Fwrite writes a specified number of items to a binary file.

int fwrite(void *PoutArea,int element Size, int count,


FILE *sp);
File status functions:
c provides three functions to handles file status questions test
end of file(feof), test error (ferror) and clear error
(clearerr)

feof( ): is used to check if the end of file has been reached. If the
file ia at the end that is if all data have read the function returns
non zero(true), if end of file has not been reduced zero (false) is
returned.

int (false) (FILE *stream);


ferror( ):is used to check the error status of the file-returns
tree(non zero) if an error has occurred, otherwise false
(if no error has occurred )

int-ferror(FILE *Stream);

clearerr( ):When an error occurs, the subsequent calls to ferror


return non zero, until the error status of the file is reset the
function clearerr is used for this purpose.

void clearerr(FILE *Stream);


System file operations:
few functions operate on the whole file instead of the contents.

These functions generally use Os calls to perform operations


such as remove a file, rename a file or create a temporary file.

1.remove – int remove(char *filename);


2. rename – int rename (const char *old file name, const char
*NFN);
3. creating temporary file.
FILE *tmp file (void);

Ex: FILE *sp;


sp=tmpfile( );

You might also like