0

I am in the beginning stages of making a c# app that downloads a .zip file, extracts a .csv file, and inserts it to a mssql db. Problem is the file contains 2 unnecessary lines at the beginning of the file. Is there a way to strip the 1st 2 lines out? This is geoip data from maxmind.com. Ideally the program would read the .csv, strip out the lines, and resave it. Any advice is welcome. Sample rows below.

Copyright (c) 2011 MaxMind Inc.  All Rights Reserved.
"beginIp","endIp","beginIpNum","endIpNum","countryCode","countryName"
"1.0.0.0","1.0.0.255","16777216","16777471","AU","Australia"
"1.0.1.0","1.0.3.255","16777472","16778239","CN","China"
"1.0.4.0","1.0.7.255","16778240","16779263","AU","Australia"
cpound
  • 85
  • 2
  • 12
  • When you read the file, just add 2 `reader.ReadLine();` statements before beginning to loop over the real data. – itsme86 Jul 16 '14 at 02:18

2 Answers2

3

As @itsme86 said in a comment, you'd probably be best to simply skip over those two lines with

reader.ReadLine();
reader.ReadLine();

That way, you don't have to worry about copying the full file and resaving it at all. However, if you do want to do that (and I notice you say "Ideally the program would read the .csv, strip out the lines, and resave it"), you can review my answer to a similar question: Cut and paste line of text from text file c#.

In essence, you'll have to read through the full file and copy it down to another.

using (var writer = new StreamWriter(newFile))
using (var reader = new StreamReader(oldFile)) // or you could take this from your `WebResponse` `Stream` if applicable.
{
    reader.ReadLine();
    reader.ReadLine();

    string line;
    while ((line = reader.ReadLine()) != null)
    {
        writer.WriteLine(line);
    }
}
Community
  • 1
  • 1
Matthew Haugen
  • 12,916
  • 5
  • 38
  • 54
1

There is a built in csv parser in .Net

The following link shows an example http://coding.abel.nu/2012/06/built-in-net-csv-parser/

Just skip the first and second line the same way the example skips the first line.

TGH
  • 38,769
  • 12
  • 102
  • 135