-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ujjwal Kumar
authored
Oct 2, 2020
1 parent
dd6233e
commit c78425a
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |