read from file-Error
I'm writing a program to retrieve data from a text file and store them in a struct but the function completely skips the while loop.
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include<iostream>
#include<cstdlib>
#include<string>
#include<fstream>
#include<windows.h>
using namespace std;
struct patient
{
int id;
int blood;
int day;
int month;
int year;
int phone;
string name;
string surname;
string addr;
string disease;
};
void retrieve(void)
{
system("cls");
struct patient p;
ifstream pfile;
pfile.open("patient.txt");
if(pfile.fail())
{
system("cls");
cout<<"Error Opening the file";
exit(1);
}
else{
system("cls");
cout<<"Open successful";
system("pause");
}
system("cls");
pfile>>p.id>>p.name>>p.surname>>p.addr>>p.disease>>p.phone>>p.blood>>p.day>>p.month>>p.year;
while(pfile.good())
{
cout<<p.id<<" "<<p.name<<" "<<p.surname<<" "<<p.addr<<" "<<p.disease<<" "<<p.phone<<" "<<p.blood<<" "<<p.day<<" "<<p.month<<" "<<p.year<<endl;
system("pause");
pfile>>p.id>>p.name>>p.surname>>p.addr>>p.disease>>p.phone>>p.blood>>p.day>>p.month>>p.year;
}
system("PAUSE");
pfile.close();
}
|
Please show a small sample of the input file.
But if your first extraction somehow fails the stream will be in an error state and the while loop will seem to be skipped.
Yeah i made some mistakes when writing to the file itself, its working fine now :) thank you for your response
Topic archived. No new replies allowed.