Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data file misses first line and repeats last whats wrong 1

Status
Not open for further replies.

ggreg

Programmer
Mar 9, 2001
201
US
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 (&quot;C:\\WINNT\\Profiles\\stcurs\\Desktop\\T7Be01.dat&quot;,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<<&quot;Code &quot;<<&quot; &quot;<<&quot;Salary&quot;<<endl;
while (!infile.eof())

{
infile>>payroll.code;
infile.ignore(1);

infile>>payroll.salary;
infile.ignore(1);

cout<<payroll.code<<&quot; &quot;<<payroll.salary<<endl;


}

//close file

infile.close();
// cout<<&quot;Payroll Code: &quot;<<payroll.code<<&quot;Payroll Salary: &quot;<<payroll.salary;


}
else //open was not sucessful
cout<<&quot;error in opening file.&quot;<<endl;
//end if

}//end of main function
 
First off... your missing the first line because you are not outputing what you read in when you begin the while loop. you are doing another input. Get rid of the input before the while and you will see the first line. As for repeating the last line, I dont see anything jumping out at me. Maybe the >> operator does nothing if the file is at EOF so the data in the struct does not change? I have had problems in the past with eof. It can be a tricky little bugger.

Matt
 
Way to go Zyrenthian,
it gave me my first record but now it is still giving me
the last record twice!LOL
but so far thanks a Bunch!!!!!!!!!!!!!!
 
Something I have used in the past, but I dont know if it will apply here is


while(!infile.eof() && infile.peek() != EOF)

see if that stops your extra line though I dont put too much trust in it

Send me a copy of your data file
zyrenthian@home.com
and i will cut and paste your code and see what I come up with.


Matt


 
I tried your line of code, no difference,
a copy of data file coming your way.
sorry I am also sending the code again!

thanks abunch for trying!
 
Hi,
I made some small changes into your program:
void main()
{
payrollinfo payroll = {32,0};
ifstream infile;
infile.open (&quot;xx.txt&quot;,ios::in);
if (infile.good())
{
infile>>payroll.code;
infile.ignore(1);
infile>>payroll.salary;
infile.ignore(1);
cout<<&quot;Code &quot;<<&quot; &quot;<<&quot;Salary&quot;<<endl;
//first change here:
cout<<payroll.code<<&quot; &quot;<<payroll.salary<<endl;
while (infile)//second change
{
infile>>payroll.code;
infile.ignore(1);
infile>>payroll.salary;
infile.ignore(1);
cout<<payroll.code<<&quot; &quot;<<payroll.salary<<endl;
}
infile.close();
}
else
cout<<&quot;error in opening file.&quot;<<endl;
}
John Fill
1c.bmp


ivfmd@mail.md
 
John- I tested your code,
your code gives me the missing first record but still repeats last value, now matt did send me working code,
My instructor too gave me working code,

here's what my instructor had me do to my code:
while (!infile.eof())

{
cout<<payroll.code<<&quot; &quot;<<payroll.salary<<endl;
infile>>payroll.code;
infile.ignore(1);

infile>>payroll.salary;
infile.ignore(1);
He told me to move my cout line and put it before my
infile>>payroll,

got to tell you before I try here, I move that line around without success but I guess i never put it below the while!

Thanks again John and matt, Matt one thing you show me is
there is more than one way to write code in C++ !



 
I don't understand what did you do. On my computer it works fine. Below is my full code:

#include <iostream.h>
#include <ctype.h>
#include <fstream.h>
struct payrollinfo
{
char code;
short salary;
};

void main()
{
payrollinfo payroll = {32,0};
ifstream infile;
infile.open (&quot;xx.txt&quot;,ios::in);
if (infile.good())
{
cout<<&quot;Code &quot;<<&quot; &quot;<<&quot;Salary&quot;<<endl;
do
{
infile>>payroll.code;
infile.ignore(1);
infile>>payroll.salary;
infile.ignore(1);
cout<<payroll.code<<&quot; &quot;<<payroll.salary<<endl;
}while (infile);
infile.close();
}
else
cout<<&quot;error in opening file.&quot;<<endl;
} John Fill
1c.bmp


ivfmd@mail.md
 
John,
copy it just as above but it still duplicates my last record
Wonder with your code if because I am running NT if that
could cause a difference, I gonna take your code into class
tomorrow night and try it there.


hey would like to ask about this line of code:
if (infile.good())
does the above mean as long as there are records same as
eof--- is it a new command or a pretty old command?
 
About the OS, I'm working also in NT(Win2000Server).
About .bad() and .good(), it indicates -- did appear an error or not. If for example is .bad(), you can restore .good() by calling .reset(). John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top