Unit 2 - Part2
Unit 2 - Part2
Unit 2 - Part2
File Handling
A file refers to a source in which a program stores the information/data in the form of bytes of
sequence on a disk (permanently). The content available on a file isn’t volatile like the compiler
memory in C. But the program can perform various operations, such as creating, opening,
reading a file, or even manipulating the data present inside the file.
The following operations can be performed on a file.
1. Creation of the new file
2. Opening an existing file
3. Reading from the file
4. Writing to the file
5. Deleting the file
Functions for file handling
There are many functions in the C library to open, read, write, search and close the file. A list of
file functions are given below:
Function Description
fopen() opens new or existing file
fprintf() write data into the file
fscanf() reads data from the file
fputc() writes a character into the file
fgetc() reads a character from file
fclose() closes the file
1. Creation of a New File
To create a new file or open an existing file (and truncate it if it already exists), you use the
fopen() function with the "w" mode. This mode opens the file for writing and creates the file if it
does not exist.
Example:
#include <stdio.h>
int main() {
FILE *file = fopen("newfile.txt", "w"); // Open file for writing (create if not exists)
if (file == NULL) {
perror("Error creating file");
return 1;
1
}
// File is successfully created and opened
fprintf(file, "This is a new file.\n"); // Write to the file
fclose(file); // Close the file
return 0;
}
Explanation:
fopen("newfile.txt", "w") creates newfile.txt if it doesn't exist or truncates it if it does.
fprintf(file, "This is a new file.\n"); writes a line of text to the file.
fclose(file); closes the file.
2
Example:
#include <stdio.h>
int main() {
FILE *file = fopen("newfile.txt", "r"); // Open file for reading
if (file == NULL) {
perror("Error opening file for reading");
return 1;
}
charch;
while ((ch = fgetc(file)) != EOF) {
putchar(ch); // Print each character read from the file
}
fclose(file); // Close the file
return 0; }
Explanation:
fgetc(file) reads a single character from the file.
putchar(ch) prints the character to the standard output.
EOF signifies the end of the file.
4. Writing to the File
To write data to a file, use fprintf(), fputs(), or fputc().
Example:
#include <stdio.h>
int main() {
FILE *file = fopen("newfile.txt", "a"); // Open file for appending
if (file == NULL) {
perror("Error opening file for appending");
return 1;
}
3
fopen("newfile.txt", "a") opens newfile.txt for appending. If the file does not exist, it will
be created.
fprintf(file, "Appending more text.\n"); writes formatted text to the file.
fputs("Another line appended.\n", file); writes a string to the file.
fputc('X', file); writes a single character to the file.
fclose(file); closes the file.
5. Deleting the File
To delete a file, use the remove() function. This function removes the file specified by its
name.
Example:
#include <stdio.h>
int main() {
// Delete a file
if (remove("newfile.txt") == 0) {
printf("File successfully deleted.\n");
} else {
perror("Error deleting file");
}
return 0;
}
Explanation:
remove("newfile.txt") deletes the file newfile.txt.
If successful, it prints a success message. Otherwise, it prints an error message.
Example for File Handling
#include <stdio.h>
int main() {
FILE *file;
const char *filename = "example.txt";
// Read data from the file again to see the appended content
file = fopen(filename, "r"); // Open file in read mode
if (file == NULL) {
printf("Error opening file for reading.\n");
return 1;
}
printf("File Content After Appending:\n");
while (fgets(buffer, sizeof(buffer), file) != NULL) {
printf("%s", buffer);
}
fclose(file); // Close the file
return 0;
5
}
2. Preprocessor Directives
Preprocessor programs provide preprocessor directives that tell the compiler to preprocess
the source code before compiling. All of these preprocessor directives begin with a ‘#’ (hash)
symbol. The ‘#’ symbol indicates that whatever statement starts with a ‘#’ will go to the
preprocessor program to get executed. We can place these preprocessor directives anywhere
in our program.
Examples of some preprocessor directives are: #include, #define, #ifndef, etc.
Preprocessor
Directives Description
2. File Inclusion
7
File inclusion directives are used to include the contents of one file within another file. This is
commonly used to include header files.
Standard Library Headers: Includes system or standard library headers.
#include <stdio.h>
#include <stdlib.h>
3. Conditional Compilation
Conditional Compilation in C directives is a type of directive that helps to compile a specific
portion of the program or to skip the compilation of some specific part of the program based on
some conditions.There are the following preprocessor directives that are used to insert
conditional code:
#if Directive
#ifdef Directive
#ifndef Directive
#else Directive
#elif Directive
#endif Directive
******