Introduction of files : A file is a place on to the disc where a group of
related data is stored. ‘C’ supports
a no.of
functions that data is stored. The ability to perform basic file which
include
Naming a file
Opining a file
Reading data from a file
Writing data from a file and
closing a file
Defining and opening a file : If we want to store data in a file in the
secondary memory specify certain things about the file to the operating system
they include
1. File name
2. Data structure
3. Purpose
File name is a string of
characters that makeup a vaild for the operating system. Data structure of a file is defined as ‘File’ in the library of standard I/O function definition .
File is a
defined data type.
File operations :
Fopen function: The general format for declaring and opening
la file is
FILE *fp;
Fp = fopen (
“file name”, “mode” );
Where the variable fp has a pointer to the data type File opens the File name a and pointer assigns an identifier
to the File type pointer fp.
This pointer which contains all the information about the File
subsequently used as a communication link between the system used as a
communication between the system and the program.
“Mode” can be one the
following
r - open the file for reading only.
w -
open the file writing only.
a - open the file for appending (adding) data
to each.
Both ‘file name’ and ‘mode’ are specified as string
they should be enclosed in ‘’ ‘’.
Ex : FILE * fp1, *fp2;
Fp1 = fopen (“data’’, “r”);
Fp2= fopen (“result”, “w”);
Where fp1,fp2 are pointer the ‘fp1’ is opens
the File named as data for reading
mode only then fp2 opens the file named as ‘result for writing made only
Fclose function : A File must be closed after all operations have
been completed. The general form of fclose function is
Fclose (file
pointer);
Ex : Fp1 =fopen (“input”,
"W”);
Fp2 = fopen(“output”, “r”);
Fclose(fp1);
Fclose(fp2);
This program opens two files
and closes them after all operations on them are completed once a file is
closed its file pointer can be reused for another file.
The getc function : It is used to read a character from a file that has
been opened in read mode the general form of statement is
C=getc (file
pointer);
Here read a character from
the file whose pointer is file pointer and assigned the reading character to
‘C’. The reading is terminated when getc encounter end of file mark Eof.
Ex : File *fp;
Char c;
Fp=fopen
(“input”, “r”);
While(c=getc(fp)!=Eof)
Putchar ( c );
The putc function : The another
simple I/o function is putc. putc is used to write a character to a file that has been opened in write mode.
Putc (c,fp);
Where read a character
through to the variable and put these character through whose file pointer is
fp the writing is terminated when putc encounters the end of file mark EOF.
Ex : FILE *fp;
Char C;
Fp=fopen(“input”, “w”);
While (c = getchar( c )!=
EOF)
Putc(c, fp);
The getw function : The simplest
integer oriented file I/o function is get
w that has been opened in read
mode the general from of statement is
Num =getw (fp);
Where read an integer value from the file
whose file pointer is fp and assigned the reading numbers to num. The reading
is terminated when getw encounters the end of file mark EOF.
Ex : FILE
*fp
Int num;
Fp=fopen
(“input”, "r”);
While (num=getw(fp)!=EOF)
Printf(“%d”,
num);
The Putw function : The simplest
I/O integer oriented function is putw. ‘putw’ is used to create an integer
value to a file that has been opened in write mode. The general form statement is
Putw(num,fp);
Where read a number through
to the variable ‘num’ and put the number into the file whose file pointer is
fp. The waiting is terminted when ‘Putw’ encounters the end of file mark Eof
(i.e,num=0)
Ex : FILE *fp
Int num;
Fp=fopen (“INPUT”,
"w”);
Scanf(“%d”, & num);
While (num!= fp)
Putw(num,fp);
Scanf(“%d”, & num);
The fprintf function : The fprintf statement is used to write data items
to the file whose file pointer is opened in the writing mode. ‘fprintf’ perform
I/O operations an files the general
form of fprintf is
Fprintf (fp, "control
string", list);
Where ‘fp’ is a file pointer associated with a
file that has been opened for writing. The control string contains output
specifications for the items in the list. The list may be including variable
constants and strings.
Ex : fprintf (fp1, "%s %d %f”, name age,7.5);
Where ‘name is an array
variable of type and age is an int variable.
The fscanf function : The fscanf
function is used to read data items to the file whose pointer is opened in the
reading mode. fscanf function performs I/O operations on files. The general
form of fscanf is
fscanf (fp,
“control string”, list);
This statement reading of
the items in the list from the file specified by fp. The control string
contains input specifications for the items in the list.
Ex : fscanf (fp1,
“%s %d”, item & quantity
Where “item “ is an array
variable of type and quanity is an int
variable. Fscanf returns the no.of items when the end of file is reached 'i'
returns the value Eof
The feof function : This function can be used to test for an end of
file condition It takes a file pointer as an argument and returns a non-zero
integer value id all of the data from the specified file has been read and
returns zero otherwise.
If (feof (fp)
Printf(“end of file”);
This statement gives the end of data when
encountered end of file condition
The ferror function : It is also
takes a file pointer its argument and returns a non-zero integer if an error
has been detected upto that point during processing it returns zero otherwise.
Ex : The statement If
(ferror (fp)!=0)
Printf(“file
could not be open”);
Print the error message if
the reading is not successful.
The ftell function : This function is useful in saving the current
position of a file, which can be used later in the program . It takes the
following form
N
= ftell ( fp);
Where ‘n’ gives the relative
offset (in bytes) of the current position. This means that ‘n’ bytes have
already read.
The Rewind function : It takes a file pointer and
resets the position to the start of the (in bytes). The statement is
Rewind (fp);
N = ftell(fp);
It will assign ‘o’ to n’
because the file position has been set to the start of the file by
rewinded. i.e.., The first byte in the
file is numbered as ‘o’ second as ‘1’ and a so on.
Fseek function : It is used to move
the file position to a desired
location within the file It takes the following form
Fseek (file
pointer, offset, position);
Here ‘file pointer’ is a
pointer to the file, ‘offset’ is a number or variable of type ‘long’, and
position is an integer variable. The offset specifies the positions (bytes) to
be moved from the location specified by position.
Value Meaning
0
beginning of the file
1
current position
2
end of the file
The
Offset may be ‘+’ve meaning moved forwards, or ‘-‘ ve meaning moved backwards.
Ex :
Fseek(fp,oL,0) goto beginning
Fseek(fp,m,0) move (n+1)th byte in the file
Fseek (fp,m,1) goto forward by ‘m’ bytes
Fseek (fp,-m,1) goto backward by ‘m’ bytes from the
current position
Fseek (fp ,-m,2) goto backward by ‘m’ bytes from the
end.
we should implement that in practically then only we can understand that exact thing clearly, but it’s no need to do it, because you have explained the concepts very well.
ReplyDeleteDocument Management Software India
Document Management Software Chennai
Document Management Software
Electronic Document Management System