File Handling

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 }

C++ Program to read from a text file and than write in another text file

#include<fstream.h> void main(){ ofstream fout(“sare1.txt”); //create a file to writeifstream fin(“sare1.txt”);fout<<“Hello….!!”;fout.close();                            //closing the file fout.open(“sare2.txt”); //create file to writechar ch;while(fin) //loop wiill run till end of file{fin>>ch;       //reading data from filefout<<ch;       //writing data to file}fin.close();fout.close();}/*you can …

C++ Program to read from a text file and than write in another text file Read More »