C++ Language



Admission Enquiry Form

  

Read Integers from The File


Write a code to read two integers from the file.

//reading two integers from the file
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int num1,num2;
// To open file in read mode
ifstream fin("c:\\compuhelp\\first.txt");
//fin.open("c:\\compuhelp\\first.txt");
fin>>num1>>num2;
cout<<"\n Value of num1= "<<num1;
cout<<"\n Value of num2= "<<num2;
fin.close();
}




Reading data from the file character by character

//Reading data from the file character by character
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream fin;
char ch;
fin.open("compuhelp.txt");
if(fin.is_open())
{
while(true)
{
ch=fin.get();

if(fin.eof())
{
break;
}
else
{
cout<<ch;
}
}//end of while loop
}
else
{
cout<<endl<<"could not open file.";
}
}//end of main