SlideShare a Scribd company logo
1 of 7
 fopen() to open a file
 fclose() to close a file
 fflush () to flush out the buffer associated with a file
 freopen() to change the file associated with a stream
 remove() to remove a file
 rename() to rename a file
 tempfile() to create a temporary binary file
 tmpnam() to generate a unique filename
1
 C requires a pointer to structure FILE defined in <stdio.h>.The
declaration of file pointer is done as follows:
 This declaration creates a variable fptr, which is pointed to the FILE
structure. The pointer to the FILE structure is also called file pointer.
 There are also predefined file pointers such stdin, stdout, and stderr
which refers to standard input (normally keyboard), standard output
(normally monitor) and standard error (connected to screen for error
handling)
 To open a file and associate it with a stream, we use fopen().Its
prototype is shown here:
2
MODE Meaning
“r” Open a text file for reading
“w” Create a text file for writing
“rb” Open a binary file for reading
“wb” Open a binary file for writing
“ab” Append to binary file
“rb+” Open a binary file for read/write
“wb+” Create a binary file for read/write
“ab+” Append a binary file for read/write
“w+” Create a text file for read/write
“a+” Append or creat a text file for read/write
3
struct rna{
char name[20];
int age;
float x;
};
struct rna e;
fp = fopen("EMP.DAT","wb");
if(fp == NULL){
puts("Cannot open file");
}
printf("Enter name, age and basic salary");
scanf("%s%d%f",e.name,&e.age,&e.x);
fwrite(&e,sizeof(e),1,fp);
fclose(fp); 4
fp = fopen("EMP.DAT","rb");
while(fread(&e,sizeof(e),1,fp)==1){
printf("%s %d %fn",e.name,e.age,e.x);
}
fclose(fp);
5
#include<stdio.h>
#include<stdlib.h>
int main(){
FILE *fp;
struct rna{
char name[40];
int age;
float x;
};
struct rna e;
fp = fopen("employ.dat","w");
if(fp == NULL){ //if file is not opened
puts("Cannot open file");
exit(1);
}
printf("nEnter name, age and basic salary: ");
scanf("%s %d %f", e.name, &e.age, &e.bs);
//writes name age and basic salary to the file
fprintf(fp, "%s %d %fn", e.name, e.age, e.x);
fclose(fp); //close the file
return 0;
} 6
struct rna e;
//opening file in writing mode
//this code try to open the employ.dat
//if it is already existed and returns null
//to fp it is not existed
fp = fopen("employ.dat","r");
if(fp == NULL){ //if file is not opened
puts("Cannot open file");
exit(1);
}
printf("nThe name, age and basic salary are: ");
//search the file until end of file encountered
while(fscanf(fp, "%s %d %fn", e.name, &e.age, &e.x)!=EOF){
printf("%s %d %fn",e.name,e.age,e.bs);
}
fclose(fp); //close the file
return 0;
}
7

More Related Content

What's hot (20)

File Management in C
File Management in CFile Management in C
File Management in C
 
Module 03 File Handling in C
Module 03 File Handling in CModule 03 File Handling in C
Module 03 File Handling in C
 
File handling in 'C'
File handling in 'C'File handling in 'C'
File handling in 'C'
 
Php file handling in Hindi
Php file handling in Hindi Php file handling in Hindi
Php file handling in Hindi
 
Php files
Php filesPhp files
Php files
 
php file uploading
php file uploadingphp file uploading
php file uploading
 
Files and Directories in PHP
Files and Directories in PHPFiles and Directories in PHP
Files and Directories in PHP
 
File in C Programming
File in C ProgrammingFile in C Programming
File in C Programming
 
PHP file handling
PHP file handling PHP file handling
PHP file handling
 
File Management
File ManagementFile Management
File Management
 
File operations in c
File operations in cFile operations in c
File operations in c
 
File handling-c
File handling-cFile handling-c
File handling-c
 
1file handling
1file handling1file handling
1file handling
 
Chap 5 php files part 1
Chap 5 php files part 1Chap 5 php files part 1
Chap 5 php files part 1
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
 
Chap 12(files)
Chap 12(files)Chap 12(files)
Chap 12(files)
 
Php File Operations
Php File OperationsPhp File Operations
Php File Operations
 
Files let you store data on secondary storage such as a hard disk so that you...
Files let you store data on secondary storage such as a hard disk so that you...Files let you store data on secondary storage such as a hard disk so that you...
Files let you store data on secondary storage such as a hard disk so that you...
 
File management and handling by prabhakar
File management and handling by prabhakarFile management and handling by prabhakar
File management and handling by prabhakar
 

Viewers also liked

Viewers also liked (20)

Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
 
List of programs for practical file
List of programs for practical fileList of programs for practical file
List of programs for practical file
 
C language first program
C language first programC language first program
C language first program
 
introduction to c language
 introduction to c language introduction to c language
introduction to c language
 
C Language Program
C Language ProgramC Language Program
C Language Program
 
What is c
What is cWhat is c
What is c
 
C PROGRAMMING BASICS- COMPUTER PROGRAMMING UNIT II
C PROGRAMMING BASICS- COMPUTER PROGRAMMING UNIT IIC PROGRAMMING BASICS- COMPUTER PROGRAMMING UNIT II
C PROGRAMMING BASICS- COMPUTER PROGRAMMING UNIT II
 
CHAPTER 3
CHAPTER 3CHAPTER 3
CHAPTER 3
 
The smartpath information systems c pro
The smartpath information systems c proThe smartpath information systems c pro
The smartpath information systems c pro
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
C program report tips
C program report tipsC program report tips
C program report tips
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
 
Library Management System Project in C
Library Management System Project in CLibrary Management System Project in C
Library Management System Project in C
 
C programming basics
C  programming basicsC  programming basics
C programming basics
 
C the basic concepts
C the basic conceptsC the basic concepts
C the basic concepts
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
C programming project by navin thapa
C programming project by navin thapaC programming project by navin thapa
C programming project by navin thapa
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
C ppt
C pptC ppt
C ppt
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 

Similar to File in C programming

file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.pptyuvrajkeshri
 
C 檔案輸入與輸出
C 檔案輸入與輸出C 檔案輸入與輸出
C 檔案輸入與輸出PingLun Liao
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10patcha535
 
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.pdfsangeeta borde
 
File handling in c
File handling in cFile handling in c
File handling in caakanksha s
 
File handling-dutt
File handling-duttFile handling-dutt
File handling-duttAnil Dutt
 
Understanding c file handling functions with examples
Understanding c file handling functions with examplesUnderstanding c file handling functions with examples
Understanding c file handling functions with examplesMuhammed Thanveer M
 
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.pdfsudhakargeruganti
 
Data Structure Using C - FILES
Data Structure Using C - FILESData Structure Using C - FILES
Data Structure Using C - FILESHarish Kamat
 

Similar to File in C programming (20)

file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
Unit5
Unit5Unit5
Unit5
 
Unit 8
Unit 8Unit 8
Unit 8
 
File management
File managementFile management
File management
 
Satz1
Satz1Satz1
Satz1
 
C 檔案輸入與輸出
C 檔案輸入與輸出C 檔案輸入與輸出
C 檔案輸入與輸出
 
Module 5 file cp
Module 5 file cpModule 5 file cp
Module 5 file cp
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
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
File handling in cFile handling in c
File handling in c
 
File handling-dutt
File handling-duttFile handling-dutt
File handling-dutt
 
5 Structure & File.pptx
5 Structure & File.pptx5 Structure & File.pptx
5 Structure & File.pptx
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
Understanding c file handling functions with examples
Understanding c file handling functions with examplesUnderstanding c file handling functions with examples
Understanding c file handling functions with examples
 
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
 
Data Structure Using C - FILES
Data Structure Using C - FILESData Structure Using C - FILES
Data Structure Using C - FILES
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 
4 text file
4 text file4 text file
4 text file
 

More from Samsil Arefin

Transmission Control Protocol and User Datagram protocol
Transmission Control Protocol and User Datagram protocolTransmission Control Protocol and User Datagram protocol
Transmission Control Protocol and User Datagram protocolSamsil Arefin
 
Evolution Phylogenetic
Evolution PhylogeneticEvolution Phylogenetic
Evolution PhylogeneticSamsil Arefin
 
Evolution Phylogenetic
Evolution PhylogeneticEvolution Phylogenetic
Evolution PhylogeneticSamsil Arefin
 
Ego net facebook data analysis
Ego net facebook data analysisEgo net facebook data analysis
Ego net facebook data analysisSamsil Arefin
 
Augmented Reality (AR)
Augmented Reality (AR)Augmented Reality (AR)
Augmented Reality (AR)Samsil Arefin
 
Client server chat application
Client server chat applicationClient server chat application
Client server chat applicationSamsil Arefin
 
Strings in programming tutorial.
Strings  in programming tutorial.Strings  in programming tutorial.
Strings in programming tutorial.Samsil Arefin
 
Linked list searching deleting inserting
Linked list searching deleting insertingLinked list searching deleting inserting
Linked list searching deleting insertingSamsil Arefin
 
Program to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical orderProgram to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical orderSamsil Arefin
 
Linked list int_data_fdata
Linked list int_data_fdataLinked list int_data_fdata
Linked list int_data_fdataSamsil Arefin
 
Linked list Output tracing
Linked list Output tracingLinked list Output tracing
Linked list Output tracingSamsil Arefin
 
Fundamentals of-electric-circuit
Fundamentals of-electric-circuitFundamentals of-electric-circuit
Fundamentals of-electric-circuitSamsil Arefin
 
Data structure lecture 1
Data structure   lecture 1Data structure   lecture 1
Data structure lecture 1Samsil Arefin
 
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or javaSamsil Arefin
 

More from Samsil Arefin (20)

Transmission Control Protocol and User Datagram protocol
Transmission Control Protocol and User Datagram protocolTransmission Control Protocol and User Datagram protocol
Transmission Control Protocol and User Datagram protocol
 
Evolution Phylogenetic
Evolution PhylogeneticEvolution Phylogenetic
Evolution Phylogenetic
 
Evolution Phylogenetic
Evolution PhylogeneticEvolution Phylogenetic
Evolution Phylogenetic
 
Ego net facebook data analysis
Ego net facebook data analysisEgo net facebook data analysis
Ego net facebook data analysis
 
Augmented Reality (AR)
Augmented Reality (AR)Augmented Reality (AR)
Augmented Reality (AR)
 
Client server chat application
Client server chat applicationClient server chat application
Client server chat application
 
Strings in programming tutorial.
Strings  in programming tutorial.Strings  in programming tutorial.
Strings in programming tutorial.
 
Linked list searching deleting inserting
Linked list searching deleting insertingLinked list searching deleting inserting
Linked list searching deleting inserting
 
Number theory
Number theoryNumber theory
Number theory
 
Program to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical orderProgram to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical order
 
Linked list int_data_fdata
Linked list int_data_fdataLinked list int_data_fdata
Linked list int_data_fdata
 
Linked list Output tracing
Linked list Output tracingLinked list Output tracing
Linked list Output tracing
 
Stack
StackStack
Stack
 
Sorting
SortingSorting
Sorting
 
Fundamentals of-electric-circuit
Fundamentals of-electric-circuitFundamentals of-electric-circuit
Fundamentals of-electric-circuit
 
Cyber security
Cyber securityCyber security
Cyber security
 
C programming
C programmingC programming
C programming
 
Data structure lecture 1
Data structure   lecture 1Data structure   lecture 1
Data structure lecture 1
 
Structure and union
Structure and unionStructure and union
Structure and union
 
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or java
 

Recently uploaded

chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 

Recently uploaded (20)

9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 

File in C programming

  • 1.  fopen() to open a file  fclose() to close a file  fflush () to flush out the buffer associated with a file  freopen() to change the file associated with a stream  remove() to remove a file  rename() to rename a file  tempfile() to create a temporary binary file  tmpnam() to generate a unique filename 1
  • 2.  C requires a pointer to structure FILE defined in <stdio.h>.The declaration of file pointer is done as follows:  This declaration creates a variable fptr, which is pointed to the FILE structure. The pointer to the FILE structure is also called file pointer.  There are also predefined file pointers such stdin, stdout, and stderr which refers to standard input (normally keyboard), standard output (normally monitor) and standard error (connected to screen for error handling)  To open a file and associate it with a stream, we use fopen().Its prototype is shown here: 2
  • 3. MODE Meaning “r” Open a text file for reading “w” Create a text file for writing “rb” Open a binary file for reading “wb” Open a binary file for writing “ab” Append to binary file “rb+” Open a binary file for read/write “wb+” Create a binary file for read/write “ab+” Append a binary file for read/write “w+” Create a text file for read/write “a+” Append or creat a text file for read/write 3
  • 4. struct rna{ char name[20]; int age; float x; }; struct rna e; fp = fopen("EMP.DAT","wb"); if(fp == NULL){ puts("Cannot open file"); } printf("Enter name, age and basic salary"); scanf("%s%d%f",e.name,&e.age,&e.x); fwrite(&e,sizeof(e),1,fp); fclose(fp); 4
  • 6. #include<stdio.h> #include<stdlib.h> int main(){ FILE *fp; struct rna{ char name[40]; int age; float x; }; struct rna e; fp = fopen("employ.dat","w"); if(fp == NULL){ //if file is not opened puts("Cannot open file"); exit(1); } printf("nEnter name, age and basic salary: "); scanf("%s %d %f", e.name, &e.age, &e.bs); //writes name age and basic salary to the file fprintf(fp, "%s %d %fn", e.name, e.age, e.x); fclose(fp); //close the file return 0; } 6
  • 7. struct rna e; //opening file in writing mode //this code try to open the employ.dat //if it is already existed and returns null //to fp it is not existed fp = fopen("employ.dat","r"); if(fp == NULL){ //if file is not opened puts("Cannot open file"); exit(1); } printf("nThe name, age and basic salary are: "); //search the file until end of file encountered while(fscanf(fp, "%s %d %fn", e.name, &e.age, &e.x)!=EOF){ printf("%s %d %fn",e.name,e.age,e.bs); } fclose(fp); //close the file return 0; } 7