File Operation In C Programming

Hello guys, welcome back to my blog. In this article, I will discuss the file operation in C programming, types of file operation performed in C programming, code for file operations, etc.

If you need an article on some other topics then comment us below in the comment section. You can also catch me @ Instagram – Chetan Shidling.

Also, read:

  1. Sorting In Data Structure And Algorithms, Code, Working, Types Of Sorting.
  2. Trees In Data Structure And Algorithm Using C, Code, Explanation, Types.
  3. Queue In Data Structure And Algorithm Using C, Code, Explanation,.

File Operation In C Programming

Before we can write to a file, we need to open it. What this actually indicates is that we need to tell the system that we want to write to a file and what the filename is. We perform this with the “fopen” function you can see in the of the program below. The file pointer, “fp” in our case, points to the file and two arguments are required in the parentheses, the filename first, followed by the file type. The filename is any valid 1616/OS filename and can be displayed in upper or lower case letters, or even mixed if you so want. It is included in double-quotes. For this example, we have chosen the name CHETAN.TXT. This file should not be on your disk at this time. If you have a file with this name, you should modify its name or move it because when we execute this program, its contents will be deleted. If you don’t have a file by this name, that is great because we will build one and put some data into it.

#include<stdio.h>
main( )
{
FILE *fp;
char stuff[25];
int index;
fp = fopen("CHETAN.TXT","w"); /* open for writing */
strcpy(stuff,"This is an example line.");
for (index = 1;index <= 10;index++)
fprintf(fp,"%s Line number %d\n",stuff,index);
fclose(fp); 
/* close the file before ending program */
}

There are tree operations performed in file operation, such as:

  1. READING (“r”)
  2. WRITING (“w”)
  3. APPENDING (“a”)

01. READING (“r”)

The second parameter is the file attribute and can be any of three letters, “r”, “w”, or “a”, and need be lower case. When an “r” is used, the file is opened for reading, a “w” is used to show a file to be utilized for writing, and an “a” means that you want to append additional data to the data already in a present file. Opening a file for reading needs that the file already exists. If it does not exist, the file pointer will be set to NULL and can be examined by the program.

02. WRITING (“w”)

When a file is opened for writing, it will be generated if it does not already exist and if the file already exists then it will delete all previous data that is present.

03. APPENDING (“a”)

When a file is opened for appending, it will be generated if it does not already exist and it will be initially empty. If it does exist, the data input point will be the end of the existing data so that any new data will be appended to any data that already exists in the file.

Closing A File

To close a file, you can use the function “fclose” with the file pointer in the parentheses.

01. C Program to create file and store data in file?

#include <stdio.h>
#include <stdlib.h>

void main()
{
    FILE *fptr;
    char name[20];
    int age, n, i, salary;
   // clrscr();
    // Opening file for writing
    fptr = fopen("C:\\Users\\HP\\Desktop\\ds\\file.txt", "w");
    if(fptr == NULL)
    {
        printf("File does not exists \n");
        return;
    }
    printf("Person Details\n");
    printf("Enter the name\n");
    gets(name);
    fprintf(fptr, "Name = %s\n", name);
    printf("Enter the age\n");
    scanf("%d", &age);
    fprintf(fptr,"Age = %d\n", age);
    printf("Enter the salary \n");
    scanf("%d", & salary);
    fprintf(fptr,"Salary = %d\n", salary);
    printf("\n");
    fclose(fptr);
    getch();
    return 0;
}

Output:

Person Details
Enter the name
Chetan
Enter the age
21
Enter the salary
1002211

If file does not exit then it will automatically create file on name file.txt and then the data such as name, age, and salary will be store in the file, you can try this code.

02. C Program to read data from the file and display?

#include <stdio.h>
#include <stdlib.h>

void main()
{
    FILE *fptr;
    char c[1000];
   // clrscr();
    // Opening file for writing
    fptr = fopen("C:\\Users\\HP\\Desktop\\ds\\file.txt", "r");
    if(fptr == NULL)
    {
        printf("File does not exists \n");
        return;
    }
    printf("Person Details\n");
    fscanf(fptr, "%[^\n]", c);
    printf("Data from the file:\n%s", c);
    fclose(fptr);
    getch();
    return 0;
}

Output:

Person Details
Data from the file:
Name = Chetan

I hope this article may help you all a lot. If you have any doubts related to this article “file operation in c programming”, then comment below in the comment box.

Also, read:

About The Author

Share Now