Sunteți pe pagina 1din 5

#include <stdio.

h>
#include <stdlib.h>

void main ()
{
FILE *fp, *fp1;
char fn[100], c;
int lc = 0, cc = 0;
printf( "\nEnter filename for reading" );
scanf( "%s", fn );
fp = fopen( fn, "r" );
if ( fp == NULL )
{
printf( "\nFile can't be opened" );
exit( 0 );
}

c = fgetc( fp );
while ( c != EOF )
{
if ( c == '\n' )
{
lc++;
}
else
{
cc++;
}
c = fgetc( fp );
}
printf( "Line Count: %d\nCharacter Count: %d\n", lc, cc );
close( fp );

}
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE * fp;
int count = 0;
fp=fopen( "test.txt","r" );
if( fp!=NULL )
{
char ch;
char c = fgetc( fp );
while( c!=EOF )
{

printf( "%c",c );
if( c=='\n' )
count++;
if( count==5 )
{
count=0;
scanf( " %c", &ch );
if( ch=='Q' )
break;

}
c=fgetc( fp );
}
}
else
printf( "Error" );
return 0;
}
#include <stdio.h>
#include <stdlib.h>

void main ()
{
FILE *fp;
char fn[100];
int ec = 0, c = 0;
while ( ec <= 3 )
{
printf( "\nEnter filename for reading" );
scanf( "%s", fn );
fp = fopen( fn, "r" );
if ( fp == NULL )
{
c = 1;
ec++;
printf( "\nFile can't be opened" );
printf( "\nNo of attempts left: %d", 3 - ec );
}
if ( c == 0 )
{
printf( "\nFile opened" );
exit ( 0 );
}
if ( ec >= 3 )
{
printf( "\nNo more attempts left" );
exit ( 0 );
}
c = 0;
}
}
#include <stdio.h>
#include <stdlib.h>

void main ()
{
FILE *fp;
char fn[100], c;
printf( "\nEnter filename for reading" );
scanf( "%s", fn );
fp = fopen( fn, "r" );
if ( fp == NULL )
{
printf( "\nFile can't be opened" );
exit( 0 );
}

c = fgetc( fp );
fseek( fp, 0, SEEK_END );
printf( "Size: %ld bytes", ftell( fp ) );
close( fp );

}
#include <stdio.h>
#include <stdlib.h>

void main ()
{
FILE *fp, *fp1;
char fn[100], c, d;
printf( "\nEnter filename for first file" );
scanf( "%s", fn );
fp = fopen( fn, "r" );
if ( fp == NULL )
{
printf( "\nFile can't be opened" );
exit( 0 );
}
printf( "\nEnter filename for second file" );
scanf( "%s", fn );
fp1 = fopen( fn, "r");
if ( fp1 == NULL )
{
printf( "\nFile can't be opened" );
exit( 0 );
}
c = fgetc( fp );
d = fgetc( fp1 );
while ( c != EOF || d!= EOF )
{
if ( c != d )
{
printf( "\nNot Similar" );
exit( 0 );
}
c = fgetc( fp );
d = fgetc( fp1 );
}
printf( "\nContents in both files are similar" );
close( fp );
close( fp1 );
}

S-ar putea să vă placă și