PDA

View Full Version : C# code for pullinf the .drf CSV file into an array


douglasw32
08-01-2013, 05:26 PM
In VB6 I would do a loop until end of file and assign into an array
Then pull it by number to crunch something.

i.e.

open for input drf
Loop x until eof
the whole file loops into drf(x)
Then I process using drf(52) that gives me the 52nd comma field.

That is not the exact code but I hope it makes my point.

How do I do the same thing in C# ?

DeltaLover
08-01-2013, 05:30 PM
Try the following:

(1) Open the file using StreamReader

(2) Write a while statement reading the next line from the reader until you receive null

(3) Tokenize each line using the string.Split method providing comma (,) as the delimeter

Augenj
08-01-2013, 09:51 PM
In VB6 I would do a loop until end of file and assign into an array
Then pull it by number to crunch something.

i.e.

open for input drf
Loop x until eof
the whole file loops into drf(x)
Then I process using drf(52) that gives me the 52nd comma field.

That is not the exact code but I hope it makes my point.

How do I do the same thing in C# ?
Use VB.Net :D

headhawg
10-19-2013, 11:48 PM
Here's a function that might work. I'm messing around with .Net and both VB and C#. My nephew wrote this routine. static string[] Split(string expression, string delimiter,
string qualifier, bool ignoreCase)
{
bool _QualifierState = false;
int _StartIndex = 0;
System.Collections.ArrayList _Values = new System.Collections.ArrayList();

for (int _CharIndex = 0; _CharIndex < expression.Length ; _CharIndex++)
{
if ((qualifier != null) & (string.Compare(expression.Substring(_CharIndex, qualifier.Length), qualifier, ignoreCase) == 0))
{
_QualifierState = !(_QualifierState);
}
else if (!(_QualifierState) & (delimiter != null) & (string.Compare(expression.Substring(_CharIndex, delimiter.Length), delimiter, ignoreCase) == 0))
{
_Values.Add(expression.Substring(_StartIndex, _CharIndex - _StartIndex));
_StartIndex = _CharIndex + 1;
}
}

if (_StartIndex <= expression.Length)
_Values.Add(expression.Substring(_StartIndex, expression.Length - _StartIndex));

string[] _returnValues = new string[_Values.Count];
_Values.CopyTo(_returnValues);
return _returnValues;
}

Maybe the code will help you out. Sorry about the formatting.