Loading

Archive for 18 Jul 2012

C Program for Screen/Text Editor

C - Program to Implement Screen/Text Editor


Output Screen - 

C Program for Screen Editor

Code -

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>

int clm=1,row=1,in[21],ci=0;
FILE * fp;
char fname[50],buf[21][78],cbuf[80];

void redisp()
{
gotoxy(60,1);
printf("        ");
gotoxy(60,1);
textcolor(4);
textbackground(2);
cprintf("Alt+f:File %d:%d",clm-2,row-1);
if(row/10==0)
cprintf("");
gotoxy(row,clm);
}

void chrc()
{
if(row<=78)
row++;
else
 {
  row=2;
   if(clm<23)
    clm++;
    if(clm==23)
     row=78;
  }
}

void dispmenu()
{
int i,val,val1;
window(1,1,80,49);
textcolor(3);
textbackground(0);
clrscr();
 gotoxy(1,2);
 printf("%c",201);
 for(i=0;i<78;i++)
  printf("%c",205);
 printf("%c",187);
 for(i=0;i<46;i++)
  printf("%c\n",186);
 printf("%c",200);
 for(i=0;i<78;i++)
   printf("%c",205);
 gotoxy(80,2);
 for(i=0;i<46;i++)
 {
  val=wherey();
  if(i==0)
   gotoxy(80,val+1);
   else
   gotoxy(80,val);
  printf("%c",186);
  }
  val=wherey();
  gotoxy(80,val);
 printf("%c",188);
 gotoxy(2,2);
 gotoxy(1,1);
 textcolor(4);
 textbackground(7);
 cprintf("  F");
 textcolor(0);
  cprintf("ile  ");
  textcolor(4);
 }
 void disp_file()
 {
  int ch=33,c=1,k=3,j=1,i,fle=0,t=0;
  char s,fnm[50],nm;

  while(ch!=27)
   {
    gotoxy(6,1);
    textcolor(4);
    textbackground(2);

    cprintf(" F");
    textcolor(0);
    cprintf("ile ");
    window(6,2,15,6);
    textcolor(4);
   if(c==1)
    textbackground(2);
    else
    textbackground(15);
   cprintf(" N");
   textcolor(0);
  cprintf("ew  ");
  textcolor(4);

      if(c==2)
    textbackground(2);
    else
    textbackground(15);
  cprintf("0");
  textcolor(0);
  cprintf("pen  ");
  textcolor(4);

    if(c==3)
    textbackground(2);
    else
    textbackground(15);
   cprintf(" S");
  textcolor(0);
  cprintf("ave  ");
  textcolor(4);

   if(c==4)
    textbackground(2);
    else
    textbackground(15);
    cprintf(" E");
    textcolor(0);
    cprintf("xit   ");
    ch=getch();
   if(ch==0)
    {
     ch=getch();
      if(ch==72)
       {
    c=c - 1;
     if(c==0)
      c=4;
    }
    if(ch==80)
    c=c+1;
    if(c==5)
    c=1;
    if(ch==27)
    {
    clrscr();
    dispmenu();
    }
    if(ch==13)
    {
     switch(c)
     {
      case 1:clrscr();
         dispmenu();
         for(i=0;i<21;i++)
          {
           for(j=0;j<79;j++)
           buf[i][j]='\0';
          }
          for(i=0;i<21;i++)
          in[i]=0;
       gotoxy(30,2);
     textcolor(15);
       printf("NONAME.txt");
      gotoxy(3,2);
      ch=27;
      break;
  case 2: clrscr();
      dispmenu();
      window(25,3,50,5);
      textcolor(0);
     textbackground(15);
     clrscr();
     gotoxy(30,35);
       printf("Enter file name:");
       textcolor(0);
       scanf("%s",fname);
     fp=fopen(fname,"r");
     for(i=0;i<21;i++)
     {
      for(j=0;j<79;j++)
    buf[i][j]='\0';
     }

    for(i=0;i<21;i++)
    in[i]=0;
   k=3;
   j=1;
     if(fp==NULL)
     {
      window(25,7,45,7);
      textcolor(4);
      textbackground(2);
      clrscr();
     printf(" File not found\n");
    window(20,9,50,9);
   textcolor(4);
   textbackground(2);
  clrscr();
  cprintf("Press any key to continue");
  getch();
  clrscr();
 dispmenu();
 }
 else
  {
   clrscr();
    dispmenu();
     while(1)
      {
       fread(&s,sizeof(s),1,fp);
     if(!feof(fp))
      {
       if(s=='\n')
        t++;
        if(s!='\n')
        {
          buf[t][in[t]]=s;
          in[t]++;
        }
       }
     if(feof(fp))
     break;
    else
     {
      if(s!='\n')
       j++;
       else
    k=k+1;
    j=1;
       gotoxy(j,k);
     printf("%c",s);
     }
    }
    fclose(fp);
     gotoxy(j+1,k);
    row=j+1;
   clm=k;
  }
  ch=27;
  break;

case 3: window(20,9,60,9);
    textcolor(4);
    textbackground(2);
    clrscr();
    fclose(fp);
    cprintf("Enter file name:");
    scanf("%s",fnm);
    fp=fopen(fnm,"w");
    for(i=0;i<21;i++)
     {
      for(j=0;j<in[i];j++)
        fwrite(&buf[i][j],sizeof(buf[i][j]),1,fp);
      if(i!=20)
      {
       s='\n';
       fwrite(&s,sizeof(s),1,fp);
      }
     }
       fclose(fp);
     for(i=0;i<21;i++)
      {
       for(j=0;j<79;j++)
    buf[i][j]='\0';
      }
      for(i=0;i<21;i++)
      in[i]=0;
     clrscr();
     dispmenu();
     if(fle==1)
      exit(0);
      else
      ch=27;
     break;
case 4: window(10,9,55,9);
    textcolor(4);
    textbackground(2);
    clrscr();
       cprintf("Do you want to save file?(y/n):");
       scanf("%c",nm);
      if(nm=='y'||nm=='Y')
    ch=3;
    fle=1;
    exit(0);
    break;
    }
       }
     }
    }
 }
 void main()
 {
  int fl=0,f=0,cp=0,cx=0;
  int c,tx,ty,i,j,sx,sy,px,py;
  int button,x,y;
  char ch;
   textbackground(0);
   clrscr();
  dispmenu();
  row=2;
  clm=3;
  while(1)
   {
    fl=0;
    gotoxy(row,clm);
    row=wherex();
    clm=wherey();
    redisp();
    ch=getch();
     c=ch;
    if(c==0||c==3||c==22||c==24)
    {
     if(c==0)
      c=getch();
    }
    if(ch==27||ch==45)
    break;
    else
    {
     if(ch==8)// if entered char is backspace
      {
      tx=wherex();
      ty=wherey();
      if(ty>=3)
      {
       if(tx>=3)
    {
     if(in[ty-3]!=0)
     {
      buf[ty-3][tx-3]='\0';
     for(i=tx-3;i<in[ty-3]-1;i++)
     buf[ty-3][i]=buf[ty-3][i+1];
       if(tx-3<in[ty-3])
       in[ty-3]--;
      buf[ty-3][in[ty-3]]='\0';
      }
     }
    else
    buf[ty-4][77]='\0';
  }
  if(row!=1||row!=80)
  {
   if((row-1)!=1)
    {
     dispmenu();
     for(j=0;j<21;j++)
      {
       for(i=0;i<in[j];i++)
    {
    gotoxy(i+2,j+3);
    printf("%c",buf[j][i]);
    }
       }
      }
      row=row - 2;
   }
  if(row==0||clm==3)
   row++;
   fl=1;
   if(row<=0 && clm>3)
   {
    row=78;
    clm--;
    gotoxy(row,clm);
   }
  }
  else if(ch==13) //if entered char is enter
  {
   if(clm!=23) { }
   if(clm==23)
    row=78;
    else
    {
     row=1;
     clm++;
    }
    fl=1;
  }
else if(ch==0||ch==3||ch==22||ch==24)
{
 if(c==72 && clm!=3) //if entered char is upward arrow
  clm=clm -1;
  if(c==80 && clm!=23) //if entered char is downward arrow
  clm=clm +1;
  if(c==75 && row!=2) //if entered char is left arrow
  row=row -1;
  if(c==77 && row!=79) //if entered char is right arrow
  row=row +1;
  if(c==33) //if entered char is Alt+f
  disp_file();
  if(c==32)
   printf("%d",c);
   fl=1;
   if(c==24)
   {
    sx=sx -2;
    sy=sy -3;
    in[sy]=0;
    for(i=0,cx=0;i<=sx-ci && cx!=1;i++)
    {
     if(buf[sy][i]!='\0')
      cx=1;
    }
    if(cx==0)
    {
     buf[sy][0]='\0';
     in[sy]=0;
    }
    else
     in[sy]=sx-ci+1;
    cp=1;
    sx=wherex();
    sy=wherey();
    clrscr();
    dispmenu();
   for(j=0;j<21;j++)
    {
     for(i=0;i<in[j];i++)
      {
       gotoxy(i+2,j+3);
    printf("%c",buf[j][i]);
      }
    }
    gotoxy(sx,sy);
 }
 if(c==22) //if entered char is Ctrl+v
 {
  if(cp==1)
   {
    px=wherex();
    py=wherey();
    clrscr();
    dispmenu();
     for(j=0;j<21;j++)
     {
      for(i=0;i<in[j];i++)
       {
    gotoxy(i+2,j+3);
    printf("%c",buf[j][i]);
       }
     }
     gotoxy(px,py);
     if(buf[py-3][px-3]=='\0')
     {
      for(i=0;i<px-2;i++)
       {
    if(buf[py-3][i]=='\0')
    {
     buf[py-3][i]=' ';
     in[py-3]++;
    }
       }
      }
   for(i=0,j=px-2;i<ci;i++,j--)
    {
     buf[py-3][j]=cbuf[i];
     in[py-3]++;
    }
   }
  }

if(c==157) //if entered char is Alt+->
{
 if(f==0)
  {
   sx=wherex();
   sy=wherey();
   f=1;
   }
   else
   {
    if(sx+1!=wherex()||sy!=wherey())
    {
     f=0;
     ci=0;
     cp=0;
     cbuf[ci]='\0';
     sx=wherex();
     sy=wherey();
     clrscr();
     dispmenu();
    for(j=0;j<21;j++)
    {
     for(i=0;i<in[j];i++)
      {
       gotoxy(i+2,j+3);
       printf("%c",buf[j][i]);
      }
    }
    gotoxy(sx,sy);
  }
  else
   {
    sx=wherex();
    sy=wherey();
   }
  }
 }
}

else if(fl!=1)
{
 tx=wherex();
 ty=wherey();
 printf("%c",ch);
 if(buf[ty-3][tx-3]=='\0' && tx-3>=0)
  {
   for(i=0;i<tx-2;i++)
    {
     if(buf[ty-3][i]=='\0')
      {
       buf[ty-3][i]=' ';
       in[ty-3]++;
       }
      }
     }
    buf[ty-3][tx-2]=ch;
    in[ty-3]++;
   }
  }
  if(c!=24 && c!=72 && c!=80 && c!=75 && c!=77 && c!=33 && c!=3 && c!=35)
  chrc();
 }
}





Posted in , , | Leave a comment