Powered By Blogger

Wednesday 27 April 2011

This C programme will split one Source txt file into two equally sized text file

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main(void)
{
int enter=0,lines=0,count=0,flag=0;
int i=0,condition=0;
char temp='\n';
FILE *fp1;
FILE *fp2;
FILE *fp3;
fp1=fopen("exsisting.txt","a+");
fp2=fopen("splitted1.txt","w+");
fp3=fopen("splitted2.txt","w+");

i=0;


while(1)
{
    if(fscanf(fp1,"%c",&temp)==EOF)
    {
        break;
    }
    if(temp=='\n')
    {
        enter=enter+1;
    }
    count=count+1;
}

fclose(fp1);
fp1=fopen("exsisting.txt","a+");
i=0;

if(enter==0&&count==0)
{
    lines=0;
}
if(enter>0||count>0)
{
lines=(enter+1);
}

condition=(lines/2);
enter=0;
if(lines==1)
{
    enter=-1;
}


while(1)
{
    if(fscanf(fp1,"%c",&temp)==EOF)
    {
        break;
    }
    if(temp=='\n')
    {
        enter=enter+1;
    }

    if(enter<condition)
    {
        fprintf(fp2,"%c",temp);
    }
   
    if(enter==condition&&flag==0)
    {
        fscanf(fp1,"%c",&temp);
        flag=1;
    }

    if(enter>=condition)
    {
        fprintf(fp3,"%c",temp);
    }
}

if(lines==1)
{
    condition++;
}

if(count==0)
{
condition=0;
lines=0;
}

printf("The number of lines present in the source files :: %d\n",lines);
printf("The number of lines present in the First source files :: %d\n",condition);
printf("The number of lines present in the Second source files :: %d\n",(lines-condition));

fclose(fp1);
fclose(fp1);
fclose(fp1);
getch();
return 0;
}

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. If u compile this program in VC 2006 or 2008 or 2010 environment... make a file named "exsisting.txt" in the folder where this source Cpp file exists and put some text over there into the file.. then run this program.. it will split the file into two text files named "splitted1.txt" and "splitted2.txt"...

    ReplyDelete