File Handling

C++ Program to Count Occurrence of a Word in a Text File

This C++ program will read  a word from user and then count its total occurrence in a text file “my_data.txt”. Make sure you have already create this text file and have some text in it. Place this file in the same directory where your program source file is present. #include<iostream.h> #include<fstream.h> #include<string.h> int main() { …

C++ Program to Count Occurrence of a Word in a Text File Read More »

C program that reads a file containing integers and appends at its end the sum of all the integers

#include<stdio.h> #include<conio.h> #include<process.h> main() { int a,i,n,sum=0; FILE *fp; clrscr(); //Writing numbers to the file fp=fopen(“DATA”,”w”); if(fp==NULL) { printf(“File could not open!!”); exit(0); } printf(“How many numbers?”); scanf(“%d”,&n); printf(“Enter numbers in the file:n”); for(i=0;i<n;++i) { scanf(“%d”,&a); putw(a,fp); } fclose(fp); //Reading the file and doing sum fp=fopen(“DATA”,”r”); if(fp==NULL) { printf(“File could not open!!”); exit(0); } while((a=getw(fp))!=EOF) …

C program that reads a file containing integers and appends at its end the sum of all the integers Read More »

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); …

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 Read More »

C program to write student record to a binary file

#include<stdio.h> #include<conio.h> #include<stdlib.h> struct student { int rollno; char *name; int m1,m2,m3; }s1; void main() { FILE *fp; clrscr(); fp=fopen(“Record.dat”,”wb”); //opening binary file in writing mode if(fp==NULL) { printf(“File could not open”); exit(0); } printf(“Enter student detailsn”); printf(“Roll No:”); scanf(“%d”,&s1.rollno); printf(“Name:”); scanf(“%s”,s1.name); printf(“Marks in three subjects:”); scanf(“%d%d%d”,&s1.m1,&s1.m2,&s1.m3); fwrite(&s1,sizeof(s1),1,fp);     //writing to binary file printf(“nRecord has …

C program to write student record to a binary file Read More »

C program to copy the contents of one file into another

#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { FILE *fp1,*fp2; char ch,*f1,*f2; clrscr(); printf(“Enter source file name(ex:source.txt): “); scanf(“%s”,f1); printf(“Enter destination file name(ex:destination.txt): “); scanf(“%s”,f2); fp1=fopen(f1,”r”); fp2=fopen(f2,”w”); if(fp1==NULL||fp2==NULL) { printf(“File could not open!!”); exit(0); } while((ch=getc(fp1))!=EOF) putc(ch,fp2); fclose(fp1); fclose(fp2); }

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 }