1

I have written a C++ program I have multiple of CSV file used as input, which I open one at a time and close it after extracting data to a output file which is the only file.

I run getline(inFile,line); outFile << line << endl;

I run this code, only only few files it suddenly output after about 200-300 line after and have a big whitespace in my output CSV file

But when I slower the code, like system("Pause") in the loop, I can get extract what I want perfectly....

Is my program running to fast, why getline would be skipping part of what things I want?

I really have no idea where the problem is coming from, or where to start

Many Thanks!

if (dataname[i] == dataname)
{
    inFile.seekg(datalength[i], ios::beg);
    for (int j = 0; j < datacount[i]; j++)
    {
        getline(inFile, line);
        outFile << line << endl;
    }

}
eckomax87
  • 33
  • 1
  • 1
  • 6
  • I think we need to see a bit more of your code. Where does `datalength[i]` come from, for example? – Mats Petersson Jun 06 '13 at 09:36
  • It is just the length for seekg... – eckomax87 Jun 06 '13 at 09:45
  • And how did you get that "length"? My point is that, at least on Windows, the newlines are different length to `'\n'`, so seekg is not just the size of the data you have read. – Mats Petersson Jun 06 '13 at 09:47
  • Thanks Mat, let me explain how I did it, I got the length with, line.length();, I basically read the whole file and store the length in another CSV file. I have tested many random sample that it is correctly points to where I have calculated the length. The file format is like a security stock file just basic date,open,high,low,close. I just dont get why it would suddenly print out whitespace or "\n" if I run with too much multiple files to extract and put in one single file, but when system("pause"); it can do it perfectly.... – eckomax87 Jun 06 '13 at 09:53

1 Answers1

1

Refer to Why seekg does not work with getline?

Add a clear() call before the seekg(pos) call to clear all error flags that might have been created by the getline(inFile, line) call.

inFile.clear();  // Clears all error flags.
inFile.seekg(pos);
Community
  • 1
  • 1
Garland
  • 911
  • 7
  • 22