INTRODUCTION TO FILE HANDLING Presentation topic:- Submitted by Guided by Vikash Maurya 👨 👸
CONTENTS 👉 1⃣ What is file? 2⃣ Need of file in C programming. 3⃣ Data organization. 4⃣ File operations. 5⃣ Creating a new file by C. 6⃣ Writing, Reading and Append operation in the file. 7⃣ Closing a file.
WHAT IS FILE? 🤔  A file is a collection of related data that a computer treats as a single unit.  Computer store files to secondary storage so that the contents of files remain intact when a computer shuts down.  When a computer reads a file, it copies the file from the storage device to memory; when it writes to a file, it transfers data from memory to the storage device.  C uses a structure called FILE (defined in stdio.h) to store the attributes of a file.
NEED OF FILE IN C PROGRAMMING 📂 Often it is not enough to just display the data on the screen. Because if the data is large, only few of it can be stored and displayed on the screen. Here memory is volatile so the contents would be lost once the program is terminated. So if we need the same data we need to re-enter it or regenerate the program. And to redo the process is time wasting. And if we would have saved it to a media where the data can be recoverable, it would help us. This medium is usually a ‘file’ on the disk.
DATA ORGANIZATION 💻 Before we start doing input/output let us first find out how data is organized on the disk. Here is the solution- Our program C Library functions OS Disk
FILE OPERATIONS 🔧 There are different operations that can be carried out in a file. These are: (a) Creation of a new file. (b) Opening an existing file. (c) Writing to a file. (d) Reading form a file. (e) Closing a file.
PROGRAM TO CREATE A FILE 📣  /* C program to create a file called emp.rec and store information  * about a person, in terms of his name, age and salary */  #include <stdio.h>  #include <conio.h>  void main()  {  FILE *fptr;  char name[20];  int age;  float salary;  /* open for writing */  fptr = fopen("emp.c", "w");  printf("Enter the name n");  scanf("%s", name);  fprintf(fptr, "Name = %sn", name);  printf("Enter the agen");  scanf("%d", &age);  fprintf(fptr, "Age = %dn", age);  printf("Enter the salaryn");  scanf("%f", &salary);  fprintf(fptr, "Salary = %.2fn", salary);  fclose(fptr);  }
OUTPUT 🔭
FILE
/*FOR READING A SAVED FILE*/  #include <stdio.h>  #include <conio.h>  void main()  {  FILE *fptr;  char a;  fptr=fopen("emp.c","r");  while(1)  {  a=fgetc(fptr);  if(a==EOF)  break;  printf("%c",a);  }  printf("n");  fclose(fptr);  getch();  }
OUTPUT 🔬
/*PROGRAM IN APPEND MODE*/  #include <stdio.h>  #include <conio.h>  void main()  {  FILE *fptr;  char name[20];  int age;  float salary;  fptr = fopen("emp.c", “a");  printf("Enter the name n");  scanf("%s", name);  fprintf(fptr, "Name = %sn", name);  printf("Enter the agen");  scanf("%d", &age);  fprintf(fptr, "Age = %dn", age);  printf("Enter the salaryn");  scanf("%f", &salary);  fprintf(fptr, "Salary = %.2fn", salary);  fclose(fptr);  }
OUTPUT 🎥
FILE
FILE OPEN MODE 📌
MORE ON FILE OPEN MODE 📡
CLOSING A FILE 🔓  When we finish with a mode, we need to close the file before ending the program or beginning another mode with that same file.  To close a file, we use fclose.
Introduction of file handling

Introduction of file handling

  • 1.
    INTRODUCTION TO FILEHANDLING Presentation topic:- Submitted by Guided by Vikash Maurya 👨 👸
  • 2.
    CONTENTS 👉 1⃣ Whatis file? 2⃣ Need of file in C programming. 3⃣ Data organization. 4⃣ File operations. 5⃣ Creating a new file by C. 6⃣ Writing, Reading and Append operation in the file. 7⃣ Closing a file.
  • 3.
    WHAT IS FILE?🤔  A file is a collection of related data that a computer treats as a single unit.  Computer store files to secondary storage so that the contents of files remain intact when a computer shuts down.  When a computer reads a file, it copies the file from the storage device to memory; when it writes to a file, it transfers data from memory to the storage device.  C uses a structure called FILE (defined in stdio.h) to store the attributes of a file.
  • 4.
    NEED OF FILEIN C PROGRAMMING 📂 Often it is not enough to just display the data on the screen. Because if the data is large, only few of it can be stored and displayed on the screen. Here memory is volatile so the contents would be lost once the program is terminated. So if we need the same data we need to re-enter it or regenerate the program. And to redo the process is time wasting. And if we would have saved it to a media where the data can be recoverable, it would help us. This medium is usually a ‘file’ on the disk.
  • 5.
    DATA ORGANIZATION 💻 Beforewe start doing input/output let us first find out how data is organized on the disk. Here is the solution- Our program C Library functions OS Disk
  • 6.
    FILE OPERATIONS 🔧 Thereare different operations that can be carried out in a file. These are: (a) Creation of a new file. (b) Opening an existing file. (c) Writing to a file. (d) Reading form a file. (e) Closing a file.
  • 7.
    PROGRAM TO CREATEA FILE 📣  /* C program to create a file called emp.rec and store information  * about a person, in terms of his name, age and salary */  #include <stdio.h>  #include <conio.h>  void main()  {  FILE *fptr;  char name[20];  int age;  float salary;  /* open for writing */  fptr = fopen("emp.c", "w");  printf("Enter the name n");  scanf("%s", name);  fprintf(fptr, "Name = %sn", name);  printf("Enter the agen");  scanf("%d", &age);  fprintf(fptr, "Age = %dn", age);  printf("Enter the salaryn");  scanf("%f", &salary);  fprintf(fptr, "Salary = %.2fn", salary);  fclose(fptr);  }
  • 8.
  • 9.
  • 10.
    /*FOR READING ASAVED FILE*/  #include <stdio.h>  #include <conio.h>  void main()  {  FILE *fptr;  char a;  fptr=fopen("emp.c","r");  while(1)  {  a=fgetc(fptr);  if(a==EOF)  break;  printf("%c",a);  }  printf("n");  fclose(fptr);  getch();  }
  • 11.
  • 12.
    /*PROGRAM IN APPENDMODE*/  #include <stdio.h>  #include <conio.h>  void main()  {  FILE *fptr;  char name[20];  int age;  float salary;  fptr = fopen("emp.c", “a");  printf("Enter the name n");  scanf("%s", name);  fprintf(fptr, "Name = %sn", name);  printf("Enter the agen");  scanf("%d", &age);  fprintf(fptr, "Age = %dn", age);  printf("Enter the salaryn");  scanf("%f", &salary);  fprintf(fptr, "Salary = %.2fn", salary);  fclose(fptr);  }
  • 13.
  • 14.
  • 15.
  • 16.
    MORE ON FILEOPEN MODE 📡
  • 17.
    CLOSING A FILE🔓  When we finish with a mode, we need to close the file before ending the program or beginning another mode with that same file.  To close a file, we use fclose.