SlideShare a Scribd company logo
1 of 34
 PROBLEM:-if we need same data to be
processed again and again,we have to enter it
again and again and if volume of data is large,it
will be time consuming process.
 SOLUTION:-data can be permanently stored,a
program can read it directly at a high speed.
 The permanent storage space on a disk is called
FILE.
 FILE is a set of records that can be accessed
through the set of library functions.
1.SEQUENTIAL FILE:-in this data is kept permanently.if we
want to read the last record of file then we need to
read all the records before that record.it means if we
wan to read 10th record then the first 9 records should
be read sequentially for reaching to the 10th record.
2.RANDOM ACCESS FILE:- in this data can be read and
modified randomly.if we want to read the last record of
the file,we can read it directly.it takes less time as
compared as sequential file.
Before using a file in a program,we must open it,which
establishes a link between the program and the
operating system.A file must be defined as:-
FILE *fp;
data structure file pointer
where FILE is the data structure which is included in the
header file,STDIO.H and fp is declared as a pointer
which contains the address of a character which is to be
read.
fp = fopen (“ABC.DOC”, “r”);
Here fp =file pointer
fopen= function to open a file
ABC.DOC=name of the file
r=mode in which file is to be
opened
fopen is a function which contains two arguments:-
1. File name ABC.DOC
2. Mode in which we want to open the file.
3. Both these are of string types.
fopen():-
 This function loads the file from disk to memory
and stores the address of the first character to
be read in the pointer variable fp.
 If file is absent,it returns a NULL pointer
variable.
fclose(fp):- the file which is opened need to be
closed and only can be done by library function
fclose.
1. Read mode(r):- purpose to read from the
file.if file exists,it sets up a pointer which
points to first char in it and if it does not
exist,it returns NULL.
2. Write mode(w):-purpose to write in the
file.if file exists,its contents are
overwritten.if it does not exist new file is
created.
3. Append mode(a):-purpose is to add data in
already existing file.if it does not exist new
file is created.
4)Read +(r +):-it is an extension to read mode.the
operations possible on this mode are to read
existing,writing new contents and modifying existings
contents.it returns NULL if file does not exist.
5)Write +(w +):-it is similar to write mode, mode.the
operations possible on this mode are to write new
contents and modify contents already written.
6)Append +(a +):- it is similar to append mode,the
operations possible on this mode are to read existing
contents,add new contents at the end of file but cannot
modify existing contents.
void main()
{
FILE *fp;
fp=fopen(“data.txt”,”r”);
if(fp = = NULL)
{
printf(“cannot open file”);
}
exit(0);
}
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr;
clrscr();
fptr=fopen("a.txt",“w");
if(fptr==null)
printf("n file cannot be opened for
creation");
else
printf("n file created successfully");
fclose(fptr);
}
 Binary modes:-
 wb(write):-it opens a binary file in write mode.
fp=fopen(“data.dat”,”wb”);
Here data.dat file is opened in binary mode for writing.
 rb(read):-it opens a binary file in read mode.
fp=fopen(“data.dat”,”rb”);
Here data.dat file is opened in binary mode for reading.
 Different functions to read a char are fgetc and getc.both
perform similar functions and have the same syntax.
ch = fgetc (fp);
char type function file pointer
variable name
 fgets() or gets():-used to read strings from a file and copy
string to a memory location which is referenced by array.
fgets(str, n, fptr);
array of chars max.length file pointer
 fputc() or putc():-both these fuction write the
character in the file at the position specified by
pointer.
putc (c, fp);
function character file pointer
type variable
The file pointer automatically moves 1 position
forward after printing characters in the file.
#include<conio.h>
void main()
{
FILE *fptr;
char c;
clrscr();
fptr=fopen("a.txt","r");
printf("the line of text is");
while((c=getc(fptr))!=EOF)
{
printf("%c",c);
}
fclose(fptr);
getch();
}
#include<conio.h>
#include<stdio.h>
void main()
{
FILE *fptr;
char c;
clrscr();
fptr=fopen("a.txt","w");
printf("Enter the line of text,press
^z to stop ");
while((c=getchar())!=EOF)
{
putc(c,fptr);
}
fclose(fptr);
}
main( )
{
FILE *fptr;
char ch;
fptr= fopen(“rec.dat”,”rb”);
if(fptr = = NULL)
printf(“file doesn‟t exist”);
else
{
while(( ch = fgetc(fptr)) !=EOF)
printf(“%c”,ch);
}
fclose(fptr);
}
EOF:-END OF FILE
EOF is an integer value sent
to prog.by OS.its value is
predefined to be -1 in
STDIO.H, No char with
the same value is stored
on disk.while creatig
file,OS detects that last
character to file has been
sent,it transmits EOF
signal.
main()
{
FILE *fp;
char name[20],arr[50];
printf(“enter the file name”);
scanf(“%s”,name);
fp=fopen(name,”w”);
if(fp = = NULL)
{
printf(“file can‟t be opened”);
exit(0);
}
else
{
printf(“the string is “);
Scanf(“%s”,arr);
fputs(arr,fp);
}
fclose(fp);
}
 fscanf( ):- it is used to read to any type of variable
i.e.,char,int,float,string
fscanf(fp,”%c”,&ch);
file pointer control char type
string variable
fscanf(fp,”%c %d %f”,&a,&b,&c);
We write stdin,this func call will work similar simple
scanf
Because stdin means standard i/p i.e.keyboard.
fscanf(stdin,”%c”,&ch);
Is equivalent to
scanf(“%c”,&ch);
#include<conio.h>
void main()
{
FILE *fptr;
char name[10];
int sal;
fptr=fopen(“rec.txt”,”r”);
fscanf(fptr,”%s%d”,name,&sal);
printf(“%s%d”,name,sal);
fclose(fptr);
getch();
}
 fprintf():- it is used to print chars,numbers or strings in
a file.
fprintf (fp,“%d”,a);
With this function,we can print a no. of variables with
single call.
fprintf (fp,”%c %d %d”,a,b,c);
We write stdout,this func call will work similar simple
printf
Because stdout means standard o/p i.e.keyboard.
fprintf(stdout,”%c”,ch);
Is equivalent to
printf(“%c”,ch);
main()
{
FILE *fp;
char name[10];
fp=fopen(“rec.dat”,”w”);
printf(“enter your name”);
scanf(“%s”,name);
fprintf(fptr,”%s”,name);
fclose(fp);
}
 fread():-reads a block(array or structure)from a file.
fread( ptr, m, n ,fptr);
address of size of no.of arrays file pointer
array/structure array/structure
 fwrite():-writes a block(array or structure)from a file.
fwrite( ptr, m, n ,fptr);
address of size of no.of arrays file pointer
array/structure array/structure
#include<conio.h>
struct student
{
int rollno;
char name[20];
};
void main()
{
struct student st;
file *fptr;
fptr=fopen("d.txt","r");
clrscr();
fread(&st,sizeof(st),1,fptr);
printf("roll number is %d",st.rollno);
printf("name is %s",st.name);
fclose(fptr);
getch();
}
#include<conio.h>
struct student
{
int rollno;
char name[20];
};
void main()
{
struct student st;
file *fptr;
fptr=fopen("d.txt","w");
clrscr();
printf("enter roll number");
scanf("%d",&st.number);
printf("enter name");
gets(st.name);
fwrite(&st,sizeof(st),1,fptr);
fclose(fptr);
getch();
}
 getw():-it is an integer oriented function.it is used to
read an integer from file in which numbers are stored.
a=getw(fptr);
int type var fun name file pointer
 putw():-it prints a number in the file.
putw( a, fptr);
lib fun int type var file pointer
#include<conio.h>
void main()
{
file *fptr;
int i,n;
clrscr();
fptr=fopen("b.txt","r");
printf("n the number are")
for(i=1;i<=10;i++)
{
n=getw(fptr);
printf("%dt",n);
}
fclose(fptr);
getch();
}
#include<conio.h>
void main()
{
file *fptr;
int i,n;
clrscr();
fptr=fopen("b.txt","w");
for(i=1;i<=10;i++)
{
printf("enter number");
scanf("%d",&n);
putw(n,fptr);
}
fclose(fptr);
getch();
}
 fseek():-it is used for setting the file position pointer at the
specified byte.
fseek( FILE *fp,long displacement,int origin);
file pointer long int can no.of bytes skipped
be +ve/-ve backward(-ve)
forward(+ve)
Constant Value position
SEEK_SET
SEEK_CURRENT
SEEK_END
0
1
2
Beginning of file
Current position
End of file
fseek(p,10L,0);
origin is 0,means that displacement will be relative to
beginning of file,so position pointer is skipped 10 bytes
forward from beginning of file.2nd argument is long
integer so L is attached with it.
fseek(p,8L,SEEK_SET);
position pointer is skipped 8 bytes forward from
beginning of file.
fseek(p,-6L,SEEK_END);
position pointer is skipped 6 bytes backward from the
end of file.
main()
{
FILE *fp;
char temp,name;
printf(“enter name of file”);
scanf(“%s”,name);
fp=fopen(name”,”r”);
while((temp=getc(fptr))!=EOF)
{
if(temp= =„x‟)
{
fseek(fp, -1,1);
putc(„y‟,fp);
}
}
fclose(fp);
}
 #include <stdio.h>
 int main ()
 {
 FILE *fp;
 fp = fopen("file.txt","w+");
 fputs("This is tutorialspoint.com", fp);
 fseek( fp, 7, SEEK_SET );
 fputs(" C Programming Langauge", fp);
fclose(fp);
 return(0);
 }
main()
{
FILE *fp;
char ch;
fp=fopen(“text”,”r”);
fseek(fp,241,0);
ch=fgetc(fp);
while(!feof (fp))
{
printf(“%c”,ch);
printf(“%d”,ftell(fp));
ch=fgetc(fp);
}
fclose(fp);
}
 Rewind():-it is used to move the file pointer to the beginning of
the given file.
rewind(fptr);
main()
{
FILE *fp;
fp=fopen(“stu.dat”,”r”);
if(fp==NULL)
{
printf(“file can not open”);
exit(0);
}
printf(“position pointer %d”,ftell(fp));
fseek(fp,0,2);
printf(“position pointer %d”,ftell(fp));
rewind(fp);
fclose(fp);
}
 ferror():-it is used for detecting error in the file on
file pointer or not.it returns the value nonzero if an
error occurs otherwise returns zero value.
ferror(fptr);

More Related Content

What's hot (19)

C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 
File handling in 'C'
File handling in 'C'File handling in 'C'
File handling in 'C'
 
File in c
File in cFile in c
File in c
 
File handling-c
File handling-cFile handling-c
File handling-c
 
File operations in c
File operations in cFile operations in c
File operations in c
 
File management
File managementFile management
File management
 
Data Structure Using C - FILES
Data Structure Using C - FILESData Structure Using C - FILES
Data Structure Using C - FILES
 
File in C Programming
File in C ProgrammingFile in C Programming
File in C Programming
 
File handling-c programming language
File handling-c programming languageFile handling-c programming language
File handling-c programming language
 
File Management
File ManagementFile Management
File Management
 
File handling in c
File  handling in cFile  handling in c
File handling in c
 
File handling in C
File handling in CFile handling in C
File handling in C
 
File handling in c
File handling in c File handling in c
File handling in c
 
Unit 8
Unit 8Unit 8
Unit 8
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Files in C
Files in CFiles in C
Files in C
 
File mangement
File mangementFile mangement
File mangement
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
 
File handling in c
File handling in cFile handling in c
File handling in c
 

Viewers also liked

แก้ข้อสอบ..
แก้ข้อสอบ..แก้ข้อสอบ..
แก้ข้อสอบ..palmchanita
 
Afternoon tea middle east
Afternoon tea middle eastAfternoon tea middle east
Afternoon tea middle eastromoshlomo44
 
Never alone ppt slide
Never alone ppt slideNever alone ppt slide
Never alone ppt sliderenzaldin
 
Game is-over
Game is-overGame is-over
Game is-overonessfee
 
Some, any, another, other, each, every
Some, any, another, other, each, everySome, any, another, other, each, every
Some, any, another, other, each, everytheLecturette
 
Control prenatal
Control prenatal Control prenatal
Control prenatal pedrothg
 
Hospice letter
Hospice letterHospice letter
Hospice letternm118486
 
Financial management ....the millieum financial management
Financial management ....the millieum financial managementFinancial management ....the millieum financial management
Financial management ....the millieum financial managementraufik tajuddin
 
Hadoop Robot from eBay at China Hadoop Summit 2015
Hadoop Robot from eBay at China Hadoop Summit 2015Hadoop Robot from eBay at China Hadoop Summit 2015
Hadoop Robot from eBay at China Hadoop Summit 2015polo li
 
Covestro y Ercros. tarragona
Covestro y Ercros. tarragonaCovestro y Ercros. tarragona
Covestro y Ercros. tarragonaoblanca
 
The history of video games goes as far back as the early 1940s
The history of video games goes as far back as the early 1940sThe history of video games goes as far back as the early 1940s
The history of video games goes as far back as the early 1940sJian Li
 
Better Biz Dev – Music Startup Academy Denver - October 8, 2015
Better Biz Dev – Music Startup Academy Denver - October 8, 2015Better Biz Dev – Music Startup Academy Denver - October 8, 2015
Better Biz Dev – Music Startup Academy Denver - October 8, 2015Shawn Yeager
 

Viewers also liked (20)

แก้ข้อสอบ..
แก้ข้อสอบ..แก้ข้อสอบ..
แก้ข้อสอบ..
 
Afternoon tea middle east
Afternoon tea middle eastAfternoon tea middle east
Afternoon tea middle east
 
Never alone ppt slide
Never alone ppt slideNever alone ppt slide
Never alone ppt slide
 
Poríferos e cnidários 3C- 2015
Poríferos e cnidários 3C- 2015Poríferos e cnidários 3C- 2015
Poríferos e cnidários 3C- 2015
 
1200 j lipman
1200 j lipman1200 j lipman
1200 j lipman
 
Game is-over
Game is-overGame is-over
Game is-over
 
Some, any, another, other, each, every
Some, any, another, other, each, everySome, any, another, other, each, every
Some, any, another, other, each, every
 
Exam54
Exam54Exam54
Exam54
 
Control prenatal
Control prenatal Control prenatal
Control prenatal
 
Xbrm Overview 2009
Xbrm Overview 2009Xbrm Overview 2009
Xbrm Overview 2009
 
Kaypp
KayppKaypp
Kaypp
 
Hospice letter
Hospice letterHospice letter
Hospice letter
 
Financial management ....the millieum financial management
Financial management ....the millieum financial managementFinancial management ....the millieum financial management
Financial management ....the millieum financial management
 
Hadoop Robot from eBay at China Hadoop Summit 2015
Hadoop Robot from eBay at China Hadoop Summit 2015Hadoop Robot from eBay at China Hadoop Summit 2015
Hadoop Robot from eBay at China Hadoop Summit 2015
 
Covestro y Ercros. tarragona
Covestro y Ercros. tarragonaCovestro y Ercros. tarragona
Covestro y Ercros. tarragona
 
Celevation
CelevationCelevation
Celevation
 
The history of video games goes as far back as the early 1940s
The history of video games goes as far back as the early 1940sThe history of video games goes as far back as the early 1940s
The history of video games goes as far back as the early 1940s
 
Modul 1 bahasa indonesia kb 1
Modul 1 bahasa indonesia kb 1Modul 1 bahasa indonesia kb 1
Modul 1 bahasa indonesia kb 1
 
Better Biz Dev – Music Startup Academy Denver - October 8, 2015
Better Biz Dev – Music Startup Academy Denver - October 8, 2015Better Biz Dev – Music Startup Academy Denver - October 8, 2015
Better Biz Dev – Music Startup Academy Denver - October 8, 2015
 
Pd t 27
Pd t 27Pd t 27
Pd t 27
 

Similar to file handling1 (20)

Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdfAdvance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
 
Unit5 C
Unit5 C Unit5 C
Unit5 C
 
Module 5 file cp
Module 5 file cpModule 5 file cp
Module 5 file cp
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
File handling
File handlingFile handling
File handling
 
PPS-II UNIT-5 PPT.pptx
PPS-II  UNIT-5 PPT.pptxPPS-II  UNIT-5 PPT.pptx
PPS-II UNIT-5 PPT.pptx
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
Handout#01
Handout#01Handout#01
Handout#01
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
 
5 Structure & File.pptx
5 Structure & File.pptx5 Structure & File.pptx
5 Structure & File.pptx
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
4 text file
4 text file4 text file
4 text file
 
Unit-VI.pptx
Unit-VI.pptxUnit-VI.pptx
Unit-VI.pptx
 
File Organization
File OrganizationFile Organization
File Organization
 
Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
 
Chap 12(files)
Chap 12(files)Chap 12(files)
Chap 12(files)
 
Lk module4 file
Lk module4 fileLk module4 file
Lk module4 file
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'
 

More from student

Logic Gates
Logic GatesLogic Gates
Logic Gatesstudent
 
Flipflops and Excitation tables of flipflops
Flipflops and Excitation tables of flipflopsFlipflops and Excitation tables of flipflops
Flipflops and Excitation tables of flipflopsstudent
 
Number Systems
Number SystemsNumber Systems
Number Systemsstudent
 
towers of hanoi
towers of hanoitowers of hanoi
towers of hanoistudent
 
header, circular and two way linked lists
header, circular and two way linked listsheader, circular and two way linked lists
header, circular and two way linked listsstudent
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structurestudent
 
Number Systems
Number SystemsNumber Systems
Number Systemsstudent
 
binary arithmetic rules
binary arithmetic rulesbinary arithmetic rules
binary arithmetic rulesstudent
 
BCD,GRAY and EXCESS 3 codes
BCD,GRAY and EXCESS 3 codesBCD,GRAY and EXCESS 3 codes
BCD,GRAY and EXCESS 3 codesstudent
 
animals colours numbers idioms
animals colours numbers idiomsanimals colours numbers idioms
animals colours numbers idiomsstudent
 
irregular verbs
irregular verbsirregular verbs
irregular verbsstudent
 
dc generator ece
dc generator ecedc generator ece
dc generator ecestudent
 
INDUCTION MOTOR
INDUCTION MOTORINDUCTION MOTOR
INDUCTION MOTORstudent
 
structure and union
structure and unionstructure and union
structure and unionstudent
 
storage class
storage classstorage class
storage classstudent
 
direct and indirect band gap
direct and indirect band gapdirect and indirect band gap
direct and indirect band gapstudent
 
hall effect
hall effecthall effect
hall effectstudent
 
optics chapter_07_solution_manual
optics chapter_07_solution_manualoptics chapter_07_solution_manual
optics chapter_07_solution_manualstudent
 
dyneins and kinesins
dyneins and kinesinsdyneins and kinesins
dyneins and kinesinsstudent
 
Structure and function of bacterial cells
Structure and function of bacterial cellsStructure and function of bacterial cells
Structure and function of bacterial cellsstudent
 

More from student (20)

Logic Gates
Logic GatesLogic Gates
Logic Gates
 
Flipflops and Excitation tables of flipflops
Flipflops and Excitation tables of flipflopsFlipflops and Excitation tables of flipflops
Flipflops and Excitation tables of flipflops
 
Number Systems
Number SystemsNumber Systems
Number Systems
 
towers of hanoi
towers of hanoitowers of hanoi
towers of hanoi
 
header, circular and two way linked lists
header, circular and two way linked listsheader, circular and two way linked lists
header, circular and two way linked lists
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structure
 
Number Systems
Number SystemsNumber Systems
Number Systems
 
binary arithmetic rules
binary arithmetic rulesbinary arithmetic rules
binary arithmetic rules
 
BCD,GRAY and EXCESS 3 codes
BCD,GRAY and EXCESS 3 codesBCD,GRAY and EXCESS 3 codes
BCD,GRAY and EXCESS 3 codes
 
animals colours numbers idioms
animals colours numbers idiomsanimals colours numbers idioms
animals colours numbers idioms
 
irregular verbs
irregular verbsirregular verbs
irregular verbs
 
dc generator ece
dc generator ecedc generator ece
dc generator ece
 
INDUCTION MOTOR
INDUCTION MOTORINDUCTION MOTOR
INDUCTION MOTOR
 
structure and union
structure and unionstructure and union
structure and union
 
storage class
storage classstorage class
storage class
 
direct and indirect band gap
direct and indirect band gapdirect and indirect band gap
direct and indirect band gap
 
hall effect
hall effecthall effect
hall effect
 
optics chapter_07_solution_manual
optics chapter_07_solution_manualoptics chapter_07_solution_manual
optics chapter_07_solution_manual
 
dyneins and kinesins
dyneins and kinesinsdyneins and kinesins
dyneins and kinesins
 
Structure and function of bacterial cells
Structure and function of bacterial cellsStructure and function of bacterial cells
Structure and function of bacterial cells
 

file handling1

  • 1.
  • 2.  PROBLEM:-if we need same data to be processed again and again,we have to enter it again and again and if volume of data is large,it will be time consuming process.  SOLUTION:-data can be permanently stored,a program can read it directly at a high speed.  The permanent storage space on a disk is called FILE.  FILE is a set of records that can be accessed through the set of library functions.
  • 3. 1.SEQUENTIAL FILE:-in this data is kept permanently.if we want to read the last record of file then we need to read all the records before that record.it means if we wan to read 10th record then the first 9 records should be read sequentially for reaching to the 10th record. 2.RANDOM ACCESS FILE:- in this data can be read and modified randomly.if we want to read the last record of the file,we can read it directly.it takes less time as compared as sequential file.
  • 4. Before using a file in a program,we must open it,which establishes a link between the program and the operating system.A file must be defined as:- FILE *fp; data structure file pointer where FILE is the data structure which is included in the header file,STDIO.H and fp is declared as a pointer which contains the address of a character which is to be read.
  • 5. fp = fopen (“ABC.DOC”, “r”); Here fp =file pointer fopen= function to open a file ABC.DOC=name of the file r=mode in which file is to be opened fopen is a function which contains two arguments:- 1. File name ABC.DOC 2. Mode in which we want to open the file. 3. Both these are of string types.
  • 6. fopen():-  This function loads the file from disk to memory and stores the address of the first character to be read in the pointer variable fp.  If file is absent,it returns a NULL pointer variable. fclose(fp):- the file which is opened need to be closed and only can be done by library function fclose.
  • 7.
  • 8. 1. Read mode(r):- purpose to read from the file.if file exists,it sets up a pointer which points to first char in it and if it does not exist,it returns NULL. 2. Write mode(w):-purpose to write in the file.if file exists,its contents are overwritten.if it does not exist new file is created. 3. Append mode(a):-purpose is to add data in already existing file.if it does not exist new file is created.
  • 9. 4)Read +(r +):-it is an extension to read mode.the operations possible on this mode are to read existing,writing new contents and modifying existings contents.it returns NULL if file does not exist. 5)Write +(w +):-it is similar to write mode, mode.the operations possible on this mode are to write new contents and modify contents already written. 6)Append +(a +):- it is similar to append mode,the operations possible on this mode are to read existing contents,add new contents at the end of file but cannot modify existing contents.
  • 10. void main() { FILE *fp; fp=fopen(“data.txt”,”r”); if(fp = = NULL) { printf(“cannot open file”); } exit(0); }
  • 11. #include<stdio.h> #include<conio.h> void main() { FILE *fptr; clrscr(); fptr=fopen("a.txt",“w"); if(fptr==null) printf("n file cannot be opened for creation"); else printf("n file created successfully"); fclose(fptr); }
  • 12.  Binary modes:-  wb(write):-it opens a binary file in write mode. fp=fopen(“data.dat”,”wb”); Here data.dat file is opened in binary mode for writing.  rb(read):-it opens a binary file in read mode. fp=fopen(“data.dat”,”rb”); Here data.dat file is opened in binary mode for reading.
  • 13.  Different functions to read a char are fgetc and getc.both perform similar functions and have the same syntax. ch = fgetc (fp); char type function file pointer variable name  fgets() or gets():-used to read strings from a file and copy string to a memory location which is referenced by array. fgets(str, n, fptr); array of chars max.length file pointer
  • 14.  fputc() or putc():-both these fuction write the character in the file at the position specified by pointer. putc (c, fp); function character file pointer type variable The file pointer automatically moves 1 position forward after printing characters in the file.
  • 15. #include<conio.h> void main() { FILE *fptr; char c; clrscr(); fptr=fopen("a.txt","r"); printf("the line of text is"); while((c=getc(fptr))!=EOF) { printf("%c",c); } fclose(fptr); getch(); } #include<conio.h> #include<stdio.h> void main() { FILE *fptr; char c; clrscr(); fptr=fopen("a.txt","w"); printf("Enter the line of text,press ^z to stop "); while((c=getchar())!=EOF) { putc(c,fptr); } fclose(fptr); }
  • 16. main( ) { FILE *fptr; char ch; fptr= fopen(“rec.dat”,”rb”); if(fptr = = NULL) printf(“file doesn‟t exist”); else { while(( ch = fgetc(fptr)) !=EOF) printf(“%c”,ch); } fclose(fptr); } EOF:-END OF FILE EOF is an integer value sent to prog.by OS.its value is predefined to be -1 in STDIO.H, No char with the same value is stored on disk.while creatig file,OS detects that last character to file has been sent,it transmits EOF signal.
  • 17. main() { FILE *fp; char name[20],arr[50]; printf(“enter the file name”); scanf(“%s”,name); fp=fopen(name,”w”); if(fp = = NULL) { printf(“file can‟t be opened”); exit(0); } else { printf(“the string is “); Scanf(“%s”,arr); fputs(arr,fp); } fclose(fp); }
  • 18.  fscanf( ):- it is used to read to any type of variable i.e.,char,int,float,string fscanf(fp,”%c”,&ch); file pointer control char type string variable fscanf(fp,”%c %d %f”,&a,&b,&c); We write stdin,this func call will work similar simple scanf Because stdin means standard i/p i.e.keyboard. fscanf(stdin,”%c”,&ch); Is equivalent to scanf(“%c”,&ch);
  • 19. #include<conio.h> void main() { FILE *fptr; char name[10]; int sal; fptr=fopen(“rec.txt”,”r”); fscanf(fptr,”%s%d”,name,&sal); printf(“%s%d”,name,sal); fclose(fptr); getch(); }
  • 20.  fprintf():- it is used to print chars,numbers or strings in a file. fprintf (fp,“%d”,a); With this function,we can print a no. of variables with single call. fprintf (fp,”%c %d %d”,a,b,c); We write stdout,this func call will work similar simple printf Because stdout means standard o/p i.e.keyboard. fprintf(stdout,”%c”,ch); Is equivalent to printf(“%c”,ch);
  • 21. main() { FILE *fp; char name[10]; fp=fopen(“rec.dat”,”w”); printf(“enter your name”); scanf(“%s”,name); fprintf(fptr,”%s”,name); fclose(fp); }
  • 22.  fread():-reads a block(array or structure)from a file. fread( ptr, m, n ,fptr); address of size of no.of arrays file pointer array/structure array/structure  fwrite():-writes a block(array or structure)from a file. fwrite( ptr, m, n ,fptr); address of size of no.of arrays file pointer array/structure array/structure
  • 23. #include<conio.h> struct student { int rollno; char name[20]; }; void main() { struct student st; file *fptr; fptr=fopen("d.txt","r"); clrscr(); fread(&st,sizeof(st),1,fptr); printf("roll number is %d",st.rollno); printf("name is %s",st.name); fclose(fptr); getch(); }
  • 24. #include<conio.h> struct student { int rollno; char name[20]; }; void main() { struct student st; file *fptr; fptr=fopen("d.txt","w"); clrscr(); printf("enter roll number"); scanf("%d",&st.number); printf("enter name"); gets(st.name); fwrite(&st,sizeof(st),1,fptr); fclose(fptr); getch(); }
  • 25.  getw():-it is an integer oriented function.it is used to read an integer from file in which numbers are stored. a=getw(fptr); int type var fun name file pointer  putw():-it prints a number in the file. putw( a, fptr); lib fun int type var file pointer
  • 26. #include<conio.h> void main() { file *fptr; int i,n; clrscr(); fptr=fopen("b.txt","r"); printf("n the number are") for(i=1;i<=10;i++) { n=getw(fptr); printf("%dt",n); } fclose(fptr); getch(); }
  • 27. #include<conio.h> void main() { file *fptr; int i,n; clrscr(); fptr=fopen("b.txt","w"); for(i=1;i<=10;i++) { printf("enter number"); scanf("%d",&n); putw(n,fptr); } fclose(fptr); getch(); }
  • 28.  fseek():-it is used for setting the file position pointer at the specified byte. fseek( FILE *fp,long displacement,int origin); file pointer long int can no.of bytes skipped be +ve/-ve backward(-ve) forward(+ve) Constant Value position SEEK_SET SEEK_CURRENT SEEK_END 0 1 2 Beginning of file Current position End of file
  • 29. fseek(p,10L,0); origin is 0,means that displacement will be relative to beginning of file,so position pointer is skipped 10 bytes forward from beginning of file.2nd argument is long integer so L is attached with it. fseek(p,8L,SEEK_SET); position pointer is skipped 8 bytes forward from beginning of file. fseek(p,-6L,SEEK_END); position pointer is skipped 6 bytes backward from the end of file.
  • 30. main() { FILE *fp; char temp,name; printf(“enter name of file”); scanf(“%s”,name); fp=fopen(name”,”r”); while((temp=getc(fptr))!=EOF) { if(temp= =„x‟) { fseek(fp, -1,1); putc(„y‟,fp); } } fclose(fp); }
  • 31.  #include <stdio.h>  int main ()  {  FILE *fp;  fp = fopen("file.txt","w+");  fputs("This is tutorialspoint.com", fp);  fseek( fp, 7, SEEK_SET );  fputs(" C Programming Langauge", fp); fclose(fp);  return(0);  }
  • 32. main() { FILE *fp; char ch; fp=fopen(“text”,”r”); fseek(fp,241,0); ch=fgetc(fp); while(!feof (fp)) { printf(“%c”,ch); printf(“%d”,ftell(fp)); ch=fgetc(fp); } fclose(fp); }
  • 33.  Rewind():-it is used to move the file pointer to the beginning of the given file. rewind(fptr); main() { FILE *fp; fp=fopen(“stu.dat”,”r”); if(fp==NULL) { printf(“file can not open”); exit(0); } printf(“position pointer %d”,ftell(fp)); fseek(fp,0,2); printf(“position pointer %d”,ftell(fp)); rewind(fp); fclose(fp); }
  • 34.  ferror():-it is used for detecting error in the file on file pointer or not.it returns the value nonzero if an error occurs otherwise returns zero value. ferror(fptr);