SlideShare a Scribd company logo
8
Most read
11
Most read
12
Most read
File operations in c
Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Ltd
Typing Speed
Week

Target Achieved

1

40

23

2

40

25

3

40

25
Jobs Applied
Week
1
2
3

Company

Designation

Applied Date

Current Status
File Operations in c

Muhammed Noufal V T
muhammednoufalvt@gmail.com
www.facebook.com/vtnoufalvt
twitter.com/noufalurnappy
in.linkedin.com/pub/muhammed-noufal
9744003056
File management
• Data can be stored on the disk and read whenever necessary, without destroying
the data
• C uses a structure called FILE (defined in stdio.h) to store the attributes of a file.
• FILE- Place on the disk where a group of related data is stored
• C supports a number of functions that have the ability to perform basic file
operations
-Naming a file
-Opening a file
-Reading data from a file
-Writing data to a file
-Closing a file
Basic file operations
• C provides several different file operations
• fopen - open a file- specify how its opened (read/write) and type
(binary/text)
• fclose - close an opened file
• fread - read from a file
• fwrite - write to a file
• fseek/fsetpos - move a file pointer to somewhere in a file.
• ftell/fgetpos - tell you where the file pointer is located.
Different modes

• Writing mode
– if file already exists then contents are deleted,
– else new file with specified name created
• Appending mode
– if file already exists then file opened with contents safe
– else new file created
• Reading mode
– if file already exists then opened with contents safe
– else error occurs.
• Additional modes
– r+ open to beginning for both reading/writing
– w+ same as w except both for reading and writing
– a+ same as ‘a’ except both for reading and writing
Opening a FILE

• Syntax
-FILE *fp; /*variable fp is pointer to type FILE*/
fp = fopen(“filename”, “mode”); /*opens file with name filename , assigns
identifier to fp */
• fp
– contains all information about file
– Communication link between system and program
• The file mode tells C how the program will use the file.
• The filename indicates the system name and location for the file.
• We assign the return value of fopen to our pointer variable:
fp = fopen(“MYFILE.TXT”, “w”);
fp = fopen(“A:MYFILE.TXT”, “w”);
Closing a FILE

• When we finish with a mode, we need to close the file before ending the program or
beginning another mode with that same file.
• To close a file, we use fclose and the pointer variable:
fclose(spData);
• Example:
FILE *p1;
p1 = fopen(“INPUT.txt”, “r”);
……..
……..
fclose(p1);
• pointer can be reused after closing
Input/output operations on FILES
•
•
•
•
•
•
•

C provides several different functions for reading/writing
getc() – read a character
putc() – write a character
fprintf() – write set of data values
fscanf() – read set of data values
getw() – read integer
putw() – write integer
getc() & putc()
• handle one character at a time like getchar() and putchar()
• syntax: putc(c,fp1);
– c : a character variable
– fp1 : pointer to file opened with mode w
• syntax: c = getc(fp2);
– c : a character variable
– fp2 : pointer to file opened with mode r
• file pointer moves by one character position after every getc() and putc()
• getc() returns end-of-file marker EOF when file end reached
Program to read/write using getc() & putc()
•

#include <stdio.h>
main()
{
FILE *fp1;
char c;
f1= fopen(“INPUT”, “w”);
while((c=getchar()) != EOF)
putc(c,f1);
fclose(f1);
f1=fopen(“INPUT”, “r”);
while((c=getc(f1))!=EOF)
printf(“%c”, c);
fclose(f1);
} /*end main */

/* open file for writing */
/*get char from keyboard until CTL-Z*/
/*write a character to INPUT */
/* close INPUT */
/* reopen file */
/*read character from file INPUT*/
/* print character to screen */
fprintf() & fscanf
• similar to scanf() and printf()
• Syntax:- fprintf()
fprintf (fp,"string",variables);
Example:
int i = 12;
float x = 2.356;
char ch = 's';
FILE *fp;
fp=fopen(“out.txt”,”w”);
fprintf (fp, "%d %f %c", i, x, ch);

• Syntax:- fscanf()
fscanf (fp,"string",identifiers);
Example:
FILE *fp;
Fp=fopen(“input.txt”,”r”);
int i;
fscanf (fp,“%d",i);
• fscanf() returns EOF when end-of file
reached
getw() & putw()
• handle one integer at a time
• syntax: putw(i,fp1);

– i : an integer variable
– fp1 : pointer to file ipened with mode w

• syntax: i = getw(fp2);

– i : an integer variable
– fp2 : pointer to file opened with mode r

• file pointer moves by one integer position, data stored in binary format native
to local system
• getw() returns end-of-file marker EOF when file end reached
fread()
• Declaration:- fread(void *ptr, size, n, FILE *stream);
• Remarks:
fread reads a specified number of equal-sized data items from an input stream into a block.
– ptr = Points to a block into which data is read
– size = Length of each item read, in bytes
– n = Number of items read
– stream = file pointer
• Example
char a[10]={'1','2','3','4','5','6','7','8','9','a'};
FILE *fs;
fs=fopen("Project.txt","w");
fwrite(a,1,10,fs);
fclose(fs)
fwrite()
• Declaration:
fwrite(const void *ptr, size, n, FILE*stream);
• Remarks:
fwrite appends a specified number of equal-sized data items to an output file.
– ptr = Pointer to any object; the data written begins at ptr
– size = Length of each item of data
– n =Number of data items to be appended
– stream = file pointer
• Example
char a[10]={'1','2','3','4','5','6','7','8','9','a'};
FILE *fs;
fs=fopen("Project.txt","w");
fwrite(a,1,10,fs);
fclose(fs);
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.

Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550

Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com

NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550

More Related Content

PPTX
File handling in C
Kamal Acharya
 
PPTX
Human vs computer
Taminul Islam
 
PPTX
Canonical form and Standard form of LPP
Sundar B N
 
PPTX
Machine Learning ppt
Student Conscious Club
 
PPT
Advances in oncological PET/CT Imaging
Hussein Farghaly
 
PPT
Band theory of solid
Keyur Patel
 
PPT
Fundamentals Of Computer
Jack Frost
 
File handling in C
Kamal Acharya
 
Human vs computer
Taminul Islam
 
Canonical form and Standard form of LPP
Sundar B N
 
Machine Learning ppt
Student Conscious Club
 
Advances in oncological PET/CT Imaging
Hussein Farghaly
 
Band theory of solid
Keyur Patel
 
Fundamentals Of Computer
Jack Frost
 

What's hot (20)

PPTX
Functions in C
Kamal Acharya
 
PPTX
File in C language
Manash Kumar Mondal
 
PPT
File handling in c
David Livingston J
 
PPTX
Strings in C
Kamal Acharya
 
PPTX
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
PPTX
File handling in c
aakanksha s
 
PPTX
Array Of Pointers
Sharad Dubey
 
PPT
File in c
Prabhu Govind
 
PPTX
Structures in c language
tanmaymodi4
 
PPT
Pointers C programming
Appili Vamsi Krishna
 
PPT
Class and object in C++
rprajat007
 
PPTX
Union in C programming
Kamal Acharya
 
PDF
Python file handling
Prof. Dr. K. Adisesha
 
PPTX
Function in C program
Nurul Zakiah Zamri Tan
 
PPT
File handling in C++
Hitesh Kumar
 
PPTX
Doubly Linked List
Ninad Mankar
 
PPTX
Programming in c Arrays
janani thirupathi
 
PPTX
C functions
University of Potsdam
 
PPTX
Constructor and Types of Constructors
Dhrumil Panchal
 
PPTX
Unit 4 python -list methods
narmadhakin
 
Functions in C
Kamal Acharya
 
File in C language
Manash Kumar Mondal
 
File handling in c
David Livingston J
 
Strings in C
Kamal Acharya
 
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
File handling in c
aakanksha s
 
Array Of Pointers
Sharad Dubey
 
File in c
Prabhu Govind
 
Structures in c language
tanmaymodi4
 
Pointers C programming
Appili Vamsi Krishna
 
Class and object in C++
rprajat007
 
Union in C programming
Kamal Acharya
 
Python file handling
Prof. Dr. K. Adisesha
 
Function in C program
Nurul Zakiah Zamri Tan
 
File handling in C++
Hitesh Kumar
 
Doubly Linked List
Ninad Mankar
 
Programming in c Arrays
janani thirupathi
 
Constructor and Types of Constructors
Dhrumil Panchal
 
Unit 4 python -list methods
narmadhakin
 
Ad

Similar to File operations in c (20)

PPTX
File management
sumathiv9
 
PPSX
1file handling
Frijo Francis
 
PPTX
file handling in c programming with file functions
10300PEDDIKISHOR
 
PPT
C-Programming Chapter 5 File-handling-C.ppt
sahakrishnan
 
PDF
Module 03 File Handling in C
Tushar B Kute
 
PPT
C-Programming Chapter 5 File-handling-C.ppt
ssuserad38541
 
PPTX
Engineering Computers L34-L35-File Handling.pptx
happycocoman
 
PPTX
want to learn files,then just use this ppt to learn
nalluribalaji157
 
PPT
File Management
Ravinder Kamboj
 
PPTX
File management
lalithambiga kamaraj
 
PPT
Unit5
mrecedu
 
PPT
How to do file-handling - in C language
SwatiAtulJoshi
 
PDF
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
sudhakargeruganti
 
PPT
File handling-c
CGC Technical campus,Mohali
 
PPTX
files c programming handling in computer programming
chatakonduyaswanth24
 
PPTX
MODULE 8-File and preprocessor.pptx for c program learners easy learning
a50905877
 
PPTX
Programming in C Session 4
Prerna Sharma
 
PDF
637225560972186380.pdf
SureshKalirawna
 
PPTX
File handling With Solve Programs
Rohan Gajre
 
PDF
file_c.pdf
Osmania University
 
File management
sumathiv9
 
1file handling
Frijo Francis
 
file handling in c programming with file functions
10300PEDDIKISHOR
 
C-Programming Chapter 5 File-handling-C.ppt
sahakrishnan
 
Module 03 File Handling in C
Tushar B Kute
 
C-Programming Chapter 5 File-handling-C.ppt
ssuserad38541
 
Engineering Computers L34-L35-File Handling.pptx
happycocoman
 
want to learn files,then just use this ppt to learn
nalluribalaji157
 
File Management
Ravinder Kamboj
 
File management
lalithambiga kamaraj
 
Unit5
mrecedu
 
How to do file-handling - in C language
SwatiAtulJoshi
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
sudhakargeruganti
 
files c programming handling in computer programming
chatakonduyaswanth24
 
MODULE 8-File and preprocessor.pptx for c program learners easy learning
a50905877
 
Programming in C Session 4
Prerna Sharma
 
637225560972186380.pdf
SureshKalirawna
 
File handling With Solve Programs
Rohan Gajre
 
file_c.pdf
Osmania University
 
Ad

More from baabtra.com - No. 1 supplier of quality freshers (20)

PPTX
Agile methodology and scrum development
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Acquiring new skills what you should know
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Baabtra.com programming at school
baabtra.com - No. 1 supplier of quality freshers
 
PDF
99LMS for Enterprises - LMS that you will love
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 6 database normalisation
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 5 transactions and dcl statements
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 4 functions, views, indexing
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 3 stored procedures
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Agile methodology and scrum development
baabtra.com - No. 1 supplier of quality freshers
 
Acquiring new skills what you should know
baabtra.com - No. 1 supplier of quality freshers
 
Baabtra.com programming at school
baabtra.com - No. 1 supplier of quality freshers
 
99LMS for Enterprises - LMS that you will love
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 6 database normalisation
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 5 transactions and dcl statements
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 4 functions, views, indexing
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 

Recently uploaded (20)

PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
How to Apply for a Job From Odoo 18 Website
Celine George
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
Virus sequence retrieval from NCBI database
yamunaK13
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
How to Apply for a Job From Odoo 18 Website
Celine George
 

File operations in c

  • 2. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  • 5. File Operations in c Muhammed Noufal V T [email protected] www.facebook.com/vtnoufalvt twitter.com/noufalurnappy in.linkedin.com/pub/muhammed-noufal 9744003056
  • 6. File management • Data can be stored on the disk and read whenever necessary, without destroying the data • C uses a structure called FILE (defined in stdio.h) to store the attributes of a file. • FILE- Place on the disk where a group of related data is stored • C supports a number of functions that have the ability to perform basic file operations -Naming a file -Opening a file -Reading data from a file -Writing data to a file -Closing a file
  • 7. Basic file operations • C provides several different file operations • fopen - open a file- specify how its opened (read/write) and type (binary/text) • fclose - close an opened file • fread - read from a file • fwrite - write to a file • fseek/fsetpos - move a file pointer to somewhere in a file. • ftell/fgetpos - tell you where the file pointer is located.
  • 8. Different modes • Writing mode – if file already exists then contents are deleted, – else new file with specified name created • Appending mode – if file already exists then file opened with contents safe – else new file created • Reading mode – if file already exists then opened with contents safe – else error occurs. • Additional modes – r+ open to beginning for both reading/writing – w+ same as w except both for reading and writing – a+ same as ‘a’ except both for reading and writing
  • 9. Opening a FILE • Syntax -FILE *fp; /*variable fp is pointer to type FILE*/ fp = fopen(“filename”, “mode”); /*opens file with name filename , assigns identifier to fp */ • fp – contains all information about file – Communication link between system and program • The file mode tells C how the program will use the file. • The filename indicates the system name and location for the file. • We assign the return value of fopen to our pointer variable: fp = fopen(“MYFILE.TXT”, “w”); fp = fopen(“A:MYFILE.TXT”, “w”);
  • 10. Closing a FILE • When we finish with a mode, we need to close the file before ending the program or beginning another mode with that same file. • To close a file, we use fclose and the pointer variable: fclose(spData); • Example: FILE *p1; p1 = fopen(“INPUT.txt”, “r”); …….. …….. fclose(p1); • pointer can be reused after closing
  • 11. Input/output operations on FILES • • • • • • • C provides several different functions for reading/writing getc() – read a character putc() – write a character fprintf() – write set of data values fscanf() – read set of data values getw() – read integer putw() – write integer
  • 12. getc() & putc() • handle one character at a time like getchar() and putchar() • syntax: putc(c,fp1); – c : a character variable – fp1 : pointer to file opened with mode w • syntax: c = getc(fp2); – c : a character variable – fp2 : pointer to file opened with mode r • file pointer moves by one character position after every getc() and putc() • getc() returns end-of-file marker EOF when file end reached
  • 13. Program to read/write using getc() & putc() • #include <stdio.h> main() { FILE *fp1; char c; f1= fopen(“INPUT”, “w”); while((c=getchar()) != EOF) putc(c,f1); fclose(f1); f1=fopen(“INPUT”, “r”); while((c=getc(f1))!=EOF) printf(“%c”, c); fclose(f1); } /*end main */ /* open file for writing */ /*get char from keyboard until CTL-Z*/ /*write a character to INPUT */ /* close INPUT */ /* reopen file */ /*read character from file INPUT*/ /* print character to screen */
  • 14. fprintf() & fscanf • similar to scanf() and printf() • Syntax:- fprintf() fprintf (fp,"string",variables); Example: int i = 12; float x = 2.356; char ch = 's'; FILE *fp; fp=fopen(“out.txt”,”w”); fprintf (fp, "%d %f %c", i, x, ch); • Syntax:- fscanf() fscanf (fp,"string",identifiers); Example: FILE *fp; Fp=fopen(“input.txt”,”r”); int i; fscanf (fp,“%d",i); • fscanf() returns EOF when end-of file reached
  • 15. getw() & putw() • handle one integer at a time • syntax: putw(i,fp1); – i : an integer variable – fp1 : pointer to file ipened with mode w • syntax: i = getw(fp2); – i : an integer variable – fp2 : pointer to file opened with mode r • file pointer moves by one integer position, data stored in binary format native to local system • getw() returns end-of-file marker EOF when file end reached
  • 16. fread() • Declaration:- fread(void *ptr, size, n, FILE *stream); • Remarks: fread reads a specified number of equal-sized data items from an input stream into a block. – ptr = Points to a block into which data is read – size = Length of each item read, in bytes – n = Number of items read – stream = file pointer • Example char a[10]={'1','2','3','4','5','6','7','8','9','a'}; FILE *fs; fs=fopen("Project.txt","w"); fwrite(a,1,10,fs); fclose(fs)
  • 17. fwrite() • Declaration: fwrite(const void *ptr, size, n, FILE*stream); • Remarks: fwrite appends a specified number of equal-sized data items to an output file. – ptr = Pointer to any object; the data written begins at ptr – size = Length of each item of data – n =Number of data items to be appended – stream = file pointer • Example char a[10]={'1','2','3','4','5','6','7','8','9','a'}; FILE *fs; fs=fopen("Project.txt","w"); fwrite(a,1,10,fs); fclose(fs);
  • 18. If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  • 19. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: [email protected] NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550