FILE handling
Are you a beginner in C programming language and want to learn how to read and write data into a file? Well, FILE handling is the way to go. In this article, we will discuss how to perform Input/Output operations in FILE Handling using C programming language. Let's start with writing content into a file. Firstly, you need to create a FILE pointer that stores the file's address. Then open the file in write mode using the fopen function. If for any reason, the file is not opened, an error message will be displayed. After opening the file, you can write to the file using the fprintf function. Finally, close the file using fclose function. Here's an example:
copy
#include<stdio.h>
#include<stdlib.h>
int main(){
FILE *ptr; // this is file pointer which store file address
ptr = fopen("text.txt","w"); // open file text.txt in write mode
// if any reason not open your file display this message
if (ptr == NULL)
{
printf("Your file not opened\n");
exit(1);
}
// fprintf function is write string in text.txt file
fprintf(ptr,"Hi I am writing %s","file handling\n");
fprintf(ptr,"And you are see this message\n");
fclose(ptr); // your file will be closed
return 0;
}
Next, let's move on to reading content from a file. To read content from a file, you need to create a FILE pointer that stores the file's address. Then open the file in read mode using the fopen function. If for any reason, the file is not opened, an error message will be displayed. After opening the file, you can read the file's content using the fscanf function. Finally, close the file using fclose function. Here's an example:
copy
#include <stdio.h>
#include <stdlib.h>
int main()
{
char string[100];
FILE *ptr; // this is file pointer which store file address
ptr = fopen("text.txt", "r"); // open file text.txt in read mode
// if any reason not open your file display this message
if (ptr == NULL)
{
printf("Your file not opened\n");
exit(1);
}
// read one by one string and print
while (1)
{
fscanf(ptr, "%s", string); // read content in file
// you can also use fgets reading one line
// fgets(string, sizeof string, ptr);
if (feof(ptr) != 0)
{
break;
}
printf("%s\n", string); // display content
}
fclose(ptr); // your file will be closed
return 0;
}
Modes of File Handling in C
There are several modes of file handling in C language. Here's a brief overview of each mode
1. w or wt - This mode opens a file in write text mode. This means that you can write text to the file. 2. r or rt - This mode opens a file in read text mode. This means that you can read text from the file. 3. w+ - This mode opens a file in write text mode, but you can also read from the file. 4. r+ - This mode opens a file in read text mode, but you can also write to the file. 5. rb - This mode opens a file in read binary mode. This means that you can read binary data from the file. 6. wb - This mode opens a file in write binary mode. This means that you can write binary data to the file. 7. rb+ - This mode opens a file in read binary mode, but you can also write to the file. 8. wb+ - This mode opens a file in write binary mode, but you can also read from the file. 9. a - This mode opens a file in append mode. This means that you can add data to the end of the file.
Examples of Reading and Writing Files in C
Let's take a look at some examples of reading and writing files in C using different modes.
Example 1: Writing Text to a File
In this example, we'll open a file in write text mode and write some text to the file.
copy
#include<stdio.h>
#include<stdlib.h>
int main(){
FILE *ptr;
ptr = fopen("text.txt","w");
if (ptr == NULL)
{
printf("Your file could not be opened.\n");
exit(1);
}
fprintf(ptr,"Hi, I am writing %s","file handling\n");
fprintf(ptr,"And you are seeing this message\n");
fclose(ptr);
return 0;
}
Output:
You can see that the file has been created in your current folder and your content will be written in the file. Open text.txt file and see now Hi, I am writing file handling And you are seeing this message
Example 2: Reading Text from a File In this example, we'll open a file in read text mode and read the text from the file.
copy
#include <stdio.h>
#include <stdlib.h>
int main()
{
char string[100];
FILE *ptr;
ptr = fopen("text.txt", "r");
if (ptr == NULL)
{
printf("Your file could not be opened.\n");
exit(1);
}
while (1)
{
fscanf(ptr, "%s", string);
if (feof(ptr) != 0)
{
break;
}
printf("%s\n", string);
}
fclose(ptr);
return 0;
}
Output:
Hi, I am writing file handling And you are seeing this message
Example 3: Writing to a file using "w+" mode:
copy
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
char text[50] = "Hello, World!";
fp = fopen("example.txt", "w+"); // open file in write mode
if(fp == NULL) {
printf("Error opening file.");
exit(1);
}
fprintf(fp, "%s", text); // write text to file
printf("Text written to file successfully!");
fclose(fp); // close file
return 0;
}
In this code, we create a file pointer `fp` and use the `fopen()` function to open a file named "example.txt" in "w+" mode. This mode allows us to write to the file and also read from it.
We then check if the file pointer is null to ensure that the file was opened successfully. If it is null, we print an error message and exit the program.
We then use the `fprintf()` function to write the text "Hello, World!" to the file. Finally, we close the file using the `fclose()` function.
Example 4: Reading from a file using "r+" mode:
copy
#include
#include <stdlib.h>
int main() {
FILE *fp;
char text[50];
fp = fopen("example.txt", "r+"); // open file in read mode
if(fp == NULL) {
printf("Error opening file.");
exit(1);
}
fscanf(fp, "%s", text); // read text from file
printf("Text read from file: %s", text);
fclose(fp); // close file
return 0;
}
In this code, we use the same file pointer `fp` and open the file "example.txt" in "r+" mode. This mode allows us to read from the file and also write to it.
We again check if the file pointer is null to ensure that the file was opened successfully. If it is null, we print an error message and exit the program.
We then use the `fscanf()` function to read the text from the file into the character array `text`. Finally, we print the text using `printf()` and close the file using `fclose()`.
Example 5: Writing to a binary file using "wb" mode:
copy
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
int data[5] = {1, 2, 3, 4, 5};
fp = fopen("example.bin", "wb"); // open file in write mode
if(fp == NULL) {
printf("Error opening file.");
exit(1);
}
fwrite(data, sizeof(int), 5, fp); // write data to file
printf("Data written to file successfully!");
fclose(fp); // close file
return 0;
}
In this code, we create a file pointer `fp` and use the `fopen()` function to open a file named "example.bin" in "wb" mode. This mode allows us to write to the file and also read from it.
We then check if the file pointer is null to ensure that the file was opened successfully. If it is null, we print an error message and exit the program.
We then use the `fwrite()` function to write the integer array `data` to the file. We specify the size of each element (in this case, an integer) and the number of elements to write. Finally, we close the file using the `fclose()` function.
Example 6: Reading from a binary file using "rb" mode:
copy
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
int data[5];
fp = fopen("example.bin", "rb"); // open file in read mode
if(fp == NULL) {
printf("Error opening file.");
exit(1);
}
fread(data, sizeof(int), 5, fp); // read data from file
printf("Data read from file: %d %d %d %d %d", data[0], data[1], data[2], data[3], data[4]);
fclose(fp); // close file
return 0;
}
In this code, we use the same file pointer fp and open the file "example.bin" in "rb" mode. This mode allows us to read from the file and also write to it.
We again check if the file pointer is null to ensure that the file was opened successfully. If it is null, we print an error message and exit the program.
We then use the `fread()` function to read the data from the file into the integer array `data`. We specify the size of each element (in this case, an integer) and the number of elements to read. Finally, we print the data using `printf()` and close the file using `fclose()`.