File input cannot be read when running the program
May 25, 2016 at 7:46am UTC
Hi, I would like to ask if there is any error in my code because I cannot read my input data when I run the program for read data from input file. Thank you
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
{
ifstream StreamInput;
StreamInput.open("InputData.txt" );
StreamInput >> ArraySize;
string name;
bool status=false ;
cin.ignore();
cout<<"\nEnter name to search : " ;
getline(cin,name);
for (int i=0;i<ArraySize;i++)
{
if (name == Student[i].Name)
{
cout<<"\n**********Search Successful**********" <<endl;
cout<<"\nTest ID : " <<Student[i].Admission_Id <<"\t" <<"\t" <<"Phone Number : " <<Student[i].Phone_Number<<endl;
cout<<"\nName : " <<Student[i].Name<<endl;
cout<<"\nFather's Name : " <<Student[i].Father_Name<<endl;
cout<<"\nAddress : " <<Student[i].Address<<endl;
status=true ;
}
break ;
}
if (status==false )
cout<<"\n**********Search Unsuccessful**********" <<endl;
}
Last edited on May 25, 2016 at 7:49am UTC
May 25, 2016 at 8:17am UTC
You should always check if the file opened correctly before to try to read or write to it.
1 2 3 4 5 6 7
#include <cstdio>
ifstream StreamInput("InputData.txt" );
if (!StreamInput)
{
perror("File error: " );
exit(1);
}
Topic archived. No new replies allowed.