JIMS Vasant Kunj-II is one of the best BCA colleges in Delhi NCR. The Course content provided to BCA students are well updated and as per the Demand of the IT Industry. It helps to get Placements in Top IT Companies.
This Pdf includes the Details of File Handling in C. This comes in Unit IV.
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
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
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
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.
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.
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
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.
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
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.
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.
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