0% found this document useful (0 votes)
29 views11 pages

File Handling

Uploaded by

gsg9732
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views11 pages

File Handling

Uploaded by

gsg9732
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

File Handling:-

File:- Storage of data or any kind of information. (Data Bundle)


Here, Data- Any information.
Data are stored in primary memory (RAM) & permanently in secondary
memory(HD, Floppy, Disk Drive, Pen Drive etc). RAM is volatile but, HD is non-
volatile. Generally, file is a collection of numbers, symbols text etc. placed on a
disk. Ss, we have to saved data as a file in disk files can be read, write & modify
according to the requirements. So, data files are the files of computer in which
data are permanently stored. So, we can add, modify & delete according to the
requirements.
(File handling means working with files specially creating, deleting, opening,
closing, reading the contents of it and writing new contents to files in the
computer.) Buffer Output
Function Hard Disk
RAM C Library
ab Function
Program File
3 4 Hard Disk
Input
File Function

Open Buffer is a temporary physical storage used


File Fil e
fp to hold data during execution of process
from one place to another.
Types of Files :-
There are 2 types of file on the basis of access:-
Sequential Files
Random Access Files.
Sequential Files:-
These files allow reading the data from the file in one by one in
sequence. There is no pre-defined order for a accessing data. All the processes
are declare & arranged by the compiler during runtime of the program. If we
want to read the last records of the file, we need to read all the records from
starting. It takes more time.
Random Access Files:-
These files allow reading data from any location in the file. In this type,
data can be read & modified randomly. It takes less time because we can read
form specific location directly. In "C" programming language, pre-defined
functions like fseek( ), rewind( ), ftell( ) etc.
Modes of File:-
r = Open for reading. r+ = Open for reading & writing. (Start at beginning)
w = Open for writing. w+ =Open for reading & writing .(Overwrite file)

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":-

Function Purpose Syntax

fopen( ) Used to create or open an existing file. It FILE *fp;


returns FILE *fp (file pointer). fp=fopen("file name”,"mode");
fp=fopen(“ram.txt", "r");

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

You might also like