Concept of file handling in c

MugdhaSharma11 317 views 27 slides Sep 29, 2021
Slide 1
Slide 1 of 27
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27

About This Presentation

introduction, file, types of files, need for file handling, steps for processing a file, file input/out functions {declaraion of file, opening a file, reading data from a file, writing data in a file, closing the file}, programs #technology #computers


Slide Content

MSc. BIOTECHNOLOGY PRESENTED TO: Ms. HIMANI VERMA PRESENTED BY:       TANYA(2084043)                                          MUGDHA(2084044 ) Concept of file handling

CONTENT- INTRODUCTION WHAT IS A FILE? TYPES OF FILES NEED OF FILE HANDLING STEPS FOR PROCESSING A FILE FILE INPUT/OUTPUT FUNCTIONS DECLARATION OF A FILE OPENING OF A FILE READING DATA FROM A FILE WRITING DATA IN A FILE CLOSING THE FILE PROGRAMS

WHAT IS A FILE? A FILE IS A COLLECTION OF DATA STORED IN ONE UNIT, IDENTIFIED BY A FILENAME. IT CAN BE A DOCUMENT, PICTURE, AUDIO OR VIDEO STREAM, DATA LIBRARY, APPLICATION, OR OTHER COLLECTION OF DATA. FILES CAN BE OPENED, SAVED, DELETED, AND MOVED TO DIFFERENT FOLDERS. THEY CAN ALSO BE TRANSFERRED ACROSS NETWORK CONNECTIONS OR DOWNLOADED FROM THE INTERNET. A FILES TYPE CAN BE DETERMINED BY VIEWING THE FILES ICON OR BY READING THE FILE EXTENSION.

INTRODUCTION FILE HANDLING IS STORING OF DATA IN A FILE USING A PROGRAM. WE CAN EXTRACT/ FETCH DATA FROM A FILE  TO  WORK WITH IT IN THE PROGRAM. FILES ARE USED TO STORE DATA IN A STORAGE DEVICE PERMANENTLY. FILE HANDLING PROVIDES A MECHANISMTO STORE THE OUTPUT OF A PROGRAM IN A FILE AND TO PERFORM VARIOUS OPERATIONS ON IT.

TYPES OF FILES TEXT FILES - IT IS THE DEFAULT MODE OF A FILE. EACH LINE IN A TEXT FILE IS TERMINATED WITH THE SPECIAL CHARACTER KNOWN AS END OF LINE. THE EXTENSION OF TEXT FILE IS .txt. BINARY FILES – IT CONTAINS SAME FORMAT IN WHICH THE INFORMATION IS HELD IN THE MEMORY. NO TRANSLATIONS ARE REQUIRED IN THIS FILE. CSV FILES – THIS IS THE COMMA SEPARATED VALUES FILE, WHICH ALLOWS DATA TO BE SAVED IN TABULAR FORM. THESE ARE USE DWITH SPREADSHEET PROGRAMS SUCH AS MICROSOFT EXCEL. TEXT FILES BINARY FILES CSV FILES

need of file handling A FILE IN ITSELF IS A BUNCH OF BYTES STORED ON SOME STORAGE DEVICE. FILE HELPS US TO STORE DATA PERMANENTLY, WHICH CAN BE RETRIEVED IN FUTURE. C - LANGUAGE PROVIDES A CONCEPT OF FILE THROUGH WHICH DATA CAN BE STORED ON A DISK OR SECONDARY STORAG DEVICE. THE STORED DATA CAN BE RETRIEVED WHENEVER NEEDED. FILE HANDLING IS REQUIREDTO STORE THE OUTPUT OF THE PREOGRAM WHICH CAN BE USED IN THE FUTURE. NO MATTER WHATEVER APPLICATION WE DEVELOP , WE NEED TO STORE THAT DATA SOMEWHERE, AND HERE DATA HANDLING COMES INTO PLAY.

STEPS FOR PROCESSING A FILE DECLARE A FILE POINTER VARIABLE OPEN A FILE USING fopen() function PROCESS THE FILE USING SUITABLE FUNCTION CLOSE THE FILE USING fclose() function

FILE INPUT/OUTPUT FUNCTIONS AVAILABLE IN THE STDIO LIBRARY ARE :

DECLARATION OF A FILE POINTER The type of file that is to be used must be  specified This is accomplished by using a      VARIABLE called FILE POINTER (fp) Pointer variable that points to a structure FILE The members of the FILE structure are used by the program in various file access operations, but programmers do not need to be concerned about them. For each file that is to be OPENED, a pointer type FILE must be declared.                  FILE is a structure  declared in stdio.h

When the function fopen() is called, that function creates an instance of the FILE structure and returns a pointer to that structure . This pointer is used in all subsequent operations on the file. The SYNTAX for declaring file pointers is as follows;    FILE *file_pointer_name,…;         FILE *fp;

OPENING A FILE:    FOR CREATION AND EDIT   Opening a file is performed using the fopen() function defined in the stdio.h header file.  The syntax for opening a file in standard I/O is:  Function fopen takes the name of a file as it's 1st parameter                             And mode as it's 2nd parameter. The string given as first parameter for filename, opens the file named and assigns an identifier to the FILE type pointer fp. This pointer, which contains all the information about the file is subsequently used as a communication link between the system and program. The string given as second parameter for mode, specifies the purpose of opening this file     FILE * fopen (" filename","mode ");

A filename in a C program can also contain path information The path specifies the drive and/or directory or folder name where the file is located. If a filename is specified without a path, it will be assumed that the file is located wherever the operating system currently designates as the default. On PCs, the backslash character ( \ ) is used to separate directory names in a path. It is to be remembered that the backslash character has a special meaning to C with respect to escape sequence when it is in a string. To represent the backslash character itself, one must precede it with another backslash.  Thus, in a C program, the filename would be represented as follows.   “c:\\examdata\\list.txt”; Directory name Drive name File name

Mode can be one of the following; If the purpose is  " reading" and if it exists, then the file is opened with current contents safe otherwise an error occurs. When the mode is " writing", a file with the specialised name is created, if the file does not exist. The contents are deleted, if the file already exists. When the purpose is " appending", the file is opened with the current contents safe. A file with the specified name is created if the file does not exist.

These statements are used to create a text file with the name data.dat under current directory It is opened in "w" mode as data are to be written into the file data.dat FILE *fp; fp = fopen("data.dat","w"); Following is an example where a file pointer “ fp” is declared, the file name, which is declared to contain a maximum of 80 characters, is obtained from the keyboard and then the file is opened in the “write” mode.  char filename[80]; FILE *fp; printf(“Enter the filename to be opened”);  gets(fi lename); fp = fopen(fi lename,“w”);

Writing data In a file C provides four functions that can be used to write text file into the disk. These are; fprintf() fputs() fputc() fwrite() They are just the file versions of printf () puts() putc () write() The only difference is that fprintf () fputs () fputc () fwrite () expects a pointer to the structure FILE.

EXAMPLE; Write to a text file #include < stdio.h >
#include < stdlib.h >
int main()
{
   int num ;
   FILE *fptr
   fptr = fopen ("C:\\ program.txt","w ");

   if( fptr == NULL)
   {
      printf ("Error!");  
      exit(1);            
   }

   printf ("Enter num : ");
   scanf ("%d",& num );

   fprintf ( fptr ,"%d", num );
   fclose ( fptr );

   return 0;
}

Writing to a binary file To write into a binary file, we need to use the fwrite ()  function. The functions take  4 arguments: Address of data to be written in the disk Size of data to be written in the disk Number of such type of data Pointer to the file where you want to write fwrite ( addressData , sizeData , numbersData , pointerToFile );

#include < stdio.h >
struct threeNum {
   int n1, n2, n3;
};
int main()
{
   int n;
   struct threeNum num ;
   FILE * fptr ;

   if (( fptr = fopen ("C:\\program.bin"," wb ")) == NULL){
       printf ("Error! opening file");

       exit(1);
   }

   for(n = 1; n < 5; ++n)
   {
      num.n1 = n;
      num.n2 = 5*n;
      num.n3 = 5*n + 1;
      fwrite (& num , sizeof (struct threeNum ), 1, fptr );
   }
   fclose ( fptr );
 
   return 0;
}

Reading data from a file C provides 4 functions that can be used to read text files from the disk fscanf() fgets() fgetc() fread() These are just the file versions of scanf () gets() getc () and read()   The only difference is that fscanf () fgets () fgetc () and fread () expects a pointer to the structural FILE.

EXAMPLE: Read from a text file      #include < stdio.h > #include < stdlib.h > int main() {    int  num ;    FILE * fptr ;    if (( fptr  =  fopen ("C:\\ program.txt","r ")) == NULL){         printf ("Error! opening file");        exit(1);    }     fscanf ( fptr ,"%d", & num );     printf ("Value of n=%d",  num );     fclose ( fptr );        return 0; }

READ FROM A BINARY FILE Function  fread() takes 4 arguments: Address of data to be written in the disk Size of data to be written in the disk Number of such type of data Pointer to the file where you want to read fread ( addressData , sizeData , numbersData , pointerToFile )

Read from a binary file using fread() #include <stdio.h> #include <stdlib.h> struct threeNum {
   int n1, n2, n3;
};

int main()
{
   int n;
   struct threeNum num;
   FILE *fptr;

   if ((fptr = fopen("C:\\program.bin","rb")) == NULL){
       printf("Error! opening file");
       exit(1);
   }

   for(n = 1; n < 5; ++n)
   {
      fread(&num, sizeof(struct threeNum), 1, fptr);
      printf("n1: %d\tn2: %d\tn3: %d", num.n1, num.n2, num.n3);
   }
   fclose(fptr);
     return 0: }

Closing a file The file (both text and binary) should be closed after reading\writing. Closing a file is performed using the fclose() function. Here, fptr is a file pointer associated with the file to be closed. fclose() returns 0 on success or –1 on error fclose ( fptr )

When a program terminates  (either by reaching the end of main() or by executing the exit() function) All streams are automatically flushed and closed. Actually, in a simple program, it is not necessary to close the file because the system closes all open files before returning to the operating system.

programs To open a file, write in it, and close the file To open a file, read from it, and close the file

REFERENCES https://www.programiz.com/c-programming/c-file-input-output https://www.geeksforgeeks.org/basics-file-handling-c/ BOOKS: Programming in C by Pradip Dey and Manas Ghosh Programming in ANSI by E Balagurusamy Computer Fundamentals by PK sinha and Priti Sinha

This Photo by Unknown author is licensed under CC BY-NC .