Hello everybody, I want to create a program that removes the first line from a text (csv) file (which is a header).

I used this code:

string deleteline = "Name, ID, D.O.B, etc., etc."; //contains the Header information to be removed
	string line;
	ifstream sup;
    sup.open("DOWNLOADED_DATA.csv");
    ofstream temp;
    temp.open("temp.csv");
	while (getline(sup,line))
    {
        if (line != deleteline)
        {
            temp << line << endl;
        }
    }
    temp.close();
    sup.close();

The code works as desired, creating a second file which indeed has the header removed. What is werid is that it copies about 95% of the file over, but not the entire file.

For example, if the non-header-removed file ends like with the last three lines like this:

1986-09-10,35.63,35.88,34.75,35.00,2737600,3.99
1986-09-09,34.63,36.00,34.63,35.75,5398400,4.08
1986-09-08,35.00,35.00,33.63,34.75,4522400,3.97

the modified (header-removed file), only makes it to:

1986-10-08,32.88,33.00,32.25,32.75,4021600,3.74
1986-10-07,34.00,34.13,32.88,33.00,4577600,3.77
1986-10-06,33.75,34.2

It doesn't even finish the last line that it was working on!

Any help would be greatly appreciated. Thank you very much!

Recommended Answers

All 5 Replies

Does

while (getline(sup,line)) temp << line << endl;

succeed in just copying the file?

If it does, then removing the first line is simply

getline(sup,line); // burn line 1
while (getline(sup,line)) temp << line << endl;

Using

while (getline(sup,line)) temp << line << endl;

does NOT copy the entire file - it stops copying at the same point. I've tried copying multiple files, and the same issue exists.

For example, for another file, the original version of the file ends with:

1990-09-11,36.00,36.13,33.75,34.00,6370800,8.01
1990-09-10,37.00,37.00,35.75,35.75,2732400,8.43
1990-09-07,35.50,36.75,35.13,36.38,2098800,8.57

While the copied version ends with

1990-10-15,28.50,28.75,26.62,27.75,7190000,6.54
1990-10-12,28.25,28.50,27.00,28.25,8169200,6.66
1990-10-11,26.75,27.87,25.50,27.75,7376800,6.

http://www.cplusplus.com/reference/iostream/ifstream/
Add some debug code, check the good, bad, eof bits.

Does it work for small files?

Which OS/Compiler are you using?

Are all the lines in the file pretty much the same length?

If you're processing many megabytes, and it's taking 10's of seconds, then try using something like task manager to watch the amount of memory allocated to the process. Is it fairly static (it should be), or a pretty steep slope?

Even with files that are 1/10th of the file size, the problem still exists.

Using OS X (10.6), compiling through XCode using GCC

Hey guys, a friend helped me out with the issue. I wasn't closing the file after using Curl to download it, so it wasn't fully written to disk when I was reading from it to make the temp file.

Thank you for all of your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.