Character Stuffing

#include<stdio.h>
#include<conio.h>
void main()
{
    FILE *fp,*fp1;
    char ch,k[4],j[9],c[2];
    long beg,end;
    clrscr();
    if((fp=fopen("input.txt","w"))==NULL)
    {
        printf("input.txt file opening prblem");
        exit(0);
    }
    if((fp1=fopen("csource.txt","w"))==NULL)
    {
        printf("csource.txt file opening problem");
        exit(0);
    }
    printf("\n enter the data to send put$ at end \n");
    while(1)
    {
        scanf("%c",&ch);
        if(ch=='$')
        break;
        putc(ch,fp);
    }
    fclose(fp);
    if((fp=fopen("input.txt","r"))==NULL)
    {
        printf("input.txt file opening problem \n");
        exit(0);
    }
    fputs("DLE STX ",fp1);
    while((ch=getc(fp))!=EOF)
    {
        if(ch=='D')
        {
            c[0]=getc(fp);
            c[1]=getc(fp);
            if(c[0]=='L'&&c[1]=='E')
            fputs("DLE",fp1);
            putc(ch,fp1);
            putc(c[0],fp1);
            putc(c[1],fp1);
        }
        else
        putc(ch,fp1);
    }
    fputs(" DLE ETX",fp1);
    fclose(fp);
    fclose(fp1);
    if((fp=fopen("csource.txt","r"))==NULL)
    {
        printf("csource.txt opening problem");
    }
    if((fp1=fopen("cdest.txt","w"))==NULL)
    {
        printf("cdest.txt opening problem");
    }
    printf("\n data after stuffing \n");
    while((ch=getc(fp))!=EOF)
    putc(ch,stdout);
    fclose(fp);
    fp=fopen("csource.txt","r");
    beg=ftell(fp);
    beg+=9;
    fseek(fp,-0L,2);
    end=ftell(fp);
    end-=8;
    fclose(fp);
    fp=fopen("csource.txt","r");
    printf("data after destuffing \n");
    fgets(j,9,fp);
    while(beg<=end)
    {
        ch=getc(fp);
        if(ch=='D')
        {
            c[0]=getc(fp);
            c[1]=getc(fp);
            if(c[0]=='L'&&c[1]=='E')
            {
                fgets(k,5,fp);
                beg+=5;
            }
            putc(ch,fp1);
            putc(c[0],fp1);
            putc(c[1],fp1);
            putc(ch,stdout);
            putc(c[0],stdout);
            putc(c[1],stdout);
            beg+=2;
        }
        else
        {
            putc(ch,fp1);
            putc(ch,stdout);
        }
        beg++;
    }
    fclose(fp);
    fclose(fp1);
    getch();
}

No comments:

My Favorite Books

  • C and Data Structures by Ashok N. kamthane
  • Web Technologies by A. A. Puntambekar
  • Learn HTML and CSS with W3Schools
  • Learn JavaScript and Ajax with W3Schools
  • HTML5 Black Book: Covers Css3, Javascript,XML, XHTML, Ajax, PHP And Jquery
  • HTML5 Application Development Fundamentals: MTA Exam 98-375
  • .NET 4.0 Programming 6-In-1, Black Book
  • SQL Server 2008 R2 Black Book
  • Asp.net 4.0 Projects Covers: net 3.5 And .net 4.0 Codes, Black Book
  • UNIX Shell Programming 1 Edition by Yashavant Kanetkar
  • UNIX and Shell Programming 1 Edition by Richard F. Gilberg, Behrouz A. Forouzan
  • Computer Networks by Andrew S. Tanenbaum
  • Multiple Choice questions in computer science by Timothy J Williams