Read and Write into a File
Write a C++ Program to Read and Write student details into a File using File Handling. Here’s simple Program to Read and Write into a File using File Handling in C++ Programming Language.
Below is the source code for C++ Program to Read and Write student details into a File using File Handling which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C++ Program to Read and Write student details into a File */ #include<iostream> #include<stdio.h> #include<stdlib.h> #include<fstream> using namespace std; class student { public: int roll; char name[100],f_name[100]; void put(); void get(); void switch_case(); }; student s; void student::put() { fstream file; cout<<"\nEnter roll no :: "; cin>>roll; cout<<"\nEnter name :: "; cin>>name; cout<<"\nEnter father name :: "; cin>>f_name; file.open("C:\\Users\\acer\\Documents\\file4.txt",ios::out|ios::app); file.write((char *)this,sizeof(student)); file.close(); s.switch_case(); } void student::get() { int temp; cout<<"\nEnter roll no: "; cin>>temp; fstream file; file.open("C:\\Users\\acer\\Documents\\file4.txt",ios::in); file.seekg(0,ios::beg); while(file.read((char *)this,sizeof(student))); { if(roll==temp) { cout<<"\nName :: "<<name<<endl; cout<<"Roll no. :: "<<roll<<endl; cout<<"Father name :: "<<f_name<<endl<<endl; } } file.close(); s.switch_case(); } void student::switch_case() { int i; cout<<"\n1)- Read\n2)- Write\n3)- Exit)\n\nEnter your choice :: "; cin>>i; switch(i) { case 1: s.put(); break; case 2: s.get(); break; case 3: exit(0); default: cout<<"\nWrong choice "; } } int main() { s.switch_case(); return 0; }
OUTPUT : :
/* C++ Program to Read and Write student details into a File */ 1)- Read 2)- Write 3)- Exit) Enter your choice :: 1 Enter roll no :: 123 Enter name :: CodezClub Enter father name :: www.codezclub.com 1)- Read 2)- Write 3)- Exit) Enter your choice :: 2 Enter roll no: 123 Name :: CodezClub Roll no. :: 123 Father name :: www.codezclub.com 1)- Read 2)- Write 3)- Exit) Enter your choice :: 3 Process returned 0
Above is the source code for C++ Program to Read and Write student details into a File which is successfully compiled and run on Windows System.The Output of the program is shown above .
If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.
Thanks for reading the post….