SlideShare a Scribd company logo
1 of 4
Files let you store data on secondary storage such as a hard disk so that your programs
can later retrieve that data again. There are 2 types of files which are text files and
data files.
Text files
Text files are used for storing character strings in a file. To create a text file you must
first declare a file pointer.
#include<stdio.h>
int main()
{
FILE *f;
return 0;
}
You must then open the file using the fopen function. fopen takes 2 parameters which
are the file name and the open mode. fopen returns a file pointer which we will assign
to the file pointer called f.
#include<stdio.h>
int main()
{
FILE *f;
f = fopen("test.txt","w");
return 0;
}
The above example will create a file called test.txt. The "w" means that the file is
being opened for writing and if the file does not exist then it will be created. Here are
some other open modes:
r Open for reading
r+ Open for reading and writing
w
Open for writing and create the file if it does not exist. If the file exists then make
it blank.
w+
Open for reading and writing and create the file if it does not exist. If the file
exists then make it blank.
a
Open for appending(writing at the end of file) and create the file if it does not
exist.
a+ Open for reading and appending and create the file if it does not exist.
To write a string to the file you must use the fprintf command. fprintf is just like printf
except that you must use the file pointer as the first parameter.
#include<stdio.h>
int main()
{
FILE *f;
f = fopen("test.txt","w");
fprintf(f,"Hello");
return 0;
}
When you are finished using a file you must always close it. If you do not close a file
then some of the data might not be written to it. Use the fclose commmand to close a
file.
#include<stdio.h>
int main()
{
FILE *f;
f = fopen("test.txt","w");
fprintf(f,"Hello");
fclose(f);
return 0;
}
The fgets command is used when reading from a text file. You must first declare a
character array as a buffer to store the string that is read from the file. The 3
parameters for fgets are the buffer variable, the size of the buffer and the file pointer.
#include<stdio.h>
int main()
{
FILE *f;
char buf[100];
f = fopen("test.txt","r");
fgets(buf,sizeof(buf),f);
fclose(f);
printf("%sn",buf);
return 0;
}
Data files
A data file is used to store types of data such as integers. When you open a data file
you must add the letter b to the open mode parameter of fopen.
#include<stdio.h>
int main()
{
FILE *f;
f = fopen("test.dat","wb");
fclose(f);
return 0;
}
fwrite is used to write to a file. The first parameter of fwrite is a pointer to the variable
that you want to write to the file. The second parameter is the size of the variable that
must e written. The third parameter is the number of variables to be written. The
fourth parameter is the file pointer of the file you want to write to.
#include<stdio.h>
int main()
{
FILE *f;
int buf;
f = fopen("test.dat","wb");
buf = 100;
fwrite(&buf,sizeof(buf),1,f);
fclose(f);
return 0;
}
fread is used to read from a file and is the same as fwrite except that the data is read
into the variable instead of writing from it. You must remember to change the file
mode to read.
#include<stdio.h>
int main()
{
FILE *f;
int buf;
f = fopen("test.dat","rb");
fread(&buf,sizeof(buf),1,f);
printf("%dn",buf);
fclose(f);
return 0;
}
Data files using structures
You can read and write structures to data files in the same way as you do with any
other data type. Here is an example:
#include<stdio.h>
struct
{
char name[100];
int age;
} p;
int main()
{
FILE *f;
strcpy(p.name,"John");
p.age = 25;
f = fopen("test.dat","wb");
fwrite(&p,1,sizeof(p),f);
fclose(f);
return 0;
}

More Related Content

What's hot

What's hot (18)

File in C language
File in C languageFile in C language
File in C language
 
Programming in C
Programming in CProgramming in C
Programming in C
 
File handling in c
File  handling in cFile  handling in c
File handling in c
 
File management and handling by prabhakar
File management and handling by prabhakarFile management and handling by prabhakar
File management and handling by prabhakar
 
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
 
Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
 
Programming in C
Programming in CProgramming in C
Programming in C
 
File handling in 'C'
File handling in 'C'File handling in 'C'
File handling in 'C'
 
File handling in c
File handling in c File handling in c
File handling in c
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'
 
PHP file handling
PHP file handling PHP file handling
PHP file handling
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
Php files
Php filesPhp files
Php files
 
Files in C
Files in CFiles in C
Files in C
 
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
 
Files and Directories in PHP
Files and Directories in PHPFiles and Directories in PHP
Files and Directories in PHP
 
OSFile#2
OSFile#2OSFile#2
OSFile#2
 

Viewers also liked (9)

Paul mathieson
Paul mathiesonPaul mathieson
Paul mathieson
 
Dan Whittacre 2014 PA-PAC US House Questionnaire
Dan Whittacre 2014 PA-PAC US House QuestionnaireDan Whittacre 2014 PA-PAC US House Questionnaire
Dan Whittacre 2014 PA-PAC US House Questionnaire
 
Farmasi Ukraine Каталог Фармаси Украина апрель 2014
Farmasi Ukraine Каталог Фармаси Украина апрель 2014Farmasi Ukraine Каталог Фармаси Украина апрель 2014
Farmasi Ukraine Каталог Фармаси Украина апрель 2014
 
A2 Media Question 4.2
A2 Media Question 4.2A2 Media Question 4.2
A2 Media Question 4.2
 
AS Media Evaluation Question 6
AS Media Evaluation Question 6 AS Media Evaluation Question 6
AS Media Evaluation Question 6
 
Wk4 pioneerofplay presentation
Wk4 pioneerofplay presentationWk4 pioneerofplay presentation
Wk4 pioneerofplay presentation
 
Fertilizantes y su uso
Fertilizantes y su usoFertilizantes y su uso
Fertilizantes y su uso
 
Les glaceres
Les glaceres Les glaceres
Les glaceres
 
Thomas Poole 2014 PA PAC Questionnaire
Thomas Poole 2014 PA PAC QuestionnaireThomas Poole 2014 PA PAC Questionnaire
Thomas Poole 2014 PA PAC Questionnaire
 

Similar to Files let you store data on secondary storage such as a hard disk so that your programs can later retrieve that data again

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
Muhammed Thanveer M
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10
patcha535
 

Similar to Files let you store data on secondary storage such as a hard disk so that your programs can later retrieve that data again (20)

Unit-VI.pptx
Unit-VI.pptxUnit-VI.pptx
Unit-VI.pptx
 
Module 5 file cp
Module 5 file cpModule 5 file cp
Module 5 file cp
 
File handling
File handlingFile handling
File handling
 
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
 
Satz1
Satz1Satz1
Satz1
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Unit 8
Unit 8Unit 8
Unit 8
 
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 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
 
Python Files I_O17.pdf
Python Files I_O17.pdfPython Files I_O17.pdf
Python Files I_O17.pdf
 
File handling in c
File handling in cFile handling in c
File handling in c
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Python file handlings
Python file handlingsPython file handlings
Python file handlings
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 
Unit v
Unit vUnit v
Unit v
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10
 
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.ppt
file.pptfile.ppt
file.ppt
 

Recently uploaded

Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 

Recently uploaded (20)

Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 

Files let you store data on secondary storage such as a hard disk so that your programs can later retrieve that data again

  • 1. Files let you store data on secondary storage such as a hard disk so that your programs can later retrieve that data again. There are 2 types of files which are text files and data files. Text files Text files are used for storing character strings in a file. To create a text file you must first declare a file pointer. #include<stdio.h> int main() { FILE *f; return 0; } You must then open the file using the fopen function. fopen takes 2 parameters which are the file name and the open mode. fopen returns a file pointer which we will assign to the file pointer called f. #include<stdio.h> int main() { FILE *f; f = fopen("test.txt","w"); return 0; } The above example will create a file called test.txt. The "w" means that the file is being opened for writing and if the file does not exist then it will be created. Here are some other open modes: r Open for reading r+ Open for reading and writing w Open for writing and create the file if it does not exist. If the file exists then make it blank. w+ Open for reading and writing and create the file if it does not exist. If the file exists then make it blank. a Open for appending(writing at the end of file) and create the file if it does not exist. a+ Open for reading and appending and create the file if it does not exist.
  • 2. To write a string to the file you must use the fprintf command. fprintf is just like printf except that you must use the file pointer as the first parameter. #include<stdio.h> int main() { FILE *f; f = fopen("test.txt","w"); fprintf(f,"Hello"); return 0; } When you are finished using a file you must always close it. If you do not close a file then some of the data might not be written to it. Use the fclose commmand to close a file. #include<stdio.h> int main() { FILE *f; f = fopen("test.txt","w"); fprintf(f,"Hello"); fclose(f); return 0; } The fgets command is used when reading from a text file. You must first declare a character array as a buffer to store the string that is read from the file. The 3 parameters for fgets are the buffer variable, the size of the buffer and the file pointer. #include<stdio.h> int main() { FILE *f; char buf[100]; f = fopen("test.txt","r"); fgets(buf,sizeof(buf),f); fclose(f); printf("%sn",buf); return 0; } Data files A data file is used to store types of data such as integers. When you open a data file you must add the letter b to the open mode parameter of fopen.
  • 3. #include<stdio.h> int main() { FILE *f; f = fopen("test.dat","wb"); fclose(f); return 0; } fwrite is used to write to a file. The first parameter of fwrite is a pointer to the variable that you want to write to the file. The second parameter is the size of the variable that must e written. The third parameter is the number of variables to be written. The fourth parameter is the file pointer of the file you want to write to. #include<stdio.h> int main() { FILE *f; int buf; f = fopen("test.dat","wb"); buf = 100; fwrite(&buf,sizeof(buf),1,f); fclose(f); return 0; } fread is used to read from a file and is the same as fwrite except that the data is read into the variable instead of writing from it. You must remember to change the file mode to read. #include<stdio.h> int main() { FILE *f; int buf; f = fopen("test.dat","rb"); fread(&buf,sizeof(buf),1,f); printf("%dn",buf); fclose(f); return 0; } Data files using structures You can read and write structures to data files in the same way as you do with any other data type. Here is an example:
  • 4. #include<stdio.h> struct { char name[100]; int age; } p; int main() { FILE *f; strcpy(p.name,"John"); p.age = 25; f = fopen("test.dat","wb"); fwrite(&p,1,sizeof(p),f); fclose(f); return 0; }