C program to read integer numbers from a file named DATA and then write all odd numbers to a file named ODD and all even numbers to a file named EVEN

#include<stdio.h> #include<conio.h> #include<process.h> void main() { int a,n,i; FILE *fp1,*fp2,*fp3; clrscr(); fp1=fopen(“DATA”,”w”); if(fp1==NULL) { printf(“File could not open!!”); exit(0); } printf(“How many numbers?”); scanf(“%d”,&n); printf(“Enter contents of DATA file:n”); for(i=0;i<n;++i) { scanf(“%d”,&a); putw(a,fp1); } fclose(fp1); fp1=fopen(“DATA”,”r”); fp2=fopen(“ODD“,”w”); fp3=fopen(“EVEN”,”w”); if(fp1==NULL||fp2==NULL||fp3==NULL) { printf(“File could not open!!”); exit(0); } while((a=getw(fp1))!=EOF) { if(a%2!=0) putw(a,fp2); else putw(a,fp3); } fclose(fp1); fclose(fp2);… Read More »

C program to read data from keyboard and write it to a text file

#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { FILE *fp; char ch,file[10]; clrscr(); printf(“Enter file name:”); scanf(“%s”,file); fp=fopen(file,”w”);   //file opening if(fp==NULL)   //exit program if file doesn’t open { printf(“File could not open!!”); exit(0); } printf(“Enter data(* to exit)n”); while(1) { ch=getche(); if(ch==’*’)      //exit when * is pressed exit(0); putc(ch,fp); } fclose(fp);   //file closing }