SlideShare a Scribd company logo
1 of 15
Download to read offline
Files in C
Presented By
TANUJA A
Topics Covered
▪ Files & Its Types
▪ Operations on File
▪ Sequential Files
▪ Random Files
Files & Its Types
Collection of information.
Types of files
1. Sequential files
2. Random files
1. Sequential file: data is accessed one after the
another Eg: Cassettes
2. Random file : data can be accessed from
anywhere Eg: CD
3
Operations on File
1.Read
2.Write
3.Append
In order to perform any operations we require to do
following task:
1. Declare File pointer
2. Open File
3. Perform operation
4. Close File
4
Continue..
Declaration of File:
FILE *Pointer_to_File;
Opening of File
Pointer_to_File= fopen(filename, mode);
Modes indicates operation on File:
r -> read
w -> write
a ->append
w+ -> readwrite -> destorys previous data
r+ -> readwrite -> fetches an error if it doesnot exist
a+ -> readwrite -> written at end of the file
Closing of File:
fclose(Pointer_to_File);
5
Sequential File
In order to perform operation we require functions to read or write data.
1. Writing data to Files:
a. int fprintf(pointer_to_file,”control string”, varlist);
b. int fputc(character, pointer_to_file );
c. int fputs(string, pointer_to_file );
2. Reading data from Files:
a. int fscanf(pointer_to_file,”control string”, varlist);
b. int fgetc(pointer_to_file);
c. char * fgets(string,string_size,pointer_to_file);
6
Writing data Using fprintf()
#include<stdio.h>
#include<conio.h>
void main()
{
//Declaration of variables INPUT
File *fptr;
int age;
char name[10];
clrscr();
//open file
fptr=fopen(fptr,”program1.c”,”w”);
if(fptr==NULL)
{
printf(“no file exist”);
exit(0); OUTPUT
}
// Get input from user
printf(“enter name and age”);
scanf(“%s%d”, &name, &age);
// write into file
fprintf(fptr,“%s%d”, name, age);
fclose(fptr);
getch();
}
7
Console Window:
enter name and age
abc
18
program1.c
abc 18
Writing data Using fputc() & fputs()
#include<stdio.h>
#include<conio.h>
void main()
{
//Declaration of variables
File *fptr;
char ch;
char name[10];
clrscr();
//open file
fptr=fopen(fptr,”program1.c”,”w”);
if(fptr==NULL)
{
printf(“no file exist”);
exit(0);
}
// Get input from user
printf(“enter name and character”);
scanf(“%s%c”, &name, &ch);
fputs(name, fptr);
fputc(ch, fptr);
fclose(fptr);
getch();
}
8
Console Window:
enter name and character
abc
$
program1.c
abc $
Reading from File
#include<stdio.h>
#include<conio.h>
void main()
{
//Declaration of variables
File *fptr;
int age;
char name[10];
clrscr();
//open file
fptr=fopen(fptr,”program1.c”,”r”);
if(fptr==NULL)
{
printf(“no file exist”);
exit(0);
}
//Reading from file
fscanf(fptr,“%s%d”, &name, &age);
printf(“contents from file %s %d”,name,age);
fclose(fptr);
getch();
}
9
Console Window:
contents from file
abc 19
program1.c
abc 19
Continue..#include<stdio.h>
#include<conio.h>
void main()
{ Input
//Declaration of variables
File *fptr;
char ch;
clrscr();
//open file
fptr=fopen(fptr,”program2.c”,”r”);
if(fptr==NULL)
{
printf(“no file exist”);
exit(0);
}
ch= fgetc(fptr);
while(ch!=EOF)
{
printf(“%c”, ch);
ch= fgetc(fptr);
}
fclose(fptr);
getch();
}
10
Console Window:
a
program2.c
a
program2.c
b
Console Window:
ab
program2.c
ab
Reading from File
#include <stdio.h>
void main ()
{
FILE *fptr;
char str[60];
/* opening file for reading */
fptr = fopen(”program2.c" , "r");
if(fptr == NULL)
{
printf("Error opening file");
exit(0);
}
fgets (str, 3, fptr);
printf(“%d”,str);
fclose(fp);
return(0);
}
11
program2.c
abcd
Console Window:
abc
Random Files
Functions used:
1. int fseek(File *stream, long offset, int whence)
▪ File pointer
▪ Number of bytes
▪ Position
2. int ftell()
3. rewind()
12
Beginning 0 or SEEK_SET
Current 1 or SEEK_CUR
End 2 or SEEK_END
Random Files
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr;
fptr=fopen(“program3.c”,”w+”);
if(fptr==NULL)
{
printf(”cannot open file”);
exit(0);
}
fputs(“hello welcome”,fptr);
fseek(fptr, 6, SEEK_SET);
fputs(“morning”,fptr);
fclose(fptr);
getch();
}
13
program3.c
hello welcome
program3.c
hello morning
Continue..
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr;
fptr=fopen(“program3.c”,”w+”);
if(fptr==NULL)
{
printf(”cannot open file”); Before rewind After rewind
exit(0);
}
fputs(“hello”,fptr);
a= ftell(fptr);
printf(“%d”,a);
rewind(fptr);
a=ftell(fptr);
printf(“%d”,a);
fclose(fptr);
getch();
}
14
Console Window:
6
program3.c
hello
Console Window:
1
6
15

More Related Content

What's hot

What's hot (20)

File handling in 'C'
File handling in 'C'File handling in 'C'
File handling in 'C'
 
1file handling
1file handling1file handling
1file handling
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Introduction to c part 4
Introduction to c  part  4Introduction to c  part  4
Introduction to c part 4
 
File handling in C
File handling in CFile handling in C
File handling in C
 
Unit5
Unit5Unit5
Unit5
 
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 operations in c
File operations in cFile operations in c
File operations in c
 
File handling-dutt
File handling-duttFile handling-dutt
File handling-dutt
 
PHP file handling
PHP file handling PHP file handling
PHP file handling
 
File accessing modes in c
File accessing modes in cFile accessing modes in c
File accessing modes in c
 
Php files
Php filesPhp files
Php files
 
Files and Directories in PHP
Files and Directories in PHPFiles and Directories in PHP
Files and Directories in PHP
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
 
PHP - Introduction to File Handling with PHP
PHP -  Introduction to  File Handling with PHPPHP -  Introduction to  File Handling with PHP
PHP - Introduction to File Handling with PHP
 
File handling-c
File handling-cFile handling-c
File handling-c
 
File management and handling by prabhakar
File management and handling by prabhakarFile management and handling by prabhakar
File management and handling by prabhakar
 
Files in C
Files in CFiles in C
Files in C
 
PHP Filing
PHP Filing PHP Filing
PHP Filing
 

Similar to Files in c

C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDYC UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
Rajeshkumar Reddy
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
Rex Joe
 

Similar to Files in c (20)

File management
File managementFile management
File management
 
File_Handling in C.ppt
File_Handling in C.pptFile_Handling in C.ppt
File_Handling in C.ppt
 
File_Handling in C.ppt
File_Handling in C.pptFile_Handling in C.ppt
File_Handling in C.ppt
 
File in C language
File in C languageFile in C language
File in C language
 
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
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
 
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDYC UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
 
File management
File managementFile management
File management
 
C-FILE MANAGEMENT.pptx
C-FILE MANAGEMENT.pptxC-FILE MANAGEMENT.pptx
C-FILE MANAGEMENT.pptx
 
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
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
File management
File managementFile management
File management
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
Unit5 C
Unit5 C Unit5 C
Unit5 C
 
Unit 8
Unit 8Unit 8
Unit 8
 
Data Structure Using C - FILES
Data Structure Using C - FILESData Structure Using C - FILES
Data Structure Using C - FILES
 
Files in c++
Files in c++Files in c++
Files in c++
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
 
Concept of file handling in c
Concept of file handling in cConcept of file handling in c
Concept of file handling in c
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 

Recently uploaded

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 

Files in c

  • 1. Files in C Presented By TANUJA A
  • 2. Topics Covered ▪ Files & Its Types ▪ Operations on File ▪ Sequential Files ▪ Random Files
  • 3. Files & Its Types Collection of information. Types of files 1. Sequential files 2. Random files 1. Sequential file: data is accessed one after the another Eg: Cassettes 2. Random file : data can be accessed from anywhere Eg: CD 3
  • 4. Operations on File 1.Read 2.Write 3.Append In order to perform any operations we require to do following task: 1. Declare File pointer 2. Open File 3. Perform operation 4. Close File 4
  • 5. Continue.. Declaration of File: FILE *Pointer_to_File; Opening of File Pointer_to_File= fopen(filename, mode); Modes indicates operation on File: r -> read w -> write a ->append w+ -> readwrite -> destorys previous data r+ -> readwrite -> fetches an error if it doesnot exist a+ -> readwrite -> written at end of the file Closing of File: fclose(Pointer_to_File); 5
  • 6. Sequential File In order to perform operation we require functions to read or write data. 1. Writing data to Files: a. int fprintf(pointer_to_file,”control string”, varlist); b. int fputc(character, pointer_to_file ); c. int fputs(string, pointer_to_file ); 2. Reading data from Files: a. int fscanf(pointer_to_file,”control string”, varlist); b. int fgetc(pointer_to_file); c. char * fgets(string,string_size,pointer_to_file); 6
  • 7. Writing data Using fprintf() #include<stdio.h> #include<conio.h> void main() { //Declaration of variables INPUT File *fptr; int age; char name[10]; clrscr(); //open file fptr=fopen(fptr,”program1.c”,”w”); if(fptr==NULL) { printf(“no file exist”); exit(0); OUTPUT } // Get input from user printf(“enter name and age”); scanf(“%s%d”, &name, &age); // write into file fprintf(fptr,“%s%d”, name, age); fclose(fptr); getch(); } 7 Console Window: enter name and age abc 18 program1.c abc 18
  • 8. Writing data Using fputc() & fputs() #include<stdio.h> #include<conio.h> void main() { //Declaration of variables File *fptr; char ch; char name[10]; clrscr(); //open file fptr=fopen(fptr,”program1.c”,”w”); if(fptr==NULL) { printf(“no file exist”); exit(0); } // Get input from user printf(“enter name and character”); scanf(“%s%c”, &name, &ch); fputs(name, fptr); fputc(ch, fptr); fclose(fptr); getch(); } 8 Console Window: enter name and character abc $ program1.c abc $
  • 9. Reading from File #include<stdio.h> #include<conio.h> void main() { //Declaration of variables File *fptr; int age; char name[10]; clrscr(); //open file fptr=fopen(fptr,”program1.c”,”r”); if(fptr==NULL) { printf(“no file exist”); exit(0); } //Reading from file fscanf(fptr,“%s%d”, &name, &age); printf(“contents from file %s %d”,name,age); fclose(fptr); getch(); } 9 Console Window: contents from file abc 19 program1.c abc 19
  • 10. Continue..#include<stdio.h> #include<conio.h> void main() { Input //Declaration of variables File *fptr; char ch; clrscr(); //open file fptr=fopen(fptr,”program2.c”,”r”); if(fptr==NULL) { printf(“no file exist”); exit(0); } ch= fgetc(fptr); while(ch!=EOF) { printf(“%c”, ch); ch= fgetc(fptr); } fclose(fptr); getch(); } 10 Console Window: a program2.c a program2.c b Console Window: ab program2.c ab
  • 11. Reading from File #include <stdio.h> void main () { FILE *fptr; char str[60]; /* opening file for reading */ fptr = fopen(”program2.c" , "r"); if(fptr == NULL) { printf("Error opening file"); exit(0); } fgets (str, 3, fptr); printf(“%d”,str); fclose(fp); return(0); } 11 program2.c abcd Console Window: abc
  • 12. Random Files Functions used: 1. int fseek(File *stream, long offset, int whence) ▪ File pointer ▪ Number of bytes ▪ Position 2. int ftell() 3. rewind() 12 Beginning 0 or SEEK_SET Current 1 or SEEK_CUR End 2 or SEEK_END
  • 13. Random Files #include<stdio.h> #include<conio.h> void main() { FILE *fptr; fptr=fopen(“program3.c”,”w+”); if(fptr==NULL) { printf(”cannot open file”); exit(0); } fputs(“hello welcome”,fptr); fseek(fptr, 6, SEEK_SET); fputs(“morning”,fptr); fclose(fptr); getch(); } 13 program3.c hello welcome program3.c hello morning
  • 14. Continue.. #include<stdio.h> #include<conio.h> void main() { FILE *fptr; fptr=fopen(“program3.c”,”w+”); if(fptr==NULL) { printf(”cannot open file”); Before rewind After rewind exit(0); } fputs(“hello”,fptr); a= ftell(fptr); printf(“%d”,a); rewind(fptr); a=ftell(fptr); printf(“%d”,a); fclose(fptr); getch(); } 14 Console Window: 6 program3.c hello Console Window: 1 6
  • 15. 15