Problem Statement: Write a C++ program that creates an output file, writes information to it, closes the file, open it again as an input file and read the information from the file.
Note:- Scroll horizontally to see the full line of code.
#include<iostream>
#include<fstream>
#include<bits/stdc++.h>
#include<string.h>
using namespace std;
int main(){
int roll_no;
string name, department, college_name;
ofstream f1;
f1.open("File.txt");
// cout<<"File Created: File.txt"<<endl;
if(f1){
cout<<endl;
cout<<"Enter the details:"<<endl;
cout<<"Name: ";
getline(cin, name);
cout<<"College Name: ";
getline(cin, college_name);
cout<<"Department: ";
getline(cin, department);
cout<<"Roll No.: ";
cin>>roll_no;
f1<<"Details of student are as follows: "<<endl;
f1<<"Name: "<<name<<endl;
f1<<"College Name: "<<college_name<<endl;
f1<<"Department: "<<department<<endl;
f1<<"Roll No.: "<<roll_no<<endl;
cout<<endl;
}
f1.close();
cout<<"Reading File"<<endl;
ifstream f2;
string output;
f2.open("File.txt");
while(getline(f2, output)){
cout<<output<<endl;
}
f2.close();
return 0;
}
Comments
Post a Comment