File Management Programs


Program to write characters into File.
#include<stdio.h>
#include<conio.h>
void main()

     FILE *fp;
     char ch;
     clrscr(); 
     fp=fopen("Demo.txt","w"); 
     while((ch=getchar())!=EOF)
     { 
                  putc(ch,fp);
     } 
     printf("File Created Succesfully");
     fclose(fp);
     getch();
}
Program to Read characters From File.
#include<stdio.h>
#include<conio.h>
void main()
{
     FILE *fp;
     char ch;
     clrscr();
     fp=fopen("Demo.txt","r");
     while((ch=getc(fp))!=EOF)
     {
           putchar(ch); 
     } 
     printf("File Read Succesfully");
     fclose(fp);
     getch();
}
     
Program to write characters into File , Read Characters from File and Display on Screen.
#include<stdio.h>
#include<conio.h>
void main()
{
     FILE *fp;
     char ch;
     clrscr();
     fp=fopen("Demo.txt","w"); 
     while((ch=getchar())!=EOF)
     {
           putc(ch,fp);
     }
     printf("File Created Succesfully\n");
     fclose(fp);
     fp=fopen("Demo.txt","r");
     while((ch=getc(fp))!=EOF)
     {
           putchar(ch);
     }
     printf("File Read Succesfully\n");
     fclose(fp);
     getch();
}
Program to write Integer Number into File, Read Integer numbers from FILE and Display on Screen.
#include<stdio.h>
#include<conio.h>
void main()
{
     FILE *fp;
     int n,i;
     clrscr();
     fp=fopen("Demo.txt","w");
     for(i=1;i<=10;i++)
     {
          printf("Enter Number:");
          scanf("%d",&n);
          putw(n,fp);
     }
     printf("File Created Succesfully\n");
     fclose(fp);
     fp=fopen("Demo.txt","r");
     while((n=getw(fp))!=EOF)
     {
           printf("%d\n",n);
     } 
     printf("File Read Succesfully\n");
     fclose(fp);
     getch();
}
Program to write 10 Integer Numbers into File and then read numebrs from FILE. If number is even then write them to even.txt file. If number is odd then write them to odd.txt File.
#include<stdio.h>
#include<conio.h>
void main()
{
     FILE *fp,*fp1,*fp2;
     int n,i;
     clrscr();
     fp=fopen("Demo.txt","w");
     for(i=1;i<=10;i++)
     {
           printf("Enter Number:");
           scanf("%d",&n);
           putw(n,fp);
     }
     printf("File Created Succesfully\n");
     fclose(fp);
     fp=fopen("Demo.txt","r");
     fp1=fopen("even.txt","w");
     fp2=fopen("odd.txt","w");
     while((n=getw(fp))!=EOF)
     {
          if(n%2==0)
          putc(n,fp1);
          else
          putc(n,fp2);
     }
     fclose(fp);
     fclose(fp1);
     fclose(fp2);
     getch();
}
Program to Copy one file into another file using Command Line Argument.
#include <stdio.h>
#include <conio.h>
void main(int argc,char *argv[])
{
     FILE *fp1,*fp2;
     char ch;
     clrscr();
     if(argc!=3)
     printf("Wrong Argument Passed\n");
     else
    {
          fp1=fopen(argv[1],"w");
          while((ch=getchar())!=EOF)
          {
                 putc(ch,fp1);
          }
          fclose(fp1);
          fp1=fopen(argv[1],"r");
          fp2=fopen(argv[2],"w");
          while((ch=getc(fp1))!=EOF)
          {
                 putc(ch,fp2);
          }
          printf("File Copied Succesfully\n");
     }
     getch();
}
Program to show use of Random Access File Function.
#include<stdio.h>
#include<conio.h>
void main()
{
     FILE *fp;
     char ch;
     int n;
     clrscr();
     fp=fopen("Demo.txt","r");
     n=ftell(fp);
     ch=getc(fp); 
     printf("File Pointer is at %d Position\n",n);
     printf("character at %d position is %c\n",n,ch);
     fseek(fp,2,0);
     n=ftell(fp);
     ch=getc(fp);
     printf("File Pointer is at %d Position\n",n);
     printf("character at %d position is %c\n",n,ch);
     rewind(fp);
     n=ftell(fp);
     ch=getc(fp);
     printf("File Pointer is at %d Position\n",n);
     printf("character at %d position is %c\n",n,ch);
     getch();
}

Download Projects


Download Programs