fstream doesn't detect eof

New to Visual C++ 2010 Express but not to C++ programming. I am trying to learn fstream coding. I want to convert a program from Visual C++ 6 where I used MFC classes. I admit I do not understand the iostream used in Express and probably have not coded this correctly. It shouldn't matter but this is on a Win 7 x64 system.

I am reading a file that contains NYSE stock data and there are 12000 input lines. The file was created in Excel in xls format and exported as a tabbed text file. The program reads and parses each line and writes the data out to an output file simply for testing.

I am having two problems.

First the program does not recognize and eof. If I rely solely on if (!infile.eof()) the program never stops long after all lines are read. I have to kill the program to stop it. The programs transfers the data to the output file but then continues to add bogus records to the output file.

When I added infile.good() it detects something wrong in one of the lines prior to eof. There is nothing wrong with the line as for as I can see. Comparing the input file to the output file the lines are identical. If I remove the break in the notgood else path the in file is not read but the for loop keeps incrementing and goes through the notgood path system pause until I kill the program. If I remove the break point I have to kill the program to get it to stop. It never recognizes the eof.

The other problem is that when I leave the test for good out of the program it process the files to the end and it works fine until the last line of the in file. Then it duplicates the last line of the input file in the output file except the data is offset one field to the right and it keeps adding information from the last line until I stop the program. This is the last few lines.

~NYSE 3/19/2012 1907 1907 1109 1109 3920 3196 1409.75
~NYSE 3/20/2012 1000 2031 1000 2031 2409 4600 1405.52
~NYSE 3/21/2012 1492 1519 1492 1519 3129 3889 1402.89
` ~NYSE 3 1519 1492 1519 3129 3889 1402.89
3 1519 1492 1519 3129 3889 1402.89
3 1519 1492 1519 3129 3889 1402.89
3 1519 1492 1519 3129 3889 1402.89
This last line is added forever until I kill the program.

I don't understand why it does not recognize the eof nor why it adds the last line forever. What am I doing wrong?

Thanks for the help.


This is the program.

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
char *filename1 = "D:\\FXR Method\\Method C 2010\\~nyse.txt";
fstream infile;
infile.open(filename1, ios::in);
if (infile.bad())
{
printf ("Could not open file %s\n", filename1);
system("Pause");
return 0;
}

char *filename2 = "D:\\FXR Method\\Method C 2010\\~nyse test.txt";
fstream outfile;
outfile.open(filename2, ios::out);
if (outfile.bad())
{
printf ("Could not open file %s\n", filename2);
system("Pause");
return 0;
}

for (int i=0; ;i++)
{
if (!infile.eof())
{
float o, h, l, c, uv, dv, spx;
char pdate[12], ticker[12];

infile >> ticker >> pdate >> o >> h >> l >> c >> uv >> dv >> spx;
// if(infile.good())
{
outfile << ticker << '\t' << pdate << '\t' << o << '\t' << h << '\t' << l << '\t' << c << '\t' << uv << '\t' << dv << '\t' << spx << '\n';
}
// else
// {
// printf("read line not good. line #&i\n", i);
// system("Pause");
// break;
// }
}
else
{
printf("eof found\n");
system("Pause");
break;
}
}
infile.close();
outfile.close();
return 0;
} // end main
why it does not recognize the eof

because the end of file was not reached (note that it's a useless check anyway, when dealing with formatted input)

nor why it adds the last line forever.

Because, as you wrote "it detects something wrong in one of the lines prior to eof." Once something is wrong, no further input takes place (you're expected to deal with the error first). Since no further input takes place, the values of your variables (ticker, pdate, o, h, l, c, uv, dv, spx) are never changed, and the loop keeps printing those values over and over.
Topic archived. No new replies allowed.