File operations – Theory and Programs [Learn to code]

What are File operations? Different modes of File operations and programs on File operations.

  • 098. Write a program to read and write a file character by character.
    #include<stdio.h>
    int main()
    {
        FILE *fp;   /* file pointer*/
        char fName[20];
        char ch;
     
        printf("nEnter file name to create :");
        scanf("%s",fName);
     
        fp = fopen(fName,"w");
        if(fp==NULL)
        {
            printf("File not created !!");
            exit(0); 
        }
     
        printf("File created successfully.");
        /*writing into file*/
        printf("nEnter text to write (press < enter > to save & quit):n");
        while( (ch = getchar())!='n')
        {
            putc(ch, fp); /*write character into file*/
        }
     
    /*again open file to read data*/
        fp = fopen(fName,"r");
        if(fp==NULL)
        {
            printf("nCan't open the file!!!");
            exit(0);
        }
     
        printf("nContents of file is :n");
        /*read text until, end of file is not detected*/
        while( (ch=getc(fp))!=EOF )
        {
            printf("%c",ch); /*print character on screen*/
        }
     
        fclose(fp);
        return 0;
    }
  • 099. Write a program to read and write a file line by line.
    #include <stdio.h>
    int main()
    {
        FILE *fp;
        char str[100];
        fp = fopen("C:\myfile.txt", "w");
     
        if (fp == NULL)
        {
           puts("An error occurred while opening the specified file");
        }
     
         printf("Enter your string:");
     
         gets(str);
     
         fputs(str, fp);
     
         fclose(fp);
     
        fp = fopen("C:\myfile.txt", "r");
     
        if (fp == NULL)
        {
           puts("An error occurred while opening the specified file");
        }
     
        while(1)
        {
           if(fgets(str, 10, fp) ==NULL)
                break;
           else
                printf("%s", str);
        }
     
        fclose(fp);
        return 0;
    }
  • 100. Merge two files by store data into third one
    #include<stdio.h>
    #include<stdlib.h>
     
    int main()
    {
       FILE *fp1 = fopen("file1.txt", "r");
     
       FILE *fp2 = fopen("file2.txt", "r");
     
       FILE *fp3 = fopen("file3.txt", "w");
     
       char c;
       if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
       {
             puts("Could not open files");
             exit(0);
       }
     
       while ((c = fgetc(fp1)) != EOF)
          fputc(c, fp3);
     
       while ((c = fgetc(fp2)) != EOF)
          fputc(c, fp3);
     
       fclose(fp1);
       fclose(fp2);
       fclose(fp3);
       return 0;
    }

100. Merge two files by store data into third one

#include<stdio.h>
#include<stdlib.h>
 
int main()
{
   FILE *fp1 = fopen("file1.txt", "r");
 
   FILE *fp2 = fopen("file2.txt", "r");
 
   FILE *fp3 = fopen("file3.txt", "w");
 
   char c;
   if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
   {
         puts("Could not open files");
         exit(0);
   }
 
   while ((c = fgetc(fp1)) != EOF)
      fputc(c, fp3);
 
   while ((c = fgetc(fp2)) != EOF)
      fputc(c, fp3);
 
   fclose(fp1);
   fclose(fp2);
   fclose(fp3);
   return 0;
}

099. Write a program to read and write a file line by line.

#include <stdio.h>
int main()
{
    FILE *fp;
    char str[100];
    fp = fopen("C:\\myfile.txt", "w");
 
    if (fp == NULL)
    {
       puts("An error occurred while opening the specified file");
    }
 
     printf("Enter your string:");
 
     gets(str);
 
     fputs(str, fp);
 
     fclose(fp);
 
    fp = fopen("C:\\myfile.txt", "r");
 
    if (fp == NULL)
    {
       puts("An error occurred while opening the specified file");
    }
 
    while(1)
    {
       if(fgets(str, 10, fp) ==NULL)
            break;
       else
            printf("%s", str);
    }
 
    fclose(fp);
    return 0;
}

098. Write a program to read and write a file character by character.

#include<stdio.h>
int main()
{
    FILE *fp;   /* file pointer*/
    char fName[20];
    char ch;
 
    printf("\nEnter file name to create :");
    scanf("%s",fName);
 
    fp = fopen(fName,"w");
    if(fp==NULL)
    {
        printf("File not created !!");
        exit(0); 
    }
 
    printf("File created successfully.");
    /*writing into file*/
    printf("\nEnter text to write (press < enter > to save & quit):\n");
    while( (ch = getchar())!='\n')
    {
        putc(ch, fp); /*write character into file*/
    }
 
/*again open file to read data*/
    fp = fopen(fName,"r");
    if(fp==NULL)
    {
        printf("\nCan't open the file!!!");
        exit(0);
    }
 
    printf("\nContents of file is :\n");
    /*read text until, end of file is not detected*/
    while( (ch=getc(fp))!=EOF )
    {
        printf("%c",ch); /*print character on screen*/
    }
 
    fclose(fp);
    return 0;
}