What is Advanced Excel and what are some best practices for designing and cre...
File handling in C hhsjsjshsjjsjsjs.pptx
1. File handling in C
File handling in C stores data of our program in our local store, which can be used
at any time because as the execution of a program completes, our data is lost.
Therefore, we need to save our data in any file form - text or binary files.
We can perform tasks like opening, reading the existing file, writing or adding new
content, creating a new file, closing the file, or even saving the file to a certain
location using C.
2. A few features of using files
•Reusability: Data stored in files can be used
repeatedly.
•Portability: Files enable data transfer between
different systems.
•Efficient: Quick access to stored data.
•Storage Capacity: Large amounts of data can be
stored beyond the constraints of RAM.
3. Types of Files in C
We will be working with two types of files: -
1.Text file
2.Binary file
Let’s understand them in brief -
Text file - The user can create these files easily while handling files in C.
It stores information in the form of ASCII characters internally, and when
the file is opened, the content is readable by humans. It can be created by
any text editor with a .txt or .rtf (rich text)extension. Since text files are
simple, they can be edited by any text editor like Microsoft Word,
Notepad, Apple Text Edit, etc.
Binary file - It stores information in the form of 0’s or 1’s, and it is saved
with the .bin extension, taking less space. Since it is stored in a binary
number system format, it is not readable by humans. Therefore it is more
secure than a text file.
4. C File Operations
There are different kinds of file operations in C.
1.Creating a new file
2.Opening an existing file
3.Writing data to a file
4.Reading data from an existing file.
5.Moving data to a specific location on the file
6.Closing the file
5. function Description Syntax
fopen()
Used to open an existing file or to
create a new file
FILE *fopen(“file_name”,
“mode”);
fprintf() Used to write data in existing file
fprintf(FILE *stream, const char
*format [, argument, ...])
fscanf() Used to read data from the file
fscanf(FILE *stream, const char
*format [, argument, ...])
fputc() Used to write characters in a file fputc(int c, FILE *stream)
fgetc() Used to read characters from a file fgetc(FILE *stream)
fclose() Used to close existing file fclose( FILE *fp )
fseek()
puts the file pointer to the specified
place
fseek(FILE *stream, long int
offset, int whence)
6. File Pointer in C
When you want to perform file operations in C like
reading from or writing to a file, you need a way to
reference that file within your program. This reference is
provided by the file pointer. The file pointer stores the
address of a FILE structure, which contains details about
the file, like its name, its current position, its size, etc.
7. Syntax of File Pointer:
FILE *pointer_name;
FILE is a predefined data type in C, defined in the stdio.h header file. It represents a
file type.
* indicates that pointer_name is a pointer.
pointer_name can be any valid variable name, and it will become the name of the
file pointer.
For example, if you want to declare a file pointer named fptr, you'd write:
FILE *fptr;
8. Later on, when you open a file, you will use functions like fopen() which will return a
file pointer, and you can assign this to your declared file pointer:
fptr = fopen("filename.txt", "r");
This fptr can then be used with other functions to perform various operations on the
file "filename.txt".
9. Mode Description
r
Opens a file for reading. The file must
exist.
w
Opens or creates a file for writing. If file
exists, its contents are overwritten.
a
Opens a file for appending. If file
doesn't exist, it's created.
r+
Opens a file for both reading and
writing. The file must exist.
w+
Opens a file for both reading and
writing. It creates the file if it doesn't
exist.
a+
Opens a file for both reading and
appending. It creates the file if it
doesn't exist.
rb Opens a binary file in reading mode.
wb
Opens or creates a binary file in writing
mode.
ab Opens a binary file in append mode.
rb+
Opens a binary file for both reading and
writing.
wb+
Opens or creates a binary file for both
reading and writing.
ab+
Opens a binary file for both reading and
appending.
10. Example of Opening a File:
#include<stdio.h>
int main() {
FILE *fptr;
fptr = fopen("example.txt", "r"); // Open the file in read
mode
if (fptr == NULL) {
printf("Error in opening the file.n");
return 1; // Exit the program if file can't be opened
}
printf("File opened successfully.n");
fclose(fptr); // It's a good practice to close the file after
operations are done
return 0;
}