DISK I/O
Files :-
Files are
used for permanent data storage. File is
nothing but collection of data or information.
We need files because we know that when we will close the program all
the contents O/S the variable will be lost.
& next time we will not get those values.
In many
situations such as maintaining data of students. Etc we need to store the data
permaantly & files provide the way of storing data permanently.
Files are
stored on the dist (eg. Floppy, hard, etc) so file I/O is also known as disk
I./O. is also known as disk I/o. have a file pointer which points to that file. For creating a file pointer we use “FILE”. The general format is,
File name;
Where File
is the required key word,
·
Indicates a pointer & ptrname is a name of file
pointer.
Which can be any valid identifier.
Ex.- FILE * fp;
FILE
* Pl5*pr, *p3;
fopen ( ) –
Before performing
any operations ( such as read, write, modify, etc ) on the files we must open
that file. Without opening it we cannot
perform any operations on file.
For
openening a file we use fopen ( ) the general format of fopen ( ) is,
Fileptr =
fopen ( “filename”
,“mode”);
Where filename
is the file which we want to open.
Mode means
the type of such as read, write, append.
The filename
& mode must be in double quotes.
Modes
“r” read
“w” write
“a” append
We are
opening a file in read mode means we can only read the data from it.
For opening
the file in read mode first we must have that file already created.
When we
open the file is write mode every time a new file is created if the file
already exists then compiler will overwrite it if not, a new file will be
created.
When we
select the mode as append, the data can be added at the end of already created file. If the file doesn’t exists. Like in write
mode compiler also creates a new file in append mode
Ex- fp: fopen (“rest”, “W”)
Pt: fopen
(“mydoc”, “r”)
P1: fopen
(“Infor.Txt”, “O”,)
·
Single character I/O
1)
fputc ( ) :-
It is used for putting a character
into the file at the position given by file ptr. In other words we can write a single
character into the file using fputc ( ).
The general form of fputc ( ) is as,
fputc ( Character, fileptr );
Where character is the character
which we want to put in file.
And fileptr is the file pointer
pointing to that particular position.
Ex. Char ch = *;
fputc
( ch, fp );
fputc
( ‘A’, p1 );
In
the first example the value of ch i.e. * will be put at the position pointed by
the file ptr fp.
In
the second ex. Char ‘A’ will be put at the position pointed by P1.
·
fgetc ( ) :-
It is used for getting a char from
the file i.e. using fgetc ( ) we can input a single character at a time form
the file.
Syntax
is,
fgetc ( fileptr );
Where fileptr is the file pointer
which determines the position from where the character is to input.
fgetc
( ) gives a character so to receive it we must have character exprenion.
Ex.
fh
= fgetc (fp);
In
the above ex. The char. From the file is read & will be stored in ch.
/*fputc demo*/
#include<stdio.h>
#include<conio.h>.
void main ( )
{
FILE *fp; /*fileptr declaration */.
char ch;
cursor ( );
Ff=fopen (“test.txt”, “w”); /*
opening file */.
if (fp=NULL)
}
printf (“\n Error in openng the file
exit (0); * terminate the program */
}
printf (“\n Enter the character. 0
to stop )
while ( ‘) /*infinite loup */
{
ch = gevche ( );
fputc ( ch, fp ); /* writing to file
*/.
if ( ch = = 0)
Break;
} /End of while */.
Fclose (fp); /* closes the file */
}
/* fgetc demo*/
#include<stdio.h>
#include<conio.h>.
main ( )
{
file * fp;
char ch;
fp = fopen (“test.txt”, “r”);
if (fp == NULL)
{
printf ( “\n Error in opening files”);
exit (0);
}
printf (“)” The contents of file
are \n );
While (1)
{
ch = fgetc (fp);
If (ch= = ‘0’)
break;
putch (on);
}
fclose (fp);
}
1.
Commandline arguments –
We
know that we can pass the arguments to any user defined fun. We have not assed any arguments to the main (
). But it is also possible to pass the
arguments or parameters to main ( ).
These arguments can’t be passed in the C editor for passing arguments to
main ( ) we have to run the program from Dos prompt or Command prompt or
line. As we are passing the arguments from
command line, they are also known as command line arguments.
There are two arguments of
main ( ) one is integer & another is array of character ptrs. We can give
any valid name to these arguments, but generally we have the name as argc &
argv [ ] as,
Main ( int argc, char * argv
[10] )
Where argc stores the total
no. of arguments passed from command line. And argv stores the actual values of
the arguments.
Running a program from command
line means we have to run the exe file of that program.
/* command line
arguments */
# include
<stdio.h>
# include <
conio.h>
void main ( int
argc, char *argv [10%),
{
int i;
clescr ( );
printf (“\n total
arguments = % d”, argc);
print (“|n The
arguments are : \n”);
for (i=0,
i<argc; i++)
printf ( “\n %s”, argv [i]);
}
In the above program first we
have to make the exe, of it. Suppose we
have stored program as friend.c then the exe file will be friend. Exe. We have to run this file from command prompt
as,
Friend MANJITA SONALI SUJATA
SAYALI
Then the argc will have the
value 5 & argv will contain all these string.
Everything will be stored in
the form of string though we have given no.
·
WAP to add to no’s the no’s are given from
command line.
·
String I/O in file –
Instead of having character by character
input / output it is better to have string I/O. i.e. reading a whole string as
well as writing a whole string at a time for this we can use fgets ( ) & fputs
( )
fputsc
( )-
It is used for putting or writing the
whole string in to the file. The syntax
is
Fputs ( string, fileptr );
Where
the string is which we want to put or write in the file.
String
should specify the address of the containts which we want to put in file.
And fileptr is the file pointer pointing
to that file.
Ex.
Char name [20] = “YASH CHOPRA”
fputs
(name.fe);
The above statements writes
the name YASH CHOPRA at the location pointed by fp. Nere name specifies the
starting address of array name.
fgets ( ) –
It is used for reading a
string from the file. Here also like fputs we have to provide a string variable
& file pointer with this we have to provide the no. of characters we want
to read from the file. The syntax is,
fgets ( String,
n, fileptr );
Where a string is which we
want to get.
& n is no. of characters
we want to read from the file.
Ex.
fgets ( name, 10, fp )
The above statement will read
10 characters from the file & will store them in name.
·
WAP
1)
Writing a name in the file.
2)
Reading the same name from that file.
·
fprintf ( ) & fspnf ( ) –
Like the formatted I/O fun } printf ( )
& scanf ( ) we can also have formatted I/O fun’s for file as fscanf ( )
& fprintf ( ).
fscanf
( ) –
It is used for reading or input the values
from file.
It is similar to scanf ( ) statement here
the input values will be taken from the file.
The syntax is,
fscanf ( fileptr, control string, list of
arg );
Where fileptr is the file pointer which
points to the file from which we want to input the values.
Control string is the string having format
specifiers.
List of arguments is the different
arguments in which use we want to store the values.
Ex.
Int rno; char name [20];
fscanf (fp, “%d%s”, & rno, name );
·
fprintf ( )
If is used for writing or putting the data
into the file the syntax is,
fprintf ( fileptr, control string, list of
arg );
Ex.
Int rno = 20;
Char name [20] = “JAGRUTI”;
fprintf (fp, “%d%S”, rno, name );
The above statement writs the value 20
& JAGRUTI in the file pointed by fp.
/*PROGRAME
FOR fprint ( ) demo */
#include
<stdio.h>
#include<conio.h>
void
main ( )
{
ink
rno;
char
name [20];
FILE
* fp; /* file ptr decl^ */
Clrsor
( );
fp
= fopen (“testr.txt”, “w”); /*opening file in write mod /.
if
(fp = = NULL )
{printf ( “\n Error in opening the file”);
exit (0); /* terminates the program */
}
printf
(“\n Enter roll no & name : ‘);
scanf
(“%d%s”, & rno, name);
fprintf
(fp, “%d%s”, rno. Name );
/*writing
to file */
fclose
(fp); closes the file */
}
/*PROGRAM
FOR fscanf ( ) demo ^/
#
include <stdio.h>
#I
include <conio.h>
void
main ( )
{
int
rno;
char
name [20];
FILE
*fp;
Clrscr
( );
fp
= fopen ( “test2.txt”, “?”);
if
(fp = = NULL)
{printf
(“\n Error in opening the file”);
exit
(0);
}
fscanf
(fp, “%d%s”, &r no, name);
printf
(“\n Roll no : % d \n Name: % s” , rno, name);
fclose
(fp);
}
fwrite
( ) –
It is used for writing a fixed block of
data into the file. It is mainly used
when we want to write the data from structures into file. The general format of furite( ) is,
Where address specifies the location from
where the data stored.
Which we want to write in file.
Size specifies the size of every block.
N specifies the no. of blocks of the size.
Fileptr is the file pointer pointing to
that file where we want to write the data.
Ex
1) struct student S;
Fwrite ( &s, size of (struct student,
1, fp);
3)
int a[10];
fwrite
(a, size of (int), 10, fp1);
The first example writes one
block of size equal to the size of student structures at the location & s
in the file pointed by fp.
The second example writes ten
blocks of size equal to the size of into at the location a in the file pointed
by fp1.
fread ( )-
fread ( ) is used for reading
the fixed block of data from the file. If
we want to use fread ( ) for reading the data from file, the file shold be
created by using fwrite ( ) otherwise we may get unwanted results.
The syntax is, similar to furite
( ) as,
fread ( address, size, n, fileptr
);
Ex.
struct student s;
fread ( & s, size of
(struct student), 1, fp );
The above statements reads one
block of size equal to size of structure student from the file pointed by fp
& stores the data at the location pointed by &s.