SlideShare a Scribd company logo
 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
Paurav Shah
 
Module 03 File Handling in C
Module 03 File Handling in CModule 03 File Handling in C
Module 03 File Handling in C
Tushar B Kute
 
File handling in 'C'
File handling in 'C'File handling in 'C'
File handling in 'C'
Gaurav Garg
 
Php file handling in Hindi
Php file handling in Hindi Php file handling in Hindi
Php file handling in Hindi
Vipin sharma
 
Php files
Php filesPhp files
Php files
kalyani66
 
php file uploading
php file uploadingphp file uploading
php file uploading
Purushottam Kumar
 
Files and Directories in PHP
Files and Directories in PHPFiles and Directories in PHP
Files and Directories in PHP
Nicole Ryan
 
File in C Programming
File in C ProgrammingFile in C Programming
File in C Programming
Sonya Akter Rupa
 
PHP file handling
PHP file handling PHP file handling
PHP file handling
wahidullah mudaser
 
File Management
File ManagementFile Management
File Management
Ravinder Kamboj
 
File operations in c
File operations in cFile operations in c
File operations in c
baabtra.com - No. 1 supplier of quality freshers
 
File handling-c
File handling-cFile handling-c
File handling-c
CGC Technical campus,Mohali
 
1file handling
1file handling1file handling
1file handling
Frijo Francis
 
Chap 5 php files part 1
Chap 5 php files part 1Chap 5 php files part 1
Chap 5 php files part 1
monikadeshmane
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
ٖFaiXy :)
 
Chap 12(files)
Chap 12(files)Chap 12(files)
Chap 12(files)
Bangabandhu Sheikh Mujibur Rahman Science and Technology University
 
Php File Operations
Php File OperationsPhp File Operations
Php File Operations
mussawir20
 
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...
Bern Jamie
 
File management and handling by prabhakar
File management and handling by prabhakarFile management and handling by prabhakar
File management and handling by prabhakar
PrabhakarPremUpreti
 

Viewers also liked (20)

Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
RAJWANT KAUR
 
List of programs for practical file
List of programs for practical fileList of programs for practical file
List of programs for practical file
swatisinghal
 
C language first program
C language first programC language first program
C language first program
NIKHIL KRISHNA
 
introduction to c language
 introduction to c language introduction to c language
introduction to c language
Rai University
 
C Language Program
C Language ProgramC Language Program
C Language Program
Warawut
 
What is c
What is cWhat is c
What is c
Nitesh Saitwal
 
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
ANJALAI AMMAL MAHALINGAM ENGINEERING COLLEGE
 
CHAPTER 3
CHAPTER 3CHAPTER 3
CHAPTER 3
mohd_mizan
 
The smartpath information systems c pro
The smartpath information systems c proThe smartpath information systems c pro
The smartpath information systems c pro
The Smartpath Information Systems,Bhilai,Durg,Chhattisgarh.
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
Wingston
 
C program report tips
C program report tipsC program report tips
C program report tips
Harry Pott
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
Vivek Singh Chandel
 
Library Management System Project in C
Library Management System Project in CLibrary Management System Project in C
Library Management System Project in C
codewithc
 
C programming basics
C  programming basicsC  programming basics
C programming basics
argusacademy
 
C the basic concepts
C the basic conceptsC the basic concepts
C the basic concepts
Abhinav Vatsa
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
Wingston
 
C programming project by navin thapa
C programming project by navin thapaC programming project by navin thapa
C programming project by navin thapa
Navinthp
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
Durga Padma
 
C ppt
C pptC ppt
C ppt
jasmeen kr
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
Amr Ali (ISTQB CTAL Full, CSM, ITIL Foundation)
 

Similar to File in C programming (20)

Presentation of file handling in C language
Presentation of file handling in C languagePresentation of file handling in C language
Presentation of file handling in C language
Shruthi48
 
File handling in c
File handling in cFile handling in c
File handling in c
David Livingston J
 
file handling1
file handling1file handling1
file handling1
student
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'
eShikshak
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
Sandeepbhuma1
 
Programming C- File Handling , File Operation
Programming C- File Handling , File OperationProgramming C- File Handling , File Operation
Programming C- File Handling , File Operation
svkarthik86
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
yuvrajkeshri
 
File management
File managementFile management
File management
lalithambiga kamaraj
 
Unit-VI.pptx
Unit-VI.pptxUnit-VI.pptx
Unit-VI.pptx
Mehul Desai
 
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
 
File mangement
File mangementFile mangement
File mangement
Jigarthacker
 
file handling, dynamic memory allocation
file handling, dynamic memory allocationfile handling, dynamic memory allocation
file handling, dynamic memory allocation
indra Kishor
 
Unit5
Unit5Unit5
Unit5
mrecedu
 
want to learn files,then just use this ppt to learn
want to learn files,then just use this ppt to learnwant to learn files,then just use this ppt to learn
want to learn files,then just use this ppt to learn
nalluribalaji157
 
File handling in c
File handling in cFile handling in c
File handling in c
aakanksha s
 
File handling With Solve Programs
File handling With Solve ProgramsFile handling With Solve Programs
File handling With Solve Programs
Rohan Gajre
 
File Handling in C.pptx
File Handling in C.pptxFile Handling in C.pptx
File Handling in C.pptx
DrJasmineBeulahG
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
Vikram Nandini
 
Satz1
Satz1Satz1
Satz1
rajeshmhvr
 
Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
Sowri Rajan
 
Presentation of file handling in C language
Presentation of file handling in C languagePresentation of file handling in C language
Presentation of file handling in C language
Shruthi48
 
file handling1
file handling1file handling1
file handling1
student
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'
eShikshak
 
Programming C- File Handling , File Operation
Programming C- File Handling , File OperationProgramming C- File Handling , File Operation
Programming C- File Handling , File Operation
svkarthik86
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
yuvrajkeshri
 
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
 
file handling, dynamic memory allocation
file handling, dynamic memory allocationfile handling, dynamic memory allocation
file handling, dynamic memory allocation
indra Kishor
 
want to learn files,then just use this ppt to learn
want to learn files,then just use this ppt to learnwant to learn files,then just use this ppt to learn
want to learn files,then just use this ppt to learn
nalluribalaji157
 
File handling in c
File handling in cFile handling in c
File handling in c
aakanksha s
 
File handling With Solve Programs
File handling With Solve ProgramsFile handling With Solve Programs
File handling With Solve Programs
Rohan Gajre
 

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
Samsil Arefin
 
Evolution Phylogenetic
Evolution PhylogeneticEvolution Phylogenetic
Evolution Phylogenetic
Samsil Arefin
 
Evolution Phylogenetic
Evolution PhylogeneticEvolution Phylogenetic
Evolution Phylogenetic
Samsil Arefin
 
Ego net facebook data analysis
Ego net facebook data analysisEgo net facebook data analysis
Ego net facebook data analysis
Samsil 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 application
Samsil 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 inserting
Samsil Arefin
 
Number theory
Number theoryNumber theory
Number theory
Samsil 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 order
Samsil Arefin
 
Linked list int_data_fdata
Linked list int_data_fdataLinked list int_data_fdata
Linked list int_data_fdata
Samsil Arefin
 
Linked list Output tracing
Linked list Output tracingLinked list Output tracing
Linked list Output tracing
Samsil Arefin
 
Stack
StackStack
Stack
Samsil Arefin
 
Sorting
SortingSorting
Sorting
Samsil Arefin
 
Fundamentals of-electric-circuit
Fundamentals of-electric-circuitFundamentals of-electric-circuit
Fundamentals of-electric-circuit
Samsil Arefin
 
Cyber security
Cyber securityCyber security
Cyber security
Samsil Arefin
 
C programming
C programmingC programming
C programming
Samsil Arefin
 
Data structure lecture 1
Data structure   lecture 1Data structure   lecture 1
Data structure lecture 1
Samsil Arefin
 
Structure and union
Structure and unionStructure and union
Structure and union
Samsil 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 java
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 protocol
Samsil Arefin
 
Evolution Phylogenetic
Evolution PhylogeneticEvolution Phylogenetic
Evolution Phylogenetic
Samsil Arefin
 
Evolution Phylogenetic
Evolution PhylogeneticEvolution Phylogenetic
Evolution Phylogenetic
Samsil Arefin
 
Ego net facebook data analysis
Ego net facebook data analysisEgo net facebook data analysis
Ego net facebook data analysis
Samsil 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 application
Samsil 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 inserting
Samsil 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 order
Samsil Arefin
 
Linked list int_data_fdata
Linked list int_data_fdataLinked list int_data_fdata
Linked list int_data_fdata
Samsil Arefin
 
Linked list Output tracing
Linked list Output tracingLinked list Output tracing
Linked list Output tracing
Samsil Arefin
 
Fundamentals of-electric-circuit
Fundamentals of-electric-circuitFundamentals of-electric-circuit
Fundamentals of-electric-circuit
Samsil Arefin
 
Data structure lecture 1
Data structure   lecture 1Data structure   lecture 1
Data structure lecture 1
Samsil 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 java
Samsil Arefin
 

Recently uploaded (20)

world subdivision.pdf...................
world subdivision.pdf...................world subdivision.pdf...................
world subdivision.pdf...................
bmmederos10
 
Video Games and Artificial-Realities.pptx
Video Games and Artificial-Realities.pptxVideo Games and Artificial-Realities.pptx
Video Games and Artificial-Realities.pptx
HadiBadri1
 
Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)
elelijjournal653
 
ISO 4548-9 Oil Filter Anti Drain Catalogue.pdf
ISO 4548-9 Oil Filter Anti Drain Catalogue.pdfISO 4548-9 Oil Filter Anti Drain Catalogue.pdf
ISO 4548-9 Oil Filter Anti Drain Catalogue.pdf
FILTRATION ENGINEERING & CUNSULTANT
 
Software_Engineering_in_6_Hours_lyst1728638742594.pdf
Software_Engineering_in_6_Hours_lyst1728638742594.pdfSoftware_Engineering_in_6_Hours_lyst1728638742594.pdf
Software_Engineering_in_6_Hours_lyst1728638742594.pdf
VanshMunjal7
 
Introduction of Structural Audit and Health Montoring.pptx
Introduction of Structural Audit and Health Montoring.pptxIntroduction of Structural Audit and Health Montoring.pptx
Introduction of Structural Audit and Health Montoring.pptx
gunjalsachin
 
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.pptfy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
sukarnoamin
 
Structural Health and Factors affecting.pptx
Structural Health and Factors affecting.pptxStructural Health and Factors affecting.pptx
Structural Health and Factors affecting.pptx
gunjalsachin
 
Air Filter Flat Sheet Media-Catalouge-Final.pdf
Air Filter Flat Sheet Media-Catalouge-Final.pdfAir Filter Flat Sheet Media-Catalouge-Final.pdf
Air Filter Flat Sheet Media-Catalouge-Final.pdf
FILTRATION ENGINEERING & CUNSULTANT
 
Webinar On Steel Melting IIF of steel for rdso
Webinar  On Steel  Melting IIF of steel for rdsoWebinar  On Steel  Melting IIF of steel for rdso
Webinar On Steel Melting IIF of steel for rdso
KapilParyani3
 
Tesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia - A Leader In Her IndustryTesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia
 
ENERGY STORING DEVICES-Primary Battery.pdf
ENERGY STORING DEVICES-Primary Battery.pdfENERGY STORING DEVICES-Primary Battery.pdf
ENERGY STORING DEVICES-Primary Battery.pdf
TAMILISAI R
 
[HIFLUX] Lok Fitting&Valve Catalog 2025 (Eng)
[HIFLUX] Lok Fitting&Valve Catalog 2025 (Eng)[HIFLUX] Lok Fitting&Valve Catalog 2025 (Eng)
[HIFLUX] Lok Fitting&Valve Catalog 2025 (Eng)
하이플럭스 / HIFLUX Co., Ltd.
 
UNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCH
UNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCHUNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCH
UNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCH
Sridhar191373
 
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning ModelEnhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
IRJET Journal
 
MODULE 5 BUILDING PLANNING AND DESIGN SY BTECH ACOUSTICS SYSTEM IN BUILDING
MODULE 5 BUILDING PLANNING AND DESIGN SY BTECH ACOUSTICS SYSTEM IN BUILDINGMODULE 5 BUILDING PLANNING AND DESIGN SY BTECH ACOUSTICS SYSTEM IN BUILDING
MODULE 5 BUILDING PLANNING AND DESIGN SY BTECH ACOUSTICS SYSTEM IN BUILDING
Dr. BASWESHWAR JIRWANKAR
 
UNIT-5-PPT Computer Control Power of Power System
UNIT-5-PPT Computer Control Power of Power SystemUNIT-5-PPT Computer Control Power of Power System
UNIT-5-PPT Computer Control Power of Power System
Sridhar191373
 
Software Developer Portfolio: Backend Architecture & Performance Optimization
Software Developer Portfolio: Backend Architecture & Performance OptimizationSoftware Developer Portfolio: Backend Architecture & Performance Optimization
Software Developer Portfolio: Backend Architecture & Performance Optimization
kiwoong (daniel) kim
 
Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...
Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...
Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...
Mohamed905031
 
world subdivision.pdf...................
world subdivision.pdf...................world subdivision.pdf...................
world subdivision.pdf...................
bmmederos10
 
Video Games and Artificial-Realities.pptx
Video Games and Artificial-Realities.pptxVideo Games and Artificial-Realities.pptx
Video Games and Artificial-Realities.pptx
HadiBadri1
 
Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)Electrical and Electronics Engineering: An International Journal (ELELIJ)
Electrical and Electronics Engineering: An International Journal (ELELIJ)
elelijjournal653
 
Software_Engineering_in_6_Hours_lyst1728638742594.pdf
Software_Engineering_in_6_Hours_lyst1728638742594.pdfSoftware_Engineering_in_6_Hours_lyst1728638742594.pdf
Software_Engineering_in_6_Hours_lyst1728638742594.pdf
VanshMunjal7
 
Introduction of Structural Audit and Health Montoring.pptx
Introduction of Structural Audit and Health Montoring.pptxIntroduction of Structural Audit and Health Montoring.pptx
Introduction of Structural Audit and Health Montoring.pptx
gunjalsachin
 
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.pptfy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
sukarnoamin
 
Structural Health and Factors affecting.pptx
Structural Health and Factors affecting.pptxStructural Health and Factors affecting.pptx
Structural Health and Factors affecting.pptx
gunjalsachin
 
Webinar On Steel Melting IIF of steel for rdso
Webinar  On Steel  Melting IIF of steel for rdsoWebinar  On Steel  Melting IIF of steel for rdso
Webinar On Steel Melting IIF of steel for rdso
KapilParyani3
 
Tesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia - A Leader In Her IndustryTesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia
 
ENERGY STORING DEVICES-Primary Battery.pdf
ENERGY STORING DEVICES-Primary Battery.pdfENERGY STORING DEVICES-Primary Battery.pdf
ENERGY STORING DEVICES-Primary Battery.pdf
TAMILISAI R
 
UNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCH
UNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCHUNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCH
UNIT-4-PPT UNIT COMMITMENT AND ECONOMIC DISPATCH
Sridhar191373
 
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning ModelEnhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
IRJET Journal
 
MODULE 5 BUILDING PLANNING AND DESIGN SY BTECH ACOUSTICS SYSTEM IN BUILDING
MODULE 5 BUILDING PLANNING AND DESIGN SY BTECH ACOUSTICS SYSTEM IN BUILDINGMODULE 5 BUILDING PLANNING AND DESIGN SY BTECH ACOUSTICS SYSTEM IN BUILDING
MODULE 5 BUILDING PLANNING AND DESIGN SY BTECH ACOUSTICS SYSTEM IN BUILDING
Dr. BASWESHWAR JIRWANKAR
 
UNIT-5-PPT Computer Control Power of Power System
UNIT-5-PPT Computer Control Power of Power SystemUNIT-5-PPT Computer Control Power of Power System
UNIT-5-PPT Computer Control Power of Power System
Sridhar191373
 
Software Developer Portfolio: Backend Architecture & Performance Optimization
Software Developer Portfolio: Backend Architecture & Performance OptimizationSoftware Developer Portfolio: Backend Architecture & Performance Optimization
Software Developer Portfolio: Backend Architecture & Performance Optimization
kiwoong (daniel) kim
 
Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...
Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...
Numerical Investigation of the Aerodynamic Characteristics for a Darrieus H-t...
Mohamed905031
 

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