c program to compare two files without using predefined Function


#include<stdio.h>  

void main()
{
FILE *fp1, *fp2, *fopen();
int ca, cb;
char fname1[40], fname2[40] ;
printf("Enter name of first file :") ;
gets(fname1);
printf("Enter name of second file:");
gets(fname2);
fp1 = fopen( fname1, "r" ); /* open file1 for reading */
fp2 = fopen( fname2, "r" ) ; /* open file 2 for reading */
if ( fp1 == NULL ) /* check does file exist etc */
{
printf("Cannot open %s for reading \n", fname1 );
exit(1); /* terminate program */
}
else if ( fp2 == NULL )
{
printf("Cannot open %s for reading \n", fname2 );
exit(1); /* terminate program */
}
else /* both files opened successfully */
{
ca = getc( fp1 ) ;
cb = getc( fp2 ) ;
while ( ca != EOF && cb != EOF && ca == cb )
{
ca = getc( fp1 ) ;
cb = getc( fp2 ) ;
}
if ( ca == cb )
printf("Files are identical \n");
else if ( ca != cb )
printf("Files differ \n" );

fclose ( fp1 );
fclose ( fp2 );
}
}

c program to count number of lines in a file, Number of lines in given text file


#include<stdio.h>     

void main()
{
FILE *fopen(), *fp;
int c , nc, nlines;
char filename[40] ;
nlines = 0 ;
nc = 0;
printf("Enter the file name: ");
gets( filename );
fp = fopen( filename, "r" );
if ( fp == NULL )
{
printf("Sorry Cannot open %s for reading \n", filename );
exit(1); /* End program */
}
c = getc( fp ) ;
while ( c != EOF )
{
if ( c == '\n' )
nlines++ ;
nc++ ;
c = getc ( fp );
}
fclose( fp );
if ( nc != 0 )
{
printf("There are %d characters in %s \n", nc, filename );
printf("There are %d lines \n", nlines );
}
else
{
printf("Ohh.h....File: %s is empty \n", filename );
}
}

c Program to count characters in given file, Count characters in from file, number of characters in given file


#include<stdio.h>     

void main()
{
FILE *fopen(), *fp;
int c , nc, nlines;
char filename[40] ;
nlines = 0 ;
nc = 0;
printf("Enter file name: ");
gets( filename );
fp = fopen( filename, "r" );
if ( fp == NULL )
{
printf("Cannot open %s for reading \n", filename );
exit(1); /* terminate program */
}
c = getc( fp ) ;
while ( c != EOF )
{
if ( c == '\n' )
nlines++ ;
nc++ ;
c = getc ( fp );
}
fclose( fp );
if ( nc != 0 )
{
printf("There are %d characters in %s \n", nc, filename );
printf("There are %d lines \n", nlines );
}
else
{
printf("File: %s is empty \n", filename );
}
}

C Program to copy one file contents to another file



#include<stdio.h>

void main()
{
FILE *fp1, *fp2, *fopen();
int c ;
fp1 = fopen( "www.programmingmansion.co.cc", "r" ); /* open for reading */
fp2 = fopen( "prog.old", "w" ) ; /* open for writing */
if ( fp1 == NULL ) /* check does file exist etc */
{
printf("Cannot open www.programmingmansion.co.cc for reading \n" );
        exit(1);    /* terminate program */   
}
else if ( fp2 == NULL )
{
printf("Cannot open prog.old for writing \n");
exit(1); /* terminate program */
}
else /* both files O.K. */
{
c = getc(fp1) ;
while ( c != EOF)
{
putc( c, fp2); /* copy to prog.old */
c = getc( fp1 ) ;
}
fclose ( fp1 ); /* Now close files */
fclose ( fp2 );
printf("Files successfully copied \n");
}
}