File Handling in C
Five Operations
• Creating a new le
• Opening an existing le
• Closing a le
• Reading from and writing information to a le
• Moving data to a speci c location on the le
fi
fi
fi
fi
fi
fi
FILE *fptr;
• When working with les, you need to declare a pointer of type le. This
declaration is needed for communication between the le and the
program
• FILE is a prede ned data type in C, de ned in the stdio.h header le. It
represents a le type
• * indicates that fptr is a pointer
• can be any valid variable name, and it will become the name of the le
pointer.
fi
fi
fi
fi
fi
fi
fi
fi
fptr = fopen(" lename.txt", "r");
• les are opened using the fopen() function. It's one of the fundamental
functions for le handling in the C language
• Syntax: FILE *fopen(const char * lename, const char *mode);
• If the le is successfully opened, the fopen() function returns a pointer to
the le
• If the le cannot be opened (for example, if the le does not exist or
cannot be found), the function returns a NULL pointer
• fclose to close the le
fi
fi
fi
fi
fi
fi
fi
fi
fi
Mode Description
r Opens a le for reading. The le must exist.
w Opens or creates a le for writing. If le exists, its contents are overwritten.
a Opens a le for appending. If le doesn't exist, it's created.
r+ Opens a le for both reading and writing. The le must exist.
w+ Opens a le for both reading and writing. It creates the le if it doesn't exist.
a+ Opens a le for both reading and appending. It creates the le if it doesn't exist.
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
#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;
}
#include <stdio.h>
int main() {
FILE * lePtr;
// Attempt to open the le for writing
lePtr = fopen("example.txt", "w");
// Check if the le was opened successfully
if ( lePtr == NULL) {
printf("Error opening or creating the le!\n");
return 1;
}
printf("File opened or created successfully.\n");
// Don't forget to close the le
fclose( lePtr);
return 0;
}
fi
fi
fi
fi
fi
fi
fi
fi
Reading from File
Function Description Syntax
FILE *fopen(const char * lename, const
fopen Opens a le
char *mode);
fgetc Reads a character from a le int fgetc(FILE *stream);
Reads a line (or speci ed number of
fgets char *fgets(char *str, int n, FILE *stream);
chars) from a le
size_t fread(void *ptr, size_t size, size_t
fread Reads blocks of data from a le
count, FILE *stream);
fclose Closes an open le int fclose(FILE *stream);
fi
fi
fi
fi
fi
fi
fi
#include <stdio.h>
int main() {
FILE * lePtr;
char ch;
// Open the le for reading
lePtr = fopen("example.txt", "r");
// Check if the le exists or was opened successfully
if ( lePtr == NULL) {
printf("Error opening the le!\n");
return 1;
}
// Read and print the contents of the le
while ((ch = fgetc( lePtr)) != EOF) {
putchar(ch);
}
// Close the le after reading
fclose( lePtr);
return 0;
}
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
Writing to File
Function Description Syntax
fopen Opens a le FILE *fopen(const char * lename, const char *mode);
fputc Writes a character to a le int fputc(int char, FILE *stream);
fputs Writes a string to a le int fputs(const char *str, FILE *stream);
Writes blocks of data to a size_t fwrite(const void *ptr, size_t size, size_t count,
fwrite
le FILE *stream);
fclose Closes an open le int fclose(FILE *stream);
fi
fi
fi
fi
fi
fi
#include <stdio.h>
int main() {
FILE * lePtr;
// Open the le for writing
lePtr = fopen("output.txt", "w");
// Check if the le was opened successfully
if ( lePtr == NULL) {
printf("Error opening the le!\n");
return 1;
}
// Write the string to the le
fputs("Hello, World!", lePtr);
// Close the le after writing
fclose( lePtr);
printf("Data written to output.txt successfully.\n");
return 0;
}
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
#include <stdio.h>
int main() {
FILE * lePtr;
// Open the le for writing
lePtr = fopen("example.txt", "w");
// Check if the le was opened successfully
if ( lePtr == NULL) {
printf("Error opening the le!\n");
return 1;
}
// Write some data to the le
fputs("This is a test.", lePtr);
// Close the le
if (fclose( lePtr) == 0) {
printf("File closed successfully.\n");
} else {
printf("Error closing the le!\n");
}
return 0;
}
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
Question
• write a c program to read two emails from two les and then add and
write the output in a new le
fi
fi
// Open the second input file
#include <stdio.h> file2 = fopen("email2.txt", "r");
#include <stdlib.h> if (file2 == NULL) {
perror("Error opening email2.txt");
#define MAX_LINE_LENGTH 1024 fclose(file1); // Close the first file before exitin
return -1;
int main() { }
FILE *file1, *file2, *fileOut;
char line[MAX_LINE_LENGTH]; // Open the output file
fileOut = fopen("combinedEmails.txt", "w");
// Open the first input file if (fileOut == NULL) {
file1 = fopen("email1.txt", "r"); perror("Error creating combinedEmails.txt");
if (file1 == NULL) { fclose(file1);
perror("Error opening email1.txt"); fclose(file2);
return -1; return -1;
} }
// Read from the first file and write to the output file
while (fgets(line, MAX_LINE_LENGTH, file1) != NULL) {
fputs(line, fileOut);
}
// Optionally, write a separator between the emails
fputs("\n-----\n\n", fileOut);
// Read from the second file and write to the output file
while (fgets(line, MAX_LINE_LENGTH, file2) != NULL) {
fputs(line, fileOut);
}
// Close all files
fclose(file1);
fclose(file2);
fclose(fileOut);
printf("Process completed. Emails combined into combinedEmails.txt.\n"
return 0;
}
Write a Program
• write a c program to read data from arrays from two les and then add
and write the output in a new le
fi
fi
#include <stdio.h>
// Open the second input le
#include <stdlib.h>
1 2 le2 = fopen("data2.txt", "r");
if ( le2 == NULL) {
int main() {
perror("Error opening data2.txt");
FILE * le1, * le2, * leOut;
fclose( le1); // Close the rst le befor
int num1, num2, sum;
return -1;
}
// Open the rst input le
le1 = fopen("data1.txt", "r");
// Open the output le
if ( le1 == NULL) {
leOut = fopen("result.txt", "w");
perror("Error opening data1.txt");
if ( leOut == NULL) {
return -1;
perror("Error creating result.txt");
}
fclose( le1);
fclose( le2);
// Read from both les, add numbers, and write to the output le
return -1;
while (fscanf( le1, "%d", &num1) != EOF && fscanf( le2, "%d", &num2) != EOF) {
}
sum = num1 + num2;
fprintf( leOut, "%d\n", sum);
}
// Close all les
fclose( le1); 3
fclose( le2);
fclose( leOut);
printf("Process completed. Output written to result.txt.\n");
return 0;
}
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi