My code below has a problem and I can not figure it out,
but it misses the first line of data from data file
and repeats the last line, can anyone tell me how to fix it?
// Purpose: To read a sequential access file
//that contains more than one field
//
#include <iostream.h>
#include <ctype.h>
#include <fstream.h>
//define record structure
struct payrollinfo
{
char code;
short salary;
};
void main()
{
//declare and initialize record variable
payrollinfo payroll = {' ',0};
//open input file
ifstream infile;
infile.open ("C:\\WINNT\\Profiles\\stcurs\\Desktop\\T7Be01.dat",ios::in);
//determine if open was successful
if (!infile.fail())// open was successful
{
infile>>payroll.code;
infile.ignore(1);
infile>>payroll.salary;
infile.ignore(1);
cout<<"Code "<<" "<<"Salary"<<endl;
while (!infile.eof())
{
infile>>payroll.code;
infile.ignore(1);
infile>>payroll.salary;
infile.ignore(1);
cout<<payroll.code<<" "<<payroll.salary<<endl;
}
//close file
infile.close();
// cout<<"Payroll Code: "<<payroll.code<<"Payroll Salary: "<<payroll.salary;
}
else //open was not sucessful
cout<<"error in opening file."<<endl;
//end if
}//end of main function
but it misses the first line of data from data file
and repeats the last line, can anyone tell me how to fix it?
// Purpose: To read a sequential access file
//that contains more than one field
//
#include <iostream.h>
#include <ctype.h>
#include <fstream.h>
//define record structure
struct payrollinfo
{
char code;
short salary;
};
void main()
{
//declare and initialize record variable
payrollinfo payroll = {' ',0};
//open input file
ifstream infile;
infile.open ("C:\\WINNT\\Profiles\\stcurs\\Desktop\\T7Be01.dat",ios::in);
//determine if open was successful
if (!infile.fail())// open was successful
{
infile>>payroll.code;
infile.ignore(1);
infile>>payroll.salary;
infile.ignore(1);
cout<<"Code "<<" "<<"Salary"<<endl;
while (!infile.eof())
{
infile>>payroll.code;
infile.ignore(1);
infile>>payroll.salary;
infile.ignore(1);
cout<<payroll.code<<" "<<payroll.salary<<endl;
}
//close file
infile.close();
// cout<<"Payroll Code: "<<payroll.code<<"Payroll Salary: "<<payroll.salary;
}
else //open was not sucessful
cout<<"error in opening file."<<endl;
//end if
}//end of main function