SlideShare a Scribd company logo
Jagannath Institute of Management Sciences
Vasant Kunj-II, New Delhi - 110070
Subject Name: Programming In C
Department of Information Technology
Created By: Dr. Arpana Chaturvedi
@Dr. Arpana Chaturvedi
Subject: Programming In C
Topic: Unit III- Part II
Concept of Files
@Dr. Arpana Chaturvedi
Topics to be Covered
▰ Concept of Files
▰ File opening in various modes and closing of a file
▰ Unformatted Input and Output Functions
▰ Formatted Input Output Functions
▰ Reading from a Binary file
▰ Writing onto a Binary file
Concept of Files in C
@Dr. Arpana Chaturvedi
Concept of Files in C
@Dr. Arpana Chaturvedi
▰ A file is an external collection of related data treated as a single unit.
▰ The primary purpose of a file is to keep a record of data.
▰ Since the contents of primary memory are lost when the computer is shut
down, we need files to store our data in a more permanent form.
▰ Hence we say, File is a collection of bytes that is stored on secondary storage
devices like disk
Why we need Files in C
@Dr. Arpana Chaturvedi
▰ When a program is terminated, the entire data is lost. Storing in a file will
preserve your data even if the program terminates.
▰ If you have to enter a large number of data, it will take a lot of time to enter
them all.
▰ However, if you have a file containing all the data, you can easily access the
contents of the file using a few commands in C.
▰ You can easily move your data from one computer to another without any
changes.
A file is an external
collection of related data treated as a unit.
Streams in C
@Dr. Arpana Chaturvedi
Data is input to and output from a stream. A stream can be
associated with a physical device, such as a terminal, or with
a file stored in auxiliary memory.
Standard Streams Of Files in C
@Dr. Arpana Chaturvedi
Standard stream names have
already been declared in the stdio.h header
file and cannot be declared again in our program.
There is no need to open and close the standard streams.
It is done automatically by the operating system.
Types of Files in C
@Dr. Arpana Chaturvedi
Types of Files in C
@Dr. Arpana Chaturvedi
There are two kinds of files in a system. They are:
▰ Text files (ASCII)
▰ Binary files
Text Files
▰ Text files contain ASCII codes of digits, alphabetic and symbols.
▰ Text files are the normal .txt files. You can easily create text files using any
simple text editors such as Notepad.
▰ When you open those files, you'll see all the contents within the file as plain
text. You can easily edit or delete the contents.
▰ They take minimum effort to maintain, are easily readable, and provide the
least security and takes bigger storage space.
Types of Files in C
@Dr. Arpana Chaturvedi
Binary Files
▰ Binary file contains collection of bytes (0’s and 1’s). Binary files are compiled
version of text files.
▰ Binary files are mostly the .bin files in your computer.
▰ Instead of storing data in plain text, they store it in the binary form (0's and 1's).
▰ They can hold a higher amount of data, are not readable easily, and provides
better security than text files.
Difference between Types of Files in C
@Dr. Arpana Chaturvedi
Text File Binary File
Bits represent character. Bits represent a custom data.
Less prone to get corrupt as changes reflect as
soon as the file is opened and can easily be
undone.
Can easily get corrupted, even a single bit
change may corrupt the file.
Can store only plain text in a file.
Can store different types of data (image, audio,
text) in a single file.
Widely used file format and can be opened
using any simple text editor.
Developed especially for an application and
may not be understood by other applications.
Mostly .txt and .rtf are used as extensions to
text files.
Can have any application defined extension.
Binary and Text Files in C
Text files store data as a sequence of characters; binary files
store data as they are stored in primary memory.
Categories of Operations of Files in C
@Dr. Arpana Chaturvedi
Categories of Standard Input Output
Functions in C
@Dr. Arpana Chaturvedi
C has eight categories of standard file library functions.
BASIC FILE OPERATIONS IN C
PROGRAMMING
@Dr. Arpana Chaturvedi
Among all 8 Categories, There are 4 main basic operations that can be
performed on any files in C programming language.
▰ Opening/Creating a file
▰ Closing a file
▰ Reading a file
▰ Writing in a file
Opening Files in C
@Dr. Arpana Chaturvedi
Opening/Creating a file:
@Dr. Arpana Chaturvedi
▰ In a C program, we declare a file pointer and use fopen().Opening a file is performed using
the fopen() function defined in the stdio.h header file.
▰ The C library function fopen() is used to open a filename pointed to, by filename using the
given mode to perform operations such as reading, writing etc.
▰ fopen() function creates a new file if the mentioned file name does not exist.
Syntax:
FILE *fopen (const char *filename, const char *mode)
OR
FILE *fp;
fp=fopen (“filename”, ”„mode”);
Where,
fp – file pointer to the data type “FILE”.
filename – the actual file name with full path of the file.
mode – refers to the operation that will be performed on
the file. Example: r, w, a, r+, w+ and a+.
Return Value
This function returns a FILE pointer. Otherwise, NULL is
returned and the global variable errno is set to indicate the
error.
File Open Result in C
@Dr. Arpana Chaturvedi
MODE OF OPERATIONS PERFORMED ON
A FILE IN C LANGUAGE:
@Dr. Arpana Chaturvedi
There are many modes in opening a file. Based on the mode of file, it can be opened
for reading or writing or appending the texts.
▰ r – Opens a file in read mode and sets pointer to the first character in the file. It
returns null if file does not exist.
▰ w – Opens a file in write mode. It returns null if file could not be opened. If file
exists, data are overwritten.
▰ a – Opens a file in append mode. It returns null if file couldn’t be opened.
▰ r+ – Opens a file for read and write mode and sets pointer to the first character in
the file.
▰ w+ – opens a file for read and write mode and sets pointer to the first character
in the file.
▰ a+ – Opens a file for read and write mode and sets pointer to the first character
in the file. But, it can’t modify existing contents.
Different Opening Modes of Files in C
@Dr. Arpana Chaturvedi
Mode Meaning Description
fopen Returns if FILE
Exists Not Exists
r Reading
"r"
Opens a file for reading. The file must exist.
– NULL
w Writing
"w"
Creates an empty file for writing. If a file with
the same name already exists, its content is
erased and the file is considered as a new
empty file.
Over write on Existing Create New File
a Append
"a"
Appends to a file. Writing operations, append
data at the end of the file. The file is created
if it does not exist.
– Create New File
r+ Reading + Writing
"r+"
Opens a file to update both reading and
writing. The file must exist.
New data is written at
the beginning
overwriting existing
data
Create New File
w+ Reading + Writing
"w+"
Creates an empty file for both reading and
writing.
Over write on Existing Create New File
a+ Reading + Appending
"a+"
New data is appended
Create New File
File Opening Modes in C
@Dr. Arpana Chaturvedi
File Opening Modes in C
Example of fopen() in C
@Dr. Arpana Chaturvedi
Output:
@Dr. Arpana Chaturvedi
Closing Files in C
@Dr. Arpana Chaturvedi
Closing a file in C
@Dr. Arpana Chaturvedi
▰ The file (both text and binary) should be closed after reading/writing. Closing a
file is performed using the fclose() function.
Syntax:
▰ int fclose(FILE *fp);
▰ fclose() function closes the file that is being pointed by file pointer fp.
Eg.: In a C program, we close a file as below.
▰ fclose (fp);
Simple Program To Show Use Of Fopen
And Fclose Functions In C
@Dr. Arpana Chaturvedi
Output:
@Dr. Arpana Chaturvedi
Read and Write Operations of Files in C
@Dr. Arpana Chaturvedi
Text and Binary Text File- Read/Write
in C
Formatted input/output, character input/output, and string
input/output functions can be used only with text files.
Block Input and Output in File in C
Unformatted Functions to read from Files in C
@Dr. Arpana Chaturvedi
Unformatted Function to Read From The
File
@Dr. Arpana Chaturvedi
1. fgetc(): It is the simplest function to read a single character from a file.
Syntax:
int fgetc( FILE * fp );
▰ The fgetc() function reads a character from the input file referenced by fp. The
return value is the character read, or in case of any error, it returns EOF.
2. fgets(): The following function allows to read a string from a stream.
Syntax:
char *fgets( char *buf, int n, FILE *fp );
▰ The functions fgets() reads up to n-1 characters from the input stream referenced
by fp. It copies the read string into the buffer buf, appending a null character to
terminate the string.
▰ If this function encounters a newline character 'n' or the end of the file EOF before
they have read the maximum number of characters, then it returns only the
characters read up to that point including the new line character
fgetc()::
@Dr. Arpana Chaturvedi
▰ Description
▰ The C library function int fgetc(FILE *stream) gets the next character (an
unsigned char) from the specified stream and advances the position indicator
for the stream.
▰ Declaration
▰ Following is the declaration for fgetc() function.
▰ int fgetc(FILE *stream)
▰ Parameters
▰ • stream − This is the pointer to a FILE object that identifies the stream on
which the operation is to be performed.
▰ Return Value
▰ This function returns the character read as an unsigned char cast to an int or
EOF on end of file or error.
Output:
@Dr. Arpana Chaturvedi
Unformatted Input Function to Read a File:
in C
@Dr. Arpana Chaturvedi
▰ gets() function
▰ The gets() function enables the user to enter some characters followed by the
enter key.
▰ All the characters entered by the user get stored in a character array.
▰ The null character is added to the array to make it a string.
▰ The gets() allows the user to enter the space-separated strings. It returns the
string entered by the user.
▰ Syntax:
▰ char[] gets(char[]);
Unformatted Input Function to Read a File:
in C
@Dr. Arpana Chaturvedi
1. fgets():
The C library function char *fgets(char *str, int n, FILE *stream) reads a line from the
specified stream and stores it into the string pointed to by str. It stops when either (n-1)
characters are read, the newline character is read, or the end-of-file is reached,
whichever comes first.
Syntax:
▰ char *fgets(char *str, int n, FILE *stream)
Parameters
▰ str − This is the pointer to an array of chars where the string read is stored.
▰ n − This is the maximum number of characters to be read (including the final null-
character). Usually, the length of the array passed as str is used.
▰ stream − This is the pointer to a FILE object that identifies
Return Value
On success, the function returns the same str
parameter. If the End-of-File is encountered
and no characters have been read, the
contents of str remain unchanged and a null
pointer is returned.
If an error occurs, a null pointer is returned.
Example of fgets():
@Dr. Arpana Chaturvedi
Program to find the number of characters,
lines, spaces from a file in C
@Dr. Arpana Chaturvedi
Unformatted Input Function to Read a File:
in C
@Dr. Arpana Chaturvedi
Unformatted Functions to Write in Files in C
@Dr. Arpana Chaturvedi
Unformatted Functions to Write into a file
@Dr. Arpana Chaturvedi
1. fputc(): It is the simplest function to write individual characters to a stream.
Syntax:
▰ int fputc( int c, FILE *fp );
▰ The function fputc() writes the character value of the argument c to the output
stream referenced by fp. It returns the written character written on success
otherwise EOF if there is an error.
2. fputs(): To write a null-terminated string to a stream fputs() function is used.
Syntax:
▰ int fputs( const char *s, FILE *fp );
▰ The function fputs() writes the string s to the output stream referenced by fp.
▰ It returns a non-negative value on success,
Otherwise EOF is returned in case of any error.
C program which copies one file to another.
@Dr. Arpana Chaturvedi
C Program Which Copies One File To
Another.
@Dr. Arpana Chaturvedi
C Program Which Copies One File To
Another.
@Dr. Arpana Chaturvedi
C Program Which Copies One File To
Another.
@Dr. Arpana Chaturvedi
Writing And The Reading From The File
Characters Entered.
@Dr. Arpana Chaturvedi
Writing And The Reading From The File
Characters Entered.
@Dr. Arpana Chaturvedi
Thank You !!
@ Dr.Arpana Chaturvedi

More Related Content

PDF
Introduction to Rstudio
PDF
Arrays 2 Dimensional Unit 2 Part 1.pdf
PPTX
Introduction to Eclipse
PDF
Arrays Fundamentals Unit II
PPT
Standard Library Functions
PDF
Python Data Types
PPTX
Getting Started with R
Introduction to Rstudio
Arrays 2 Dimensional Unit 2 Part 1.pdf
Introduction to Eclipse
Arrays Fundamentals Unit II
Standard Library Functions
Python Data Types
Getting Started with R

What's hot (19)

PPTX
Workshop presentation hands on r programming
PDF
PDF
Lexyacc
PDF
PDF
A short tutorial on r
DOCX
Primitive data types
DOCX
Yacc topic beyond syllabus
PPTX
R Programming Language
PDF
1 R Tutorial Introduction
PPT
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
PPTX
Lexical analysis-using-lex
PPTX
Lex & yacc
PPT
Unit 2 Principles of Programming Languages
PDF
220 runtime environments
PDF
Sparql semantic information retrieval by
PPTX
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 1)
PDF
Python Programming - XII. File Processing
PPTX
R language
Workshop presentation hands on r programming
Lexyacc
A short tutorial on r
Primitive data types
Yacc topic beyond syllabus
R Programming Language
1 R Tutorial Introduction
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
Lexical analysis-using-lex
Lex & yacc
Unit 2 Principles of Programming Languages
220 runtime environments
Sparql semantic information retrieval by
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 1)
Python Programming - XII. File Processing
R language
Ad

Similar to File Handling in C Part I (20)

PPTX
INput output stream in ccP Full Detail.pptx
PDF
FILES IN C
PDF
VIT351 Software Development VI Unit5
PPTX
File handing in C
PPTX
File handling in C hhsjsjshsjjsjsjs.pptx
PPTX
Topic - File operation.pptx
PDF
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
PPTX
Programming C- File Handling , File Operation
PPTX
File Handling in C
PPT
How to do file-handling - in C language
PPTX
PPS-II UNIT-5 PPT.pptx
PPTX
File handling in c Programming - Unit 5.1
PPTX
Concept of file handling in c
PPTX
want to learn files,then just use this ppt to learn
PPTX
UNIT 10. Files and file handling in C
PDF
Unit 5 File handling in C programming.pdf
PDF
Module 5 file cp
PPTX
PPS PPT 2.pptx
PDF
637225560972186380.pdf
PPTX
File handling in C by Faixan
INput output stream in ccP Full Detail.pptx
FILES IN C
VIT351 Software Development VI Unit5
File handing in C
File handling in C hhsjsjshsjjsjsjs.pptx
Topic - File operation.pptx
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
Programming C- File Handling , File Operation
File Handling in C
How to do file-handling - in C language
PPS-II UNIT-5 PPT.pptx
File handling in c Programming - Unit 5.1
Concept of file handling in c
want to learn files,then just use this ppt to learn
UNIT 10. Files and file handling in C
Unit 5 File handling in C programming.pdf
Module 5 file cp
PPS PPT 2.pptx
637225560972186380.pdf
File handling in C by Faixan
Ad

More from Arpana Awasthi (9)

PDF
Unit 5 Part 1 Macros
PDF
Unit 2 Part 1.2 Data Types.pdf
PDF
Introduction To Python.pdf
PDF
Unit 2 Part 1 POLYMORPHISM.pdf
PDF
Unit 2 Part 1 Constructors.pdf
PDF
Eclipse - GUI Palette
PDF
Machine Learning: Need of Machine Learning, Its Challenges and its Applications
PDF
Programming language
PDF
Role of machine learning in detection, prevention and treatment of cancer
Unit 5 Part 1 Macros
Unit 2 Part 1.2 Data Types.pdf
Introduction To Python.pdf
Unit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 Constructors.pdf
Eclipse - GUI Palette
Machine Learning: Need of Machine Learning, Its Challenges and its Applications
Programming language
Role of machine learning in detection, prevention and treatment of cancer

Recently uploaded (20)

PDF
Electronic commerce courselecture one. Pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Encapsulation theory and applications.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Machine Learning_overview_presentation.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Electronic commerce courselecture one. Pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Review of recent advances in non-invasive hemoglobin estimation
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Unlocking AI with Model Context Protocol (MCP)
sap open course for s4hana steps from ECC to s4
Advanced methodologies resolving dimensionality complications for autism neur...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Encapsulation theory and applications.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Network Security Unit 5.pdf for BCA BBA.
Encapsulation_ Review paper, used for researhc scholars
MIND Revenue Release Quarter 2 2025 Press Release
Assigned Numbers - 2025 - Bluetooth® Document
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Machine learning based COVID-19 study performance prediction
Machine Learning_overview_presentation.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf

File Handling in C Part I

  • 1. Jagannath Institute of Management Sciences Vasant Kunj-II, New Delhi - 110070 Subject Name: Programming In C Department of Information Technology Created By: Dr. Arpana Chaturvedi @Dr. Arpana Chaturvedi
  • 2. Subject: Programming In C Topic: Unit III- Part II Concept of Files @Dr. Arpana Chaturvedi
  • 3. Topics to be Covered ▰ Concept of Files ▰ File opening in various modes and closing of a file ▰ Unformatted Input and Output Functions ▰ Formatted Input Output Functions ▰ Reading from a Binary file ▰ Writing onto a Binary file
  • 4. Concept of Files in C @Dr. Arpana Chaturvedi
  • 5. Concept of Files in C @Dr. Arpana Chaturvedi ▰ A file is an external collection of related data treated as a single unit. ▰ The primary purpose of a file is to keep a record of data. ▰ Since the contents of primary memory are lost when the computer is shut down, we need files to store our data in a more permanent form. ▰ Hence we say, File is a collection of bytes that is stored on secondary storage devices like disk
  • 6. Why we need Files in C @Dr. Arpana Chaturvedi ▰ When a program is terminated, the entire data is lost. Storing in a file will preserve your data even if the program terminates. ▰ If you have to enter a large number of data, it will take a lot of time to enter them all. ▰ However, if you have a file containing all the data, you can easily access the contents of the file using a few commands in C. ▰ You can easily move your data from one computer to another without any changes. A file is an external collection of related data treated as a unit.
  • 7. Streams in C @Dr. Arpana Chaturvedi Data is input to and output from a stream. A stream can be associated with a physical device, such as a terminal, or with a file stored in auxiliary memory.
  • 8. Standard Streams Of Files in C @Dr. Arpana Chaturvedi Standard stream names have already been declared in the stdio.h header file and cannot be declared again in our program. There is no need to open and close the standard streams. It is done automatically by the operating system.
  • 9. Types of Files in C @Dr. Arpana Chaturvedi
  • 10. Types of Files in C @Dr. Arpana Chaturvedi There are two kinds of files in a system. They are: ▰ Text files (ASCII) ▰ Binary files Text Files ▰ Text files contain ASCII codes of digits, alphabetic and symbols. ▰ Text files are the normal .txt files. You can easily create text files using any simple text editors such as Notepad. ▰ When you open those files, you'll see all the contents within the file as plain text. You can easily edit or delete the contents. ▰ They take minimum effort to maintain, are easily readable, and provide the least security and takes bigger storage space.
  • 11. Types of Files in C @Dr. Arpana Chaturvedi Binary Files ▰ Binary file contains collection of bytes (0’s and 1’s). Binary files are compiled version of text files. ▰ Binary files are mostly the .bin files in your computer. ▰ Instead of storing data in plain text, they store it in the binary form (0's and 1's). ▰ They can hold a higher amount of data, are not readable easily, and provides better security than text files.
  • 12. Difference between Types of Files in C @Dr. Arpana Chaturvedi Text File Binary File Bits represent character. Bits represent a custom data. Less prone to get corrupt as changes reflect as soon as the file is opened and can easily be undone. Can easily get corrupted, even a single bit change may corrupt the file. Can store only plain text in a file. Can store different types of data (image, audio, text) in a single file. Widely used file format and can be opened using any simple text editor. Developed especially for an application and may not be understood by other applications. Mostly .txt and .rtf are used as extensions to text files. Can have any application defined extension.
  • 13. Binary and Text Files in C Text files store data as a sequence of characters; binary files store data as they are stored in primary memory.
  • 14. Categories of Operations of Files in C @Dr. Arpana Chaturvedi
  • 15. Categories of Standard Input Output Functions in C @Dr. Arpana Chaturvedi C has eight categories of standard file library functions.
  • 16. BASIC FILE OPERATIONS IN C PROGRAMMING @Dr. Arpana Chaturvedi Among all 8 Categories, There are 4 main basic operations that can be performed on any files in C programming language. ▰ Opening/Creating a file ▰ Closing a file ▰ Reading a file ▰ Writing in a file
  • 17. Opening Files in C @Dr. Arpana Chaturvedi
  • 18. Opening/Creating a file: @Dr. Arpana Chaturvedi ▰ In a C program, we declare a file pointer and use fopen().Opening a file is performed using the fopen() function defined in the stdio.h header file. ▰ The C library function fopen() is used to open a filename pointed to, by filename using the given mode to perform operations such as reading, writing etc. ▰ fopen() function creates a new file if the mentioned file name does not exist. Syntax: FILE *fopen (const char *filename, const char *mode) OR FILE *fp; fp=fopen (“filename”, ”„mode”); Where, fp – file pointer to the data type “FILE”. filename – the actual file name with full path of the file. mode – refers to the operation that will be performed on the file. Example: r, w, a, r+, w+ and a+. Return Value This function returns a FILE pointer. Otherwise, NULL is returned and the global variable errno is set to indicate the error.
  • 19. File Open Result in C @Dr. Arpana Chaturvedi
  • 20. MODE OF OPERATIONS PERFORMED ON A FILE IN C LANGUAGE: @Dr. Arpana Chaturvedi There are many modes in opening a file. Based on the mode of file, it can be opened for reading or writing or appending the texts. ▰ r – Opens a file in read mode and sets pointer to the first character in the file. It returns null if file does not exist. ▰ w – Opens a file in write mode. It returns null if file could not be opened. If file exists, data are overwritten. ▰ a – Opens a file in append mode. It returns null if file couldn’t be opened. ▰ r+ – Opens a file for read and write mode and sets pointer to the first character in the file. ▰ w+ – opens a file for read and write mode and sets pointer to the first character in the file. ▰ a+ – Opens a file for read and write mode and sets pointer to the first character in the file. But, it can’t modify existing contents.
  • 21. Different Opening Modes of Files in C @Dr. Arpana Chaturvedi Mode Meaning Description fopen Returns if FILE Exists Not Exists r Reading "r" Opens a file for reading. The file must exist. – NULL w Writing "w" Creates an empty file for writing. If a file with the same name already exists, its content is erased and the file is considered as a new empty file. Over write on Existing Create New File a Append "a" Appends to a file. Writing operations, append data at the end of the file. The file is created if it does not exist. – Create New File r+ Reading + Writing "r+" Opens a file to update both reading and writing. The file must exist. New data is written at the beginning overwriting existing data Create New File w+ Reading + Writing "w+" Creates an empty file for both reading and writing. Over write on Existing Create New File a+ Reading + Appending "a+" New data is appended Create New File
  • 22. File Opening Modes in C @Dr. Arpana Chaturvedi
  • 24. Example of fopen() in C @Dr. Arpana Chaturvedi
  • 26. Closing Files in C @Dr. Arpana Chaturvedi
  • 27. Closing a file in C @Dr. Arpana Chaturvedi ▰ The file (both text and binary) should be closed after reading/writing. Closing a file is performed using the fclose() function. Syntax: ▰ int fclose(FILE *fp); ▰ fclose() function closes the file that is being pointed by file pointer fp. Eg.: In a C program, we close a file as below. ▰ fclose (fp);
  • 28. Simple Program To Show Use Of Fopen And Fclose Functions In C @Dr. Arpana Chaturvedi
  • 30. Read and Write Operations of Files in C @Dr. Arpana Chaturvedi
  • 31. Text and Binary Text File- Read/Write in C Formatted input/output, character input/output, and string input/output functions can be used only with text files.
  • 32. Block Input and Output in File in C
  • 33. Unformatted Functions to read from Files in C @Dr. Arpana Chaturvedi
  • 34. Unformatted Function to Read From The File @Dr. Arpana Chaturvedi 1. fgetc(): It is the simplest function to read a single character from a file. Syntax: int fgetc( FILE * fp ); ▰ The fgetc() function reads a character from the input file referenced by fp. The return value is the character read, or in case of any error, it returns EOF. 2. fgets(): The following function allows to read a string from a stream. Syntax: char *fgets( char *buf, int n, FILE *fp ); ▰ The functions fgets() reads up to n-1 characters from the input stream referenced by fp. It copies the read string into the buffer buf, appending a null character to terminate the string. ▰ If this function encounters a newline character 'n' or the end of the file EOF before they have read the maximum number of characters, then it returns only the characters read up to that point including the new line character
  • 35. fgetc():: @Dr. Arpana Chaturvedi ▰ Description ▰ The C library function int fgetc(FILE *stream) gets the next character (an unsigned char) from the specified stream and advances the position indicator for the stream. ▰ Declaration ▰ Following is the declaration for fgetc() function. ▰ int fgetc(FILE *stream) ▰ Parameters ▰ • stream − This is the pointer to a FILE object that identifies the stream on which the operation is to be performed. ▰ Return Value ▰ This function returns the character read as an unsigned char cast to an int or EOF on end of file or error.
  • 37. Unformatted Input Function to Read a File: in C @Dr. Arpana Chaturvedi ▰ gets() function ▰ The gets() function enables the user to enter some characters followed by the enter key. ▰ All the characters entered by the user get stored in a character array. ▰ The null character is added to the array to make it a string. ▰ The gets() allows the user to enter the space-separated strings. It returns the string entered by the user. ▰ Syntax: ▰ char[] gets(char[]);
  • 38. Unformatted Input Function to Read a File: in C @Dr. Arpana Chaturvedi 1. fgets(): The C library function char *fgets(char *str, int n, FILE *stream) reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first. Syntax: ▰ char *fgets(char *str, int n, FILE *stream) Parameters ▰ str − This is the pointer to an array of chars where the string read is stored. ▰ n − This is the maximum number of characters to be read (including the final null- character). Usually, the length of the array passed as str is used. ▰ stream − This is the pointer to a FILE object that identifies Return Value On success, the function returns the same str parameter. If the End-of-File is encountered and no characters have been read, the contents of str remain unchanged and a null pointer is returned. If an error occurs, a null pointer is returned.
  • 39. Example of fgets(): @Dr. Arpana Chaturvedi
  • 40. Program to find the number of characters, lines, spaces from a file in C @Dr. Arpana Chaturvedi
  • 41. Unformatted Input Function to Read a File: in C @Dr. Arpana Chaturvedi
  • 42. Unformatted Functions to Write in Files in C @Dr. Arpana Chaturvedi
  • 43. Unformatted Functions to Write into a file @Dr. Arpana Chaturvedi 1. fputc(): It is the simplest function to write individual characters to a stream. Syntax: ▰ int fputc( int c, FILE *fp ); ▰ The function fputc() writes the character value of the argument c to the output stream referenced by fp. It returns the written character written on success otherwise EOF if there is an error. 2. fputs(): To write a null-terminated string to a stream fputs() function is used. Syntax: ▰ int fputs( const char *s, FILE *fp ); ▰ The function fputs() writes the string s to the output stream referenced by fp. ▰ It returns a non-negative value on success, Otherwise EOF is returned in case of any error.
  • 44. C program which copies one file to another. @Dr. Arpana Chaturvedi
  • 45. C Program Which Copies One File To Another. @Dr. Arpana Chaturvedi
  • 46. C Program Which Copies One File To Another. @Dr. Arpana Chaturvedi
  • 47. C Program Which Copies One File To Another. @Dr. Arpana Chaturvedi
  • 48. Writing And The Reading From The File Characters Entered. @Dr. Arpana Chaturvedi
  • 49. Writing And The Reading From The File Characters Entered. @Dr. Arpana Chaturvedi
  • 50. Thank You !! @ Dr.Arpana Chaturvedi