File Handling In C

Total Post:70

Points:350

 802  View(s)
Ratings:
Rate this:
  1. File Handling In C

    Before starting with the programming part one should know the difference between the terms like file, append, read and write.

    Read : Means Reading the file.
    Write : Means writing the file.
    Apend : Add (something) to the end of a written document.


    For file handling in ‘C’, below are some keywords which we can use in our coding.

    r: If we use ‘r’ , it means file is in read mode only.

    w; If we use ‘w’ , it means file is in write mode only. If file exists then it will clear the data and write data into file. If file does not exist then it will create a new file.

    a: It I called the append mode. If we use ‘w’ , it means file is in write mode only. If file exists then it will not clear the data and write data file after the last text was written. If file does not exist then it will create a new file.

    r+: if file exist, then open a file both for reading and writing. If file does not exist then it will not create new one.

    w+: if file exist, then open a file both for reading and writing. If file does exist then it will not create new one. It will truncate the file to zero length.

    a+: it is called append plus mode.  if file exist, then open a file both for reading and writing. If file does exist then it will not create new one. It will not truncate the file to zero length. Write data file after the last text was written.

    If you want to use binary data then we can use "rb", "wb", "ab", "rb+", "r+b", "wb+", "w+b", "ab+", "a+b".

    File is the keyword which is used to declare the file it is similar that we declare for variable like “ int a”.

    Syntax : File *fp;

    We need to provide the path of the file.

    Fp= fopen(“D:\ WriteLines.txt”,”w+”);

    How to write the text in a file?

    We can use fprintf, fputs, fputc to write the text in a file.

    Here is the small program which is helpful to under the basic of C.

      #include <stdio.h>
     
       main() {
       FILE *fp;
       char ch;
       char str1[30], str2[30]; printf("******Enter the string 1"); scanf("%s",str1);
       printf("******Enter the string 2"); scanf("%s",str2); fp = fopen("D:\WriteLines.txt", "w+");
       fprintf(fp, "&s",str1);
       fputs(str2,,fp);
       for (i=0;i<30;i++)
       {
           Ch =     getc(stdin);
           fputc(ch,fp)
       }
       fclose(fp);
       }

     

    We can use fscanf, fgets, fgetc to Read the text in a file. 

    #include <stdio.h>
    int main()
    {
       FILE *fp;
       char str[60];
       fp = fopen("D:\WriteLines.txt " , "r");
       if(fp == NULL)    {  printf("Error while opening the file");
          return(-1);
       }
       if( fgets(str, 60, fp)!=NULL )
       {    
          puts(str);
       }
     
    FILE *fp;
       int c;
       int n = 0;
     
       fp =fopen("D:\WriteLines.txt ","r");
       if(fp == NULL)    {
          printf("Error in opening file");
        return(-1);
       }
       do
       {
        c = fgetc(fp);
        if(feof(fp) )
        {
        break ;
        }
          printf("%c", c);
       }while(1);
       fp = fopen("D:\WriteLines.txt ","r");
       if(fp == NULL)    {
          printf("Error in opening file");
        return(-1);
       }
       do
       {
     
    fscanf(fp, "%s", str1);
    printf(“%s”,str1)
        if(feof(fp) )
        {
        break ;
        }
      fclose(fp);  
       return(0);
    }
    }while(1);
     
       fclose(fp);  
    return(0);
    }

      Modified On Mar-29-2018 12:13:12 AM

Answer