forked from harsh2102/C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileHandling.c
52 lines (51 loc) · 1015 Bytes
/
fileHandling.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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);
}