Files:-
------
*
Collection of data/information is known as file.
*
File is a permanent storage medium.
* We
can store & retrive information from files.
* C
supports file handling with a set of predefined functions in
header file stdio.h
* we
can add, append, modify, delete and retrieve data with files.
1. Opening a
file ( create a file )
2. Writing
data into a file ( storing )
3. Reading
data from the file ( retreiving )
4. Close the
file after usage
5. Random
disk operations
( Direct Access method )
stdio.h
1. Open a file
To open a file we need a file pointer
FILE *fptr;
syntax
fptr
= fopen("filename","mode");
Ex:
fpt=fopen("number.data","a+");
mode of
operation
w write
r read
a append ( adding at the end of existing file
)
w+ write as well as read
r+ read as well as write (
existing file )
a+ append as well as read
4. Close the
file
fclose(fptr);
2. Writing
data into a file
write a character
putc(char,fptr)
write formatted data
fprintf(fptr,"sp.char",arg);
write a record
fwrite(&s,sizeof(s),1,fptr);
Here s is a structure variable
3. Reading
data from the file
read
a character
char = getc(fptr);
read
formatted data
fscanf(fptr,"sp.char",arg);
read
a record
fread(&s,sizeof(s),1,fptr);
File
Functions:-
---------------
feof -->
Used to Check End of file is Reached.
Return
non-Zero for Reached, Zero for Else.
Syn:-
feof(file-pointer);
ftell -->
Used to Return the Current Position of the pointer
in
the Given file.
Syn:-
ftell(file-pointer);
rewind --> Used
to set the file pointer from current position
into
beggining of the file.
Syn:-
rewind(file-pointer);
fseek -->
Used to move the file pointer from current location
into
any desired location within the file.
Syn:-
fseek(pointer,offset,Position);
Pointer --> File Pointer Variable.
offset --> Number of Position to be moved from
the
location given in Position value.
Position --> Give the starting value.
0 --> Beggining.
1 --> Current.
2 --> End
of File.
No comments: