I am working on a program that you can select a header from a csv file(it parses the headers out initially) and i want the user to be able to get the column’s data without having to read the entire file.(making a list of string arrays and pulling out the string array index for the information, which is the simple easy thing to do).
Is there a way i can use seek to do this? maybe search for a string(which would be the header) and get the information within its column.
This is the getHeader function in case it helps…
private string[] getHeader(string path)//gets the headers from the file path specified.
{
List<string> row = new List<string>();
using (StreamReader readFile = new StreamReader(path))
{
row = (readFile.ReadLine().Split(',').ToList());
}
return row.ToArray();
}
(the to list and to array is there just to get around setting a size for an array initially…)
Thanks!
Impossible.
Seek can go to a definite position in the file. This would be an OK idea if you had a fixed length record (SDF/COBOL) file with HUGE record lengths and few records.
Unfortunately, .csv is by definition Variable Length Records. You can only tell where one record stops and starts based on hitting the cr/lf at the end of the record.
Also this would not be a good idea even with most fixed-record formats. Due to buffering, the OS will read the whole file anyway, as you would be seeking ahead smaller amounts then the OS has preloaded.
I can see why you would want to do this; intuitively, it sounds like it would be faster. While you should always design your code with speed in mind, this is low-level enough to qualify as ‘premature optimization’ – google that. Basically you have to prove to yourself that it runs slow by writing it, then, when you see it’s a huge impediment to functionality, you can optimize.
Don’t parse it yourself!
MS has an assy for that.
Microsoft.VisualBasic.FileIO.TextFieldParser. Yes you can use it with C# (even though the sample below is in VB, you can figure out what you’ll need to do). Don’t be too proud to include a reference to VB. Time and money in your pocket matter, not being able to say, ‘I won’t touch anything VB with a 10-foot pole’. Changes are the assembly will already by on your target PCs. Only concern is if you have program install package download size issues, or, are deploying to a non-PC platform.