Reputation: 71
I am totally new to C#, I want to read a CSV file line by line and write to another CSV file while writing I need to skip the first 4 lines, Can anyone help me with this?
Thanks in advance.
Below is the code I tried.
int index = 0;
using (StreamWriter writer = new StreamWriter(File.OpenWrite("D:\\work\\POCs\\POC_RESOURCES\\Pricing_Files\\index-edited-ap-east-1.csv")))
using (StreamReader reader = new StreamReader(File.OpenRead("D:\\work\\POCs\\SPOT_POC_RESOURCES\\Pricing_Files\\index-ap-east-1.csv")))
{
while (!reader.EndOfStream)
{
Console.WriteLine(index);
string line = reader.ReadLine();
writer.WriteLine(line);
}
}
Upvotes: 0
Views: 1776
Reputation: 3498
To Skip the first 4 lines of the input file you need to read a line on every iteration of the while
loop
using (var writer = new StreamWriter(File.OpenWrite("D:\\work\\POCs\\POC_RESOURCES\\Pricing_Files\\index-edited-ap-east-1.csv")))
using (var reader = new StreamReader(File.OpenRead("D:\\work\\POCs\\SPOT_POC_RESOURCES\\Pricing_Files\\index-ap-east-1.csv")))
{
var index = 0;
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
if (index > 4)
{
Console.WriteLine(index);
writer.WriteLine(line);
}
index++;
}
}
Upvotes: 1