SlideShare a Scribd company logo
Pointers
Topics
 File and it’s operation
 Opening and Closing file
 Input/Output Operations on Files
File and It’s Operation
 File: A file is a place on the disk where a
group of related data is stored.
 File Operations:
1. Naming a file
2. Opening a file
3. Reading data from a file
4. Writing data to a file
5. Closing a file
Opening and Closing a file
 Opening a File: The general format for declaring
and opening a file:
FILE *fp;
fp = fopen(“filename”,”mode”);
Here filename is a valid name and mode can be
one of the following:
r – open the file for reading only
w – open the file for writing only
a - open the file for appending data to it
 Closing a file: The general form:
fclose(file_pointer);
Input/Output Operations on Files
 putc(): It writes a character to the file
specified by the file pointer. The general
format is-
putc(c,fp);
 getc(): It reads a character from a file
specified by the file pointer. The general
format is-
c=getc(fp);
Writing to and Reading from a File
main()
{
FILE *fp;
char c;
fp=fopen(“Sample”,”w”);
while((c=getchar())!=‘n’)
putc(c,fp);
fclose(fp);
fp=fopen(“Sample”,”r”);
While((c=getc(fp)!=EOF)
printf(“%c”,c);
getch();
}
Input/Output Operations on Files
 putw(): It writes an integer to a file specified
by the file pointer. The general format is-
putw(integer, fp);
 getw(): It reads an integer from a file
specified by the file pointer. The general
format is-
n=getw(fp);
Input/Output Operations on Files
 fprintf(): It writes a group of mixed data
items to the file specified by the file pointer.
The general format is-
fprintf(fp,”control string”,list);
 fscanf(): It reads a group of mixed data
items from a file specified by the file pointer.
The general format is-
fscanf(fp,”control string”,list);
Handling of Integer data files
main()
{
FILE *fp;
int x,k;
fp=fopen(“Sample”,”w”);
for(k=1;k<=10;k++)
{ scanf(“%d”,&x);
putw(x,fp);}
fclose(fp);
fp=fopen(“Sample”,”r”);
for(k=1;k<=10;k++)
{ x=getw(fp);
printf(“%d ”,x}
fclose(fp);
}
Handling of files with mixed data types
main()
{
FILE *fp;
int x,k;
float y;
fp=fopen(“Sample”,”w”);
for(k=1;k<=10;k++)
{ scanf(“%d %f”,&x,&y);
fprintf(fp,”%d %f”,x,y); }
fclose(fp);
fp=fopen(“Sample”,”r”);
for(k=1;k<=10;k++)
{ fscanf(fp,”%d %f”,&x,&y);
printf(“n%d %f ”,x,y}
fclose(fp);
}
Error Handling during I/O Operation
 Reasons of error during file operation:
1. Trying to read beyond the end-of-file
mark.
2. Device overflow
3. Trying to use a file that has not been
opened.
4. Trying to perform an operation on a file,
when the file is opened for another type of
operation
5. Opening a file with an invalid filename
6. Attempting to write to a write-protected
Continue…
 The feof() function can be used to test for an end of the file
condition. If fp is a pointer to file that has just been opened
for reading, then the statement
if(feof(fp))
printf(“End of the file”);
would display the message “End of the file” on reaching the
end of file condition.
 The ferror() function reports the status of the file indicated.
It returns nonzero integer if an error has been detected
upto that point otherwise returns zero. The statement
if(ferror(fp) !=0)
printf(“An error has occurred”);
would print the error message if the reading is not
successful.
Random Access to Files
 When we are interested in accessing only a
particular part of a file and not in reading the
other parts, then we need some other
functions named fseek, ftell and rewind.
 Ftell() takes a file pointer and returns a
number of type long, that corresponding to
the current position. The form-
n = ftell(fp);
n would give the relative offset (in bytes) of
the current position.
Continue…
 rewind() takes a file pointer and reset the position
to the start of the file.
rewind(fp);
 Fseek() is used to move the file position to a
desired location within the file. The general format
is
fseek(file_pointer, offset, position);
file_pointer is a pointer to the file concerned, offset
is a number or variable of type long and position is
an integer number. The offset specifies the
number of positions (bytes) to be moved from the
location specified by position.
Continue…
 Example of fseek():
fseek(fp,0L,0); go to the beginning
fseek(fp,0L,1); stay at current position
fseek(fp,0L,2); go to the end of the file
fseek(fp,m,0); move to (m+1)th byte in the file
fseek(fp,m,1); go forward by m bytes
fseek(fp,-m,1); go backward by m bytes from
current position
fseek(fp,-m,2); to backward by bytes from the end
Use of fseek and ftell function
main()
{
FILE *fp;
long n;
char c;
fp = fopen(“Random”,”w”);
while((c=getchar())!=‘n’)
putc(c,fp);
printf(“Number of char = %dn”,ftell(fp));
fclose(fp);
fp = fopen(“Random”,”r”);
n = 0L;
Continue…
while(feof(fp) = = 0)
{
fseek(fp, n, 0);
printf(“position of %c is %ldn”,getc(fp),ftell(fp);
n = n+5L;
}
putchar(“n”);
fseek(fp,-1L,2);
do{
putchar(getc(fp));
}while(!fseek(fp,-2L,1));
fclose(fp);
}

More Related Content

PPTX
classes and objects in C++
PDF
JavaScript - Chapter 11 - Events
PPTX
PPTX
File Management in C
PPTX
Exception handling c++
PPT
File handling in C++
PPT
Files in c++ ppt
PPTX
Packages In Python Tutorial
classes and objects in C++
JavaScript - Chapter 11 - Events
File Management in C
Exception handling c++
File handling in C++
Files in c++ ppt
Packages In Python Tutorial

What's hot (20)

PPT
PDF
Java I/o streams
PDF
Python programming : Files
PPT
Input and output in C++
PPSX
Break and continue
DOCX
Recursion in C++
PPTX
File in C language
PPTX
Member Function in C++
PPSX
Files in c++
PPTX
PPTX
File handling in c
PPT
Functions in C++
PDF
C++ OOPS Concept
PDF
Applications of stack
PPTX
Tuple in python
PPT
Function overloading(c++)
PDF
Functions and modules in python
PPTX
Chapter 05 classes and objects
Java I/o streams
Python programming : Files
Input and output in C++
Break and continue
Recursion in C++
File in C language
Member Function in C++
Files in c++
File handling in c
Functions in C++
C++ OOPS Concept
Applications of stack
Tuple in python
Function overloading(c++)
Functions and modules in python
Chapter 05 classes and objects
Ad

Similar to Chap 12(files) (20)

PPTX
want to learn files,then just use this ppt to learn
PPT
PPT
File in c
PPTX
File management
DOCX
PPTX
C Programming Unit-5
PPT
Unit5
PPTX
File Handling ppt.pptx shjd dbkd z bdjdb d
PPT
File handling
DOC
Unit v
PDF
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
PPTX
Programming C- File Handling , File Operation
PPT
files in c ppt.ppt
PPT
presentation_files_1451938150_140676.ppt
PPT
Lecture 20 - File Handling
PDF
14. fiile io
PDF
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
PDF
Module 03 File Handling in C
PPTX
File handling in c
PPTX
PPS-II UNIT-5 PPT.pptx
want to learn files,then just use this ppt to learn
File in c
File management
C Programming Unit-5
Unit5
File Handling ppt.pptx shjd dbkd z bdjdb d
File handling
Unit v
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
Programming C- File Handling , File Operation
files in c ppt.ppt
presentation_files_1451938150_140676.ppt
Lecture 20 - File Handling
14. fiile io
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
Module 03 File Handling in C
File handling in c
PPS-II UNIT-5 PPT.pptx
Ad

More from Bangabandhu Sheikh Mujibur Rahman Science and Technology University (20)

Recently uploaded (20)

PPTX
Cell Types and Its function , kingdom of life
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Institutional Correction lecture only . . .
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Business Ethics Teaching Materials for college
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Pharma ospi slides which help in ospi learning
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Classroom Observation Tools for Teachers
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
Cell Types and Its function , kingdom of life
Microbial diseases, their pathogenesis and prophylaxis
Institutional Correction lecture only . . .
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Microbial disease of the cardiovascular and lymphatic systems
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPH.pptx obstetrics and gynecology in nursing
STATICS OF THE RIGID BODIES Hibbelers.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Business Ethics Teaching Materials for college
Basic Mud Logging Guide for educational purpose
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Pharma ospi slides which help in ospi learning
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Classroom Observation Tools for Teachers
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
TR - Agricultural Crops Production NC III.pdf

Chap 12(files)

  • 1. Pointers Topics  File and it’s operation  Opening and Closing file  Input/Output Operations on Files
  • 2. File and It’s Operation  File: A file is a place on the disk where a group of related data is stored.  File Operations: 1. Naming a file 2. Opening a file 3. Reading data from a file 4. Writing data to a file 5. Closing a file
  • 3. Opening and Closing a file  Opening a File: The general format for declaring and opening a file: FILE *fp; fp = fopen(“filename”,”mode”); Here filename is a valid name and mode can be one of the following: r – open the file for reading only w – open the file for writing only a - open the file for appending data to it  Closing a file: The general form: fclose(file_pointer);
  • 4. Input/Output Operations on Files  putc(): It writes a character to the file specified by the file pointer. The general format is- putc(c,fp);  getc(): It reads a character from a file specified by the file pointer. The general format is- c=getc(fp);
  • 5. Writing to and Reading from a File main() { FILE *fp; char c; fp=fopen(“Sample”,”w”); while((c=getchar())!=‘n’) putc(c,fp); fclose(fp); fp=fopen(“Sample”,”r”); While((c=getc(fp)!=EOF) printf(“%c”,c); getch(); }
  • 6. Input/Output Operations on Files  putw(): It writes an integer to a file specified by the file pointer. The general format is- putw(integer, fp);  getw(): It reads an integer from a file specified by the file pointer. The general format is- n=getw(fp);
  • 7. Input/Output Operations on Files  fprintf(): It writes a group of mixed data items to the file specified by the file pointer. The general format is- fprintf(fp,”control string”,list);  fscanf(): It reads a group of mixed data items from a file specified by the file pointer. The general format is- fscanf(fp,”control string”,list);
  • 8. Handling of Integer data files main() { FILE *fp; int x,k; fp=fopen(“Sample”,”w”); for(k=1;k<=10;k++) { scanf(“%d”,&x); putw(x,fp);} fclose(fp); fp=fopen(“Sample”,”r”); for(k=1;k<=10;k++) { x=getw(fp); printf(“%d ”,x} fclose(fp); }
  • 9. Handling of files with mixed data types main() { FILE *fp; int x,k; float y; fp=fopen(“Sample”,”w”); for(k=1;k<=10;k++) { scanf(“%d %f”,&x,&y); fprintf(fp,”%d %f”,x,y); } fclose(fp); fp=fopen(“Sample”,”r”); for(k=1;k<=10;k++) { fscanf(fp,”%d %f”,&x,&y); printf(“n%d %f ”,x,y} fclose(fp); }
  • 10. Error Handling during I/O Operation  Reasons of error during file operation: 1. Trying to read beyond the end-of-file mark. 2. Device overflow 3. Trying to use a file that has not been opened. 4. Trying to perform an operation on a file, when the file is opened for another type of operation 5. Opening a file with an invalid filename 6. Attempting to write to a write-protected
  • 11. Continue…  The feof() function can be used to test for an end of the file condition. If fp is a pointer to file that has just been opened for reading, then the statement if(feof(fp)) printf(“End of the file”); would display the message “End of the file” on reaching the end of file condition.  The ferror() function reports the status of the file indicated. It returns nonzero integer if an error has been detected upto that point otherwise returns zero. The statement if(ferror(fp) !=0) printf(“An error has occurred”); would print the error message if the reading is not successful.
  • 12. Random Access to Files  When we are interested in accessing only a particular part of a file and not in reading the other parts, then we need some other functions named fseek, ftell and rewind.  Ftell() takes a file pointer and returns a number of type long, that corresponding to the current position. The form- n = ftell(fp); n would give the relative offset (in bytes) of the current position.
  • 13. Continue…  rewind() takes a file pointer and reset the position to the start of the file. rewind(fp);  Fseek() is used to move the file position to a desired location within the file. The general format is fseek(file_pointer, offset, position); file_pointer is a pointer to the file concerned, offset is a number or variable of type long and position is an integer number. The offset specifies the number of positions (bytes) to be moved from the location specified by position.
  • 14. Continue…  Example of fseek(): fseek(fp,0L,0); go to the beginning fseek(fp,0L,1); stay at current position fseek(fp,0L,2); go to the end of the file fseek(fp,m,0); move to (m+1)th byte in the file fseek(fp,m,1); go forward by m bytes fseek(fp,-m,1); go backward by m bytes from current position fseek(fp,-m,2); to backward by bytes from the end
  • 15. Use of fseek and ftell function main() { FILE *fp; long n; char c; fp = fopen(“Random”,”w”); while((c=getchar())!=‘n’) putc(c,fp); printf(“Number of char = %dn”,ftell(fp)); fclose(fp); fp = fopen(“Random”,”r”); n = 0L;
  • 16. Continue… while(feof(fp) = = 0) { fseek(fp, n, 0); printf(“position of %c is %ldn”,getc(fp),ftell(fp); n = n+5L; } putchar(“n”); fseek(fp,-1L,2); do{ putchar(getc(fp)); }while(!fseek(fp,-2L,1)); fclose(fp); }