File Handling
File Handling
a = Open for appending. a+ =Open for reading & writing (append if file exists)
(for add)
getw( ) Used to read integer value from file. int variable=getw(file pointer);
n=getw(fp);
putw( ) Used to write integer value in the file. putw(int variable, file pointer);
putw(n, fp);
fprintf( ) Used to write values in the specification fprintf(file pointer, "control specification",
variable name);
format. fprintf(fp, "%s",n);
fscanf( ) Used to read values from the file. fscanf(file pointer, "Control specification",
variable name);
fscanf(fp, "%s", name);
ftell( ) Used to tell current position of file. integer variable=ftell(file pointer);
n=ftell(fp); n in long integer.
rewind( ) Reset the pointer to starting position of the rewind(file pointer);
file. rewind(fp);
fseek( ) Used to move the file pointer to a desire fseek(file pointer, offset, position);
location. fseek(fp, 10, 0);
Where, 0-beginning 1- current
2- ending.
List of file operation in File Handling:-
We can use different functions for different operation in file handling in "C":-
fclose( ) Used for closing the file. It takes file pointer as fclose(file pointer);
argument. fclose(fp);
getc( ) Used to get a read character from a file. char variable=getc(file pointer);
c=getc(fp);
putc( ) Used to write character in a file. putc(char variable, file pointer);
putc(c, fp);
fgets( ) Used to read string from file it will return fgets(char str[ ], int n, FILE*fp);
NULL.
fputs( ) Used to write string to the file. fputs(char str[ ], FILE *fp);
Example program-1:-
(WAP by using "getc" function in any file in file handling (for read & write purpose).
#include<stdio.h>
#include<conio.h>
main( )
{ file pointer
FILE *fp;
char ch; program for write mode
fp=fopen("ram.txt", "w");
printf("Input line and press EOF:\n");
while((ch=getchar( ) )!=EOF)
{
putc(ch, fp);
}
fclose(fp);
printf("OUTPUT:\n");
fp=fopen("ram.txt", "r"); program for read mode
while((ch=getc(fp))!=EOF)
{ fclose(fp);
printf("%c", ch); getch( );
} }
Example program-2:-
(WAP by using "fgetc" & "fputs“ function in character array in file handling (for read
& write purpose).
#include<stdio.h>
#include<conio.h>
main( ) file pointer
{ we can see file ram.txt
FILE *fp; in my document.
char str[20];
fp=fopen("ram.txt", "w");
fputs("This is easy for you", fp);
fclose(fp);
getch( ); for string write.
}
for no. of string.
or
fp=fopen("ram.txt", "r");
fgets(str, 10, fp);
printf("Text is : %s", str);
fclose(fp);
getch( );
}
Example program-3:-
(WAP by using "getw" & "putw" function in integer number in file handling (for read
& write purpose).
#include<stdio.h>
#include<conio.h>
main( )
{
FILE *fp;
int no, i;
fp=fopen(“ram.txt", "w");
for(i=1; i<=20; i++)
{
putw(i, fp);
}
fclose(fp);
fp=fopen("ram.txt", "r");
while(no=getw(fp) !=EOF)
{
printf("%d", no);
}
getch( );
}
Example program-4:-
(WAP by using "fscanf" & "fprintf" function in file handling (for read & write
purpose).
#include<stdio.h>
#include<conio.h>
main( )
{
FILE *fp;
int rollno;
char name[10];
fp=fopen(“ram.txt", "w");
printf("Enter roll no:");
scanf("%d", &rollno);
printf("Enter student name:");
scanf("%s", &name);
fprintf(fp, "%d%s", rollno, name);
fclose(fp);
fp=fopen(“ram.txt", "r");
fscanf(fp, "%d%s", &rollno, &name); fclose(fp);
printf("Student rollno :%d\n", rollno); getch( );
printf("Student name:%s", name); }
Introduction to Graphics:-
We can write programming codes in specially two(2) modes:-
Text Mode
Graphics Mode
Programming in graphic mode, we have to install graphic driver with *.BGI
(Borland Graphic Interface) files. After initialize the graphic drivers on the
computer, this is done using the "initgraph( )" function provides in "#include
<graphic.h>" header file in "C" library. The method "initgraph( )" has the
following prototype:-
void initgraph( int far *graphdriver, int far *graphmode,
char far *pathtodriver);
*graphdriver:-
This is an integer value that specifies the graphic driver to be used.
Normally, we use value as "0" (requests auto detect) & other values are 1 to 10
and description of each enumeration type.
*graphmode:-
This is an also integer value that specifies the initial graphic
mode(unless *graphdriver=DETECT). Then, initgraph( ) sets "*graphmode" to
the highest resolution available for the detected graphic driver.
*pathtodriver:-
It specifies the directory path where initgraph( ) looks for graphic
drivers (*.BGI) first.
If they are not here, initgtaph( ) function looks in the current directory.
If "pathtodriver" is null, the driver files must be in the current directory.
Graphic Function:-
The pre-defined header file <graphic.h> is used to include & facilitate
graphical operations in program. initgraph( ) function can be used to draw
different shpes, display text in different fonts, change colors & many more.
Using functions of <graphic.h> header file, we can make pgraphic programs,
animations projects & games. These are main graphic functions:-
initgraph( ), closegraph( ) , setcolor( ), getimage( ), putpixel( ), sector( ),
line( ), circle( ), bar( ), arc( ), ellipse( ), etc.
Graphic Mode:-
The method function "initgraph( )" initializes the graphic system by coading
the graphic driver from disk, then putting the system into graphics mode.
The method "initgraph ( )" also resets all graphics settings(color, palatte,
curent position, viewport etc.) to their defaults
In text mode, display screen divided into the 25 rows & 80 columns
in order to display text without images or alphanumeric mode.
Example program of Graphic:-
(WAP by using "getw" & "putw" function in integer number in file handling (for read
& write purpose).
WAP to draw circle, rectangle & line in graphic mode:-
#include<stdio.h> Total=640 pixel position.
#include<conio.h> 01234…………………………………..………………..639
0 100
#include<graphic.h> 1
main( ) 2
80
{ "c:\\TC\BGI"); .
200
x,y
int gd=0, gm; .
.
initgraph(&gd, &gm, " "); . For draw circle
circle(200, 100, 80); 479
rectangle(300, 50, 450, 100);
line(250, 200, 500, 350);
getch( );
50
closegraph( ); 300
100 200
} 250
350
450
500
For drawRectangle
For draw Line