Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Ujjwal Kumar authored Oct 2, 2020
1 parent dd6233e commit c78425a
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions fileHandling.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*This code shows the use of file handling which is
*very useful when it comes to projects in C
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int primeTest(int n)
{
int j,flag=-1;
for(j=2;j<=sqrt(n);j++)
{
if(n%j==0)
{
flag=1;
break;
}
}
if(flag==1)
return 0;
else
return 1;
}
int main()
{
int i;
char c;
FILE *src,*trg;
src=fopen("source.txt","w");
if(src==NULL)
{
printf("ERROR IN OPENING FILE\n");
return 0;
}
for(i=1001;i<10000;i++)
fprintf(src,"%d ",i);
fclose(src);
trg=fopen("target.txt","w");
src=fopen("source.txt","r");
if(trg==NULL || src==NULL)
{
printf("ERROR IN OPENING FILE\n");
return 0;
}

while((fscanf(src,"%d",&i))!=-1)
{
if(primeTest(i))
fprintf(trg,"%d ",i);
}
fclose(src);
fclose(trg);
}

0 comments on commit c78425a

Please sign in to comment.