From c78425a0b5cc9fac1f040028778709fd45d60c77 Mon Sep 17 00:00:00 2001 From: Ujjwal Kumar <70900673+ujjwalll26@users.noreply.github.com> Date: Fri, 2 Oct 2020 19:31:07 +0530 Subject: [PATCH] Add files via upload --- fileHandling.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 fileHandling.c diff --git a/fileHandling.c b/fileHandling.c new file mode 100644 index 0000000..a1eb207 --- /dev/null +++ b/fileHandling.c @@ -0,0 +1,52 @@ +/*This code shows the use of file handling which is + *very useful when it comes to projects in C +*/ +#include +#include +#include +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); +}