SlideShare a Scribd company logo
Mrs. G. Nagalakshmi, M.Sc., M.Phil.,
Ms. N. Malathi, M.Sc.,
Programming in C (18UAMA41)
II B.Sc. Mathematics (SF)
A file is a space in a memory where data is stored. 'C'
programming provides various functions to deal with a file. A
mechanism of manipulating with the files is called
as file management. A file must be opened before performing
operations on it. A file can be opened in a read, write or an append
mode.
Whenever we want to work with a file, the first step is to
create a file. A file is nothing but space in a memory where data is
stored.
To create a file in a 'C' program following syntax is used,
FILE *fp;
fp = fopen ("file_name", "mode");
fopen is a standard function which is used to open a file.
•If the file is not present on the system, then it is created and then
opened.
•If a file is already present on the system, then it is directly opened
using this function.
fp is a file pointer which points to the type file.
A file in 'C' programming can be created or opened for
reading/writing purposes. A mode is used to specify whether you
want to open a file for any of the below-given purposes. Following
are the different types of modes in 'C' programming which can be
used while working with a file.
File
Mode
Description
r Open a file for reading. If a file is in reading mode, then no
data is deleted if a file is already present on a system.
w Open a file for writing. If a file is in writing mode, then a new
file is created if a file doesn't exist at all. If a file is already
present on a system, then all the data inside the file is
truncated, and it is opened for writing purposes.
a Open a file in append mode. If a file is in append mode, then
the file is opened. The content within the file doesn't
change.
One should always close a file whenever the operations on file
are over. It means the contents and links to the file are terminated.
This prevents accidental damage to the file.
'C' provides the fclose function to perform file closing
operation. The syntax of fclose is as follows,
fclose (file_pointer);
There are three different functions dedicated to reading data from a
file
fgetc(file_pointer): It returns the next character from the file
pointed to by the file pointer. When the end of the file has been
reached, the EOF is sent back.
fgets(string, n, file_pointer): It reads n-1 characters from the file
and stores the string in which the NULL character '0' is appended as
the last character.
fscanf(file_pointer, conversion_specifiers, variable_adresses): It
is used to parse and analyze data. It reads characters from the file
and assigns the input to a list of variable pointers variable_adresses
using conversion specifiers. fscanf stops reading a string when space
or newline is encountered.
In C, when you write to a file, newline characters 'n' must be
explicitly added.
The stdio library offers the necessary functions to write to a file:
fputc(char, file_pointer): It writes a character to the file pointed to
by file_pointer.
fputs(str, file_pointer): It writes a string to the file pointed to by
file_pointer.
fprintf(file_pointer, str, variable_lists): It prints a string to the file
pointed to by file_pointer. The string can optionally include format
specifiers and a list of variables variable_lists.
While dealing with files, it is possible that an error may occur. This
error may occur due to following reasons:
•Reading beyond the end of file mark.
•Performing operations on the file that has not still been opened.
•Writing to a file that is opened in the read mode.
•Opening a file with invalid filename.
•Device overflow.
Thus to check the status of the pointer in the file and to detect the
error is the file. C provides two status-enquiry library functions
The feof() function can be used to test for an end of file
condition
Syntax
feof(FILE *file_pointer);
Example
if(feof(fp))
printf(“End of file”);
The ferror() function reports on the error state of the stream
and returns true if an error has occurred.
Syntax
ferror(FILE *file_pointer);
Example
if(ferror(fp)!=0)
printf(“An error has occurred”);
There is no need to read each record sequentially, if we want
to access a particular record. C supports these functions for random
access file processing.
•fseek()
•ftell()
•rewind()
This function is used for seeking the pointer position in the
file at the specified byte.
Syntax: fseek( file pointer, displacement, pointer position);
Where
file pointer ---- It is the pointer which points to the file.
displacement ---- It is positive or negative.This is the number of
bytes which are skipped backward (if negative) or forward( if
positive) from the current position.This is attached with L because
this is a long integer.
Examples:
1) fseek( p,10L,0)
0 means pointer position is on beginning of the file, from this
statement pointer position is skipped 10 bytes from the beginning of
the file.
2)fseek( p,5L,1)
1 means current position of the pointer position. From this statement
pointer position is skipped 5 bytes forward from the current position.
3)fseek(p,-5L,1)
From this statement pointer position is skipped 5 bytes backward
from the current position.
This function returns the value of the current pointer position in the
file. The value is count from the beginning of the file.
Syntax: ftell(fptr);
Where fptr is a file pointer.
This function is used to move the file pointer to the beginning of the
given file.
Syntax: rewind( fptr);
Where fptr is a file pointer.
Command line argument is a parameter supplied to the
program when it is invoked. Command line argument is an important
concept in C programming. It is mostly used when you need to
control your program from outside. Command line arguments are
passed to the main() method.
Syntax:
int main(int argc, char *argv[])
Here argc counts the number of arguments on the command line
and argv[ ] is a pointer array which holds pointers of type char which
points to the arguments passed to the program.
Programming in C

More Related Content

What's hot

File Management in C
File Management in CFile Management in C
File Management in C
Paurav Shah
 
File handling in C
File handling in CFile handling in C
File handling in C
Rabin BK
 
4 text file
4 text file4 text file
4 text file
hasan Mohammad
 
File Management
File ManagementFile Management
File Management
Ravinder Kamboj
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'
eShikshak
 
File handling in c
File handling in cFile handling in c
File handling in c
mohit biswal
 
UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in C
Ashim Lamichhane
 
Programming in C Session 4
Programming in C Session 4Programming in C Session 4
Programming in C Session 4
Prerna Sharma
 
File in C Programming
File in C ProgrammingFile in C Programming
File in C Programming
Sonya Akter Rupa
 
Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
Sowri Rajan
 
Read write program
Read write programRead write program
Read write programAMI AMITO
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
yndaravind
 
file handling1
file handling1file handling1
file handling1student
 
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 handling-c
File handling-cFile handling-c
File handling in 'C'
File handling in 'C'File handling in 'C'
File handling in 'C'
Gaurav Garg
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
YOGESH SINGH
 
C files
C filesC files

What's hot (20)

File Management in C
File Management in CFile Management in C
File Management in C
 
File handling in C
File handling in CFile handling in C
File handling in C
 
4 text file
4 text file4 text file
4 text file
 
File Management
File ManagementFile Management
File Management
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'
 
File handling in c
File handling in cFile handling in c
File handling in c
 
UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in C
 
Programming in C Session 4
Programming in C Session 4Programming in C Session 4
Programming in C Session 4
 
File in C Programming
File in C ProgrammingFile in C Programming
File in C Programming
 
Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
 
File operations in c
File operations in cFile operations in c
File operations in c
 
Read write program
Read write programRead write program
Read write program
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
file handling1
file handling1file handling1
file handling1
 
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 handling-c
File handling-cFile handling-c
File handling-c
 
File handling in 'C'
File handling in 'C'File handling in 'C'
File handling in 'C'
 
7.0 files and c input
7.0 files and c input7.0 files and c input
7.0 files and c input
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
C files
C filesC files
C files
 

Similar to Programming in C

File management
File managementFile management
File management
sumathiv9
 
File handling in c
File handling in cFile handling in c
File handling in c
aakanksha s
 
Unit-VI.pptx
Unit-VI.pptxUnit-VI.pptx
Unit-VI.pptx
Mehul Desai
 
Handout#01
Handout#01Handout#01
Handout#01
Sunita Milind Dol
 
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
sudhakargeruganti
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
Vikram Nandini
 
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
sangeeta borde
 
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
 
INput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptxINput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptx
AssadLeo1
 
Module 5 file cp
Module 5 file cpModule 5 file cp
Module 5 file cp
Amarjith C K
 
File management
File managementFile management
File management
lalithambiga kamaraj
 
File management
File managementFile management
File management
AnishaThakkar2
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
RavindraSalunke3
 
Unit V.pptx
Unit V.pptxUnit V.pptx
Unit V.pptx
ShaswatSurya
 
File Handling in C Part I
File Handling in C Part IFile Handling in C Part I
File Handling in C Part I
Arpana Awasthi
 
5 Structure & File.pptx
5 Structure & File.pptx5 Structure & File.pptx
5 Structure & File.pptx
aarockiaabinsAPIICSE
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
Praveen M Jigajinni
 

Similar to Programming in C (20)

File management
File managementFile management
File management
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Unit-VI.pptx
Unit-VI.pptxUnit-VI.pptx
Unit-VI.pptx
 
Handout#01
Handout#01Handout#01
Handout#01
 
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
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 
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
 
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
 
Unit5
Unit5Unit5
Unit5
 
INput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptxINput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptx
 
Module 5 file cp
Module 5 file cpModule 5 file cp
Module 5 file cp
 
File management
File managementFile management
File management
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Unit 8
Unit 8Unit 8
Unit 8
 
File management
File managementFile management
File management
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
 
Unit V.pptx
Unit V.pptxUnit V.pptx
Unit V.pptx
 
File Handling in C Part I
File Handling in C Part IFile Handling in C Part I
File Handling in C Part I
 
5 Structure & File.pptx
5 Structure & File.pptx5 Structure & File.pptx
5 Structure & File.pptx
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 

More from MalathiNagarajan20

Dynamics Apse and Apsidal Distance
Dynamics Apse and Apsidal DistanceDynamics Apse and Apsidal Distance
Dynamics Apse and Apsidal Distance
MalathiNagarajan20
 
Programming in C
Programming in CProgramming in C
Programming in C
MalathiNagarajan20
 
Dynamics problems
Dynamics problemsDynamics problems
Dynamics problems
MalathiNagarajan20
 
Dynamics ppt
Dynamics pptDynamics ppt
Dynamics ppt
MalathiNagarajan20
 
Numerical analysis ppt
Numerical analysis pptNumerical analysis ppt
Numerical analysis ppt
MalathiNagarajan20
 
C
CC
Programming in c (18 uama41)(1)
Programming in c (18 uama41)(1)Programming in c (18 uama41)(1)
Programming in c (18 uama41)(1)
MalathiNagarajan20
 
Numerical analysis (15 umtc64)
Numerical analysis (15 umtc64)Numerical analysis (15 umtc64)
Numerical analysis (15 umtc64)
MalathiNagarajan20
 
Dynamics (18 umtc41)
Dynamics (18 umtc41)Dynamics (18 umtc41)
Dynamics (18 umtc41)
MalathiNagarajan20
 
Statics
StaticsStatics
Transforms
TransformsTransforms
Transforms
MalathiNagarajan20
 
Transforms
TransformsTransforms
Transforms
MalathiNagarajan20
 
Statics
StaticsStatics

More from MalathiNagarajan20 (13)

Dynamics Apse and Apsidal Distance
Dynamics Apse and Apsidal DistanceDynamics Apse and Apsidal Distance
Dynamics Apse and Apsidal Distance
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Dynamics problems
Dynamics problemsDynamics problems
Dynamics problems
 
Dynamics ppt
Dynamics pptDynamics ppt
Dynamics ppt
 
Numerical analysis ppt
Numerical analysis pptNumerical analysis ppt
Numerical analysis ppt
 
C
CC
C
 
Programming in c (18 uama41)(1)
Programming in c (18 uama41)(1)Programming in c (18 uama41)(1)
Programming in c (18 uama41)(1)
 
Numerical analysis (15 umtc64)
Numerical analysis (15 umtc64)Numerical analysis (15 umtc64)
Numerical analysis (15 umtc64)
 
Dynamics (18 umtc41)
Dynamics (18 umtc41)Dynamics (18 umtc41)
Dynamics (18 umtc41)
 
Statics
StaticsStatics
Statics
 
Transforms
TransformsTransforms
Transforms
 
Transforms
TransformsTransforms
Transforms
 
Statics
StaticsStatics
Statics
 

Recently uploaded

Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 

Recently uploaded (20)

Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 

Programming in C

  • 1. Mrs. G. Nagalakshmi, M.Sc., M.Phil., Ms. N. Malathi, M.Sc., Programming in C (18UAMA41) II B.Sc. Mathematics (SF)
  • 2.
  • 3. A file is a space in a memory where data is stored. 'C' programming provides various functions to deal with a file. A mechanism of manipulating with the files is called as file management. A file must be opened before performing operations on it. A file can be opened in a read, write or an append mode.
  • 4. Whenever we want to work with a file, the first step is to create a file. A file is nothing but space in a memory where data is stored. To create a file in a 'C' program following syntax is used, FILE *fp; fp = fopen ("file_name", "mode"); fopen is a standard function which is used to open a file. •If the file is not present on the system, then it is created and then opened. •If a file is already present on the system, then it is directly opened using this function. fp is a file pointer which points to the type file.
  • 5. A file in 'C' programming can be created or opened for reading/writing purposes. A mode is used to specify whether you want to open a file for any of the below-given purposes. Following are the different types of modes in 'C' programming which can be used while working with a file.
  • 6. File Mode Description r Open a file for reading. If a file is in reading mode, then no data is deleted if a file is already present on a system. w Open a file for writing. If a file is in writing mode, then a new file is created if a file doesn't exist at all. If a file is already present on a system, then all the data inside the file is truncated, and it is opened for writing purposes. a Open a file in append mode. If a file is in append mode, then the file is opened. The content within the file doesn't change.
  • 7. One should always close a file whenever the operations on file are over. It means the contents and links to the file are terminated. This prevents accidental damage to the file. 'C' provides the fclose function to perform file closing operation. The syntax of fclose is as follows, fclose (file_pointer);
  • 8. There are three different functions dedicated to reading data from a file fgetc(file_pointer): It returns the next character from the file pointed to by the file pointer. When the end of the file has been reached, the EOF is sent back. fgets(string, n, file_pointer): It reads n-1 characters from the file and stores the string in which the NULL character '0' is appended as the last character. fscanf(file_pointer, conversion_specifiers, variable_adresses): It is used to parse and analyze data. It reads characters from the file and assigns the input to a list of variable pointers variable_adresses using conversion specifiers. fscanf stops reading a string when space or newline is encountered.
  • 9. In C, when you write to a file, newline characters 'n' must be explicitly added. The stdio library offers the necessary functions to write to a file: fputc(char, file_pointer): It writes a character to the file pointed to by file_pointer. fputs(str, file_pointer): It writes a string to the file pointed to by file_pointer. fprintf(file_pointer, str, variable_lists): It prints a string to the file pointed to by file_pointer. The string can optionally include format specifiers and a list of variables variable_lists.
  • 10. While dealing with files, it is possible that an error may occur. This error may occur due to following reasons: •Reading beyond the end of file mark. •Performing operations on the file that has not still been opened. •Writing to a file that is opened in the read mode. •Opening a file with invalid filename. •Device overflow. Thus to check the status of the pointer in the file and to detect the error is the file. C provides two status-enquiry library functions
  • 11. The feof() function can be used to test for an end of file condition Syntax feof(FILE *file_pointer); Example if(feof(fp)) printf(“End of file”);
  • 12. The ferror() function reports on the error state of the stream and returns true if an error has occurred. Syntax ferror(FILE *file_pointer); Example if(ferror(fp)!=0) printf(“An error has occurred”);
  • 13. There is no need to read each record sequentially, if we want to access a particular record. C supports these functions for random access file processing. •fseek() •ftell() •rewind()
  • 14. This function is used for seeking the pointer position in the file at the specified byte. Syntax: fseek( file pointer, displacement, pointer position); Where file pointer ---- It is the pointer which points to the file. displacement ---- It is positive or negative.This is the number of bytes which are skipped backward (if negative) or forward( if positive) from the current position.This is attached with L because this is a long integer.
  • 15. Examples: 1) fseek( p,10L,0) 0 means pointer position is on beginning of the file, from this statement pointer position is skipped 10 bytes from the beginning of the file. 2)fseek( p,5L,1) 1 means current position of the pointer position. From this statement pointer position is skipped 5 bytes forward from the current position. 3)fseek(p,-5L,1) From this statement pointer position is skipped 5 bytes backward from the current position.
  • 16. This function returns the value of the current pointer position in the file. The value is count from the beginning of the file. Syntax: ftell(fptr); Where fptr is a file pointer.
  • 17. This function is used to move the file pointer to the beginning of the given file. Syntax: rewind( fptr); Where fptr is a file pointer.
  • 18. Command line argument is a parameter supplied to the program when it is invoked. Command line argument is an important concept in C programming. It is mostly used when you need to control your program from outside. Command line arguments are passed to the main() method. Syntax: int main(int argc, char *argv[]) Here argc counts the number of arguments on the command line and argv[ ] is a pointer array which holds pointers of type char which points to the arguments passed to the program.