Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 22

FILE HANDLING

WITH C
The Persistent World Of Files
File
Definition
A file can be understand as a named location in
computers secondary memory which is used to
store related data.

Usage
A file is used to store our information
permanently in computers secondary memory.
File Handling

File handling is a way to manage and manipulate
(Create, Delete, Read, Write) Files. With help of file
handling we can get user input after the program is
unloaded from the memory even. Because with the
file we can persist user input in to a disk file.

A file source Can be :-
A Hard Disk Drive
A Floppy Disk Drive
A Compact Disk
A USB Drive
Or Any Magnetic Media.

File Handling Architecture
SOURCE
CODE FILE
FILE
CONTENTS
Hard Disk
Drive
FILE ON
HARD
DRIVE
Reading
Writing
File Handling With C
Handling These files with c code is termed as file
handling with C.


C language allow us to manipulate these file.


For this purpose it provide us rich set of built-in
function which can be used perform different
operations on file. All these functions are located in
stdio.h header file.
File Handling Functions
Name Function
fopen( ) Opens a file.

fclose( ) Closes a file.

fputc( ) Same as putc() .

fgetc( ) Same as getc() .

fgets( ) Reads a string from a file.

fputs( ) Writes a string to a file.

fseek( ) Seeks to a specified byte in a file.




File Handling Functions
Name Function

ftell( ) Returns the current file position.

feof( ) Returns true if end-of-file is reached.

rewind( ) Resets the file position indicator to the
beginning of the file.

remove( ) Delete a file.

rename() Rename a file.

File Handling Functions
1. fopen() Is Used to open a file.
prototype
FILE * fopen(char *filename,char *filemode)

Description
filename File name to be open.

filemode Mode that decides which
operations are possible
with the file.
FILE * Is the Built-In FILE Structure
Pointer variable used to hold
low-level information about the
file.
Note :
FILE Structure Is also located in stdio.h header file.
File Handling Functions
fclose() Is Used to Close a Previously
Open file

prototype
void fclose(FILE *)

Description
FILE * Will be the FILE * which holds
the opend file information


File Modes
Mode Meaning

r Open a text file for reading.

w Create a text file for writing.

a Append to a text file.

rb Open a binary file for reading.

wb Create a binary file for writing.

ab Append to a binary file.

r+ Open a text file for read/write.

File Modes
Mode Meaning

w+ Create a text file for read/write.

a+ Append or create a text file for
read/write.

r+b Open a binary file for
read/write.

w+b Create a binary file for
read/write.

a+b Append or create a binary file
for read/write.

.

File Handling Functions
Example To Open & Close A File
#include<stdio.h>
#include<process.h>
#include<conio.h>
int main()
{
FILE *fp;
fp=fopen("details.txt","r");
if(fp==NULL)
{
printf("File Can Not Be Opened!!!");
exit(1);
}
else
{
printf("File Successfuly Opened!!!");
}
printf("Hit Enter To Close The File");
getch();
fclose(fp);
printf("File Successfuly Closed!!!");
return 0;
}
File Handling Functions
If a file is open in r mode then if it is not exists then null is
returned by fopen() otherwise a FILE * containing file
information is returned

If file is open in w mode then if file does not exists then a new
file is created and if it exists then all of its content will
overwritten and a blank file would be created.

If file is open in a mode then the if file does not exists then a new
file is created otherwise new contents are added at the end of
the file.
File Handling Functions
1 fputc() Writes a single character to
file
Prototype
void fputc(char,FILE *)

2 fgetc() Reads a single character
from a file.
Prototype
char fgetc(FILE *)

3 feof() Detects either end of file
occurred or not
Prototype
int feof(FILE *)

File Handling Functions
#include<stdio.h>
#include<conio.h>
#include<process.h>
int main()
{
char ch=' , FILE *fp;
fp=fopen("test.txt",w");
if(fp==NULL)
{
printf("File can't be open!!!!");
exit(1);
}
printf("Enter Character (Type @ to terminate):\n");
ch=getche();
while(ch != '@')
{
fputc(ch,fp);
ch=getche();
}
fclose(fp);
printf(Details Saved To File);
return 0;
}

Writing Character One By One In To A File
File Handling Functions
#include<stdio.h>
#include<conio.h>
#include<process.h>
int main()
{
FILE *fp;
char ch;
printf("Data of file is : -\n");
fp=fopen("test.txt","r");
if(fp==NULL) {
printf("File can't open for reading!!!!");
exit(1);
}
do {
ch=fgetc(fp);
putchar(ch);
}while(ch!=EOF);
fclose(fp);
return 0;
}
Reading Character One By One In From a File
File Handling Functions
We can use fseek function for random access in file.
fseek() Sets the file pointer to a
specific position in a file.
Prototype
int fseek(FILE *,long offset,int whence)
FILE * File pointer from which you
want to read

long offset How much difference you
want (in Bytes)

int whence From where you want to
position your pointer in file.
Random access in file
File Handling Functions

#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
FILE *fp=fopen("test.txt","r");
fseek(fp,5,SEEK_SET);
ch=fgetc(fp);
printf("\t%c",ch); // Will read r from file
fseek(fp,-6,SEEK_END);
ch=fgetc(fp);
printf("\t%c",ch); // Will read n from file
fclose(fp);
getch();
return 0;
}

Output :- r n
Contents Of File Are :- power of c with file handling
File Handling Functions
To Read and Write strings in file We use
following Functions.

1. fputs() Writes a string in to file.
Prototype
int fputs(const char *,FILE *)

2. fgets() Reads a string from a file.

Prototype
char * fgets(char *, int, FILE *)
File Handling Functions
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
clrscr();
FILE *fp;
int i;
fp=fopen("mystrings.txt","w");
char str[50];
printf("Input Three Strings :-\n");
for(i=1;i<=3;i++)
{
printf("Input String : ");
gets(str);
strcat(str,"\n");
fputs(str,fp);
}
fclose(fp);
printf("Strings Saved In To File!!!");
return 0;
}
Writing Strings in a text file
File Handling Functions
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
clrscr();
FILE *fp;
char str[50];
int i;
fp=fopen("mystrings.txt","r");
if(fp!=NULL)
{
fseek(fp,0,SEEK_SET);
printf("Strings Are :-\n");
for(i=1;i<=3;i++)
{
fgets(str,sizeof(str),fp);
printf("%s",str);
}
fclose(fp);
}
return 0;
}
Reading Strings in From a text file
File Handling


THANK YOU

You might also like