Reading lines of various sizes in .csv file

  • Thread starter Thread starter salmon27
  • Start date Start date
S

salmon27

Guest
I am trying to read from a .csv file that has about 50 lines. Each line/row has at least 2 columns, but some have up to 50 columns. I am trying to store each line into a List to split() later, but it's reading the line as if it has the maximum number of columns even if it doesn't. For example:

1. AK Alaska CD PC
2. AL ALABAMA AT FL GA TN MS


The string for line 2 is coming out correct, as "AL,ALBAMA,AT,FL,GA,TN,MS"

The string for line one, however, is reading "AK,Alaska,CD,PC,,,,"

How do I get rid of the extra commas and only read the next value if it's not null?

Code:

string filename = ofdStates.FileName;
string line;

System.IO.StreamReader file = new System.IO.StreamReader(filename);

while ((line = file.ReadLine()) != null)
{
dataList.Add(line);
}

Continue reading...
 
Back
Top